forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewPage.cs
More file actions
234 lines (194 loc) · 5.43 KB
/
ViewPage.cs
File metadata and controls
234 lines (194 loc) · 5.43 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web;
using ServiceStack.MiniProfiler;
using ServiceStack.WebHost.Endpoints.Support.Markdown;
namespace ServiceStack.RazorEngine
{
public class ViewPage
{
public RazorFormat RazorFormat { get; set; }
public string Layout { get; set; }
public string FilePath { get; set; }
public string Name { get; set; }
public string Contents { get; set; }
public RazorPageType PageType { get; set; }
public string TemplatePath { get; set; }
public string DirectiveTemplatePath { get; set; }
public DateTime? LastModified { get; set; }
public List<IExpirable> Dependents { get; private set; }
public const string ModelName = "Model";
public ViewPage()
{
this.Dependents = new List<IExpirable>();
}
public ViewPage(RazorFormat razorFormat, string fullPath, string name, string contents)
: this(razorFormat, fullPath, name, contents, RazorPageType.ViewPage) {}
public ViewPage(RazorFormat razorFormat, string fullPath, string name, string contents, RazorPageType pageType)
: this()
{
RazorFormat = razorFormat;
FilePath = fullPath;
Name = name;
Contents = contents;
PageType = pageType;
}
public DateTime? GetLastModified()
{
//if (!hasCompletedFirstRun) return null;
var lastModified = this.LastModified;
foreach (var expirable in this.Dependents)
{
if (!expirable.LastModified.HasValue) continue;
if (!lastModified.HasValue || expirable.LastModified > lastModified)
{
lastModified = expirable.LastModified;
}
}
return lastModified;
}
public string GetTemplatePath()
{
return this.DirectiveTemplatePath ?? this.TemplatePath;
}
public string PageName
{
get
{
return this.PageType == RazorPageType.Template
|| this.PageType == RazorPageType.ContentPage
? this.FilePath
: this.Name;
}
}
public void Prepare()
{
Razor.Compile(this.Contents, PageName);
}
private int timesRun;
private Exception initException;
readonly object readWriteLock = new object();
private bool isBusy;
public void Reload()
{
var contents = File.ReadAllText(this.FilePath);
Reload(contents);
}
public void Reload(string contents)
{
var fi = new FileInfo(this.FilePath);
var lastModified = fi.LastWriteTime;
lock (readWriteLock)
{
try
{
isBusy = true;
this.Contents = contents;
foreach (var markdownReplaceToken in RazorFormat.MarkdownReplaceTokens)
{
this.Contents = this.Contents.Replace(markdownReplaceToken.Key, markdownReplaceToken.Value);
}
this.LastModified = lastModified;
initException = null;
timesRun = 0;
Prepare();
}
catch (Exception ex)
{
initException = ex;
}
isBusy = false;
Monitor.PulseAll(readWriteLock);
}
}
public string RenderToHtml()
{
return RenderToString((object)null);
}
public string RenderToHtml<T>(T model)
{
return RenderToString(model);
}
public string RenderToString<T>(T model)
{
var template = RazorFormat.ExecuteTemplate(model, this.PageName, this.TemplatePath);
return template.Result;
}
public IRazorTemplate GetRazorTemplate()
{
return Razor.DefaultTemplateService.GetTemplate(this.PageName);
}
//From https://github.com/NancyFx/Nancy/blob/master/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs
public virtual void Write(object value)
{
WriteLiteral(HtmlEncode(value));
}
public virtual void WriteLiteral(object value)
{
//contents.Append(value);
}
public virtual void WriteTo(TextWriter writer, object value)
{
writer.Write(HtmlEncode(value));
}
public virtual void WriteLiteralTo(TextWriter writer, object value)
{
writer.Write(value);
}
public virtual void WriteTo(TextWriter writer, HelperResult value)
{
if (value != null)
{
value.WriteTo(writer);
}
}
public virtual void WriteLiteralTo(TextWriter writer, HelperResult value)
{
//if (value != null)
//{
// value.WriteTo(writer);
//}
}
public virtual void DefineSection(string sectionName, Action action)
{
//this.Sections.Add(sectionName, action);
}
public virtual object RenderBody()
{
//this.contents.Append(this.childBody);
return null;
}
public virtual object RenderSection(string sectionName)
{
return this.RenderSection(sectionName, true);
}
public virtual object RenderSection(string sectionName, bool required)
{
//string sectionContent;
//var exists = this.childSections.TryGetValue(sectionName, out sectionContent);
//if (!exists && required)
//{
// throw new InvalidOperationException("Section name " + sectionName + " not found and is required.");
//}
//this.contents.Append(sectionContent ?? String.Empty);
return null;
}
public virtual bool IsSectionDefined(string sectionName)
{
//return this.childSections.ContainsKey(sectionName);
return false;
}
private static string HtmlEncode(object value)
{
if (value == null)
{
return null;
}
var str = value as IHtmlString;
return str != null ? str.ToHtmlString() : HttpUtility.HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture));
}
}
}