forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownTemplate.cs
More file actions
162 lines (136 loc) · 3.9 KB
/
Copy pathMarkdownTemplate.cs
File metadata and controls
162 lines (136 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using ServiceStack.Markdown;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Support.Markdown
{
public class MarkdownTemplate : ITemplateWriter, IExpirable
{
public MarkdownTemplate() { }
public MarkdownTemplate(string fullPath, string name, string contents)
{
FilePath = fullPath;
Name = name;
Contents = contents;
}
public string FilePath { get; set; }
public string Name { get; set; }
public string Contents { get; set; }
public DateTime? LastModified { get; set; }
public string[] TextBlocks { get; set; }
public string[] VarRefBlocks { get; set; }
public static string BodyPlaceHolder = "Body";
public static string PlaceHolderPrefix = "<!--@";
public static string PlaceHolderSuffix = "-->";
internal Exception initException;
readonly object readWriteLock = new object();
private bool isBusy;
public void Reload(string templateContents, DateTime lastModified)
{
lock (readWriteLock)
{
try
{
isBusy = true;
this.Contents = templateContents;
this.LastModified = lastModified;
initException = null;
Prepare();
}
catch (Exception ex)
{
initException = ex;
}
isBusy = false;
Monitor.PulseAll(readWriteLock);
}
}
public void Prepare()
{
if (this.Contents == null)
throw new ArgumentNullException("Contents");
var textBlocks = new List<string>();
var variablePositions = new Dictionary<int, string>();
int pos;
var lastPos = 0;
while ((pos = this.Contents.IndexOf(PlaceHolderPrefix, lastPos)) != -1)
{
var contentBlock = this.Contents.Substring(lastPos, pos - lastPos);
textBlocks.Add(contentBlock);
var endPos = this.Contents.IndexOf(PlaceHolderSuffix, pos);
if (endPos == -1)
throw new InvalidDataException("Unterminated PlaceHolder expecting -->");
var varRef = this.Contents.Substring(
pos + PlaceHolderPrefix.Length, endPos - (pos + PlaceHolderPrefix.Length));
var index = textBlocks.Count;
variablePositions[index] = varRef;
lastPos = endPos + PlaceHolderSuffix.Length;
}
if (lastPos != this.Contents.Length - 1)
{
var lastBlock = this.Contents.Substring(lastPos);
textBlocks.Add(lastBlock);
}
this.TextBlocks = textBlocks.ToArray();
this.VarRefBlocks = new string[this.TextBlocks.Length];
foreach (var varPos in variablePositions)
{
this.VarRefBlocks[varPos.Key] = varPos.Value;
}
}
public string RenderToString(Dictionary<string, object> scopeArgs)
{
lock (readWriteLock)
{
while (isBusy)
Monitor.Wait(readWriteLock);
}
if (TextBlocks == null || VarRefBlocks == null)
throw new InvalidDataException("Template has not been Initialized.");
var sb = StringBuilderCache.Allocate();
for (var i = 0; i < TextBlocks.Length; i++)
{
var textBlock = TextBlocks[i];
var varName = VarRefBlocks[i];
if (varName != null && scopeArgs != null)
{
object varValue;
if (scopeArgs.TryGetValue(varName, out varValue))
{
sb.Append(varValue);
}
}
sb.Append(textBlock);
}
return StringBuilderCache.ReturnAndFree(sb);
}
public void Write(MarkdownViewBase instance, TextWriter textWriter, Dictionary<string, object> scopeArgs)
{
lock (readWriteLock)
{
while (isBusy)
Monitor.Wait(readWriteLock);
}
if (TextBlocks == null || VarRefBlocks == null)
throw new InvalidDataException("Template has not been Initialized.");
for (var i = 0; i < TextBlocks.Length; i++)
{
var textBlock = TextBlocks[i];
var varName = VarRefBlocks[i];
if (varName != null)
{
object varValue;
if (scopeArgs.TryGetValue(varName, out varValue))
{
textWriter.Write(varValue);
}
}
textWriter.Write(textBlock);
}
}
}
}