forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FluidViewRenderer.cs
281 lines (217 loc) · 10.8 KB
/
FluidViewRenderer.cs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using Fluid.Ast;
using Fluid.Parser;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Fluid.ViewEngine
{
/// <summary>
/// This class is registered as a singleton.
/// </summary>
public class FluidViewRenderer : IFluidViewRenderer
{
private static readonly char[] PathSeparators = { '/', '\\' };
private record struct LayoutKey (string ViewPath, string LayoutPath);
private class CacheEntry
{
public IDisposable Callback;
public ConcurrentDictionary<string, IFluidTemplate> TemplateCache = new();
}
private readonly ConcurrentDictionary<IFileProvider, CacheEntry> _cache = new();
private readonly ConcurrentDictionary<LayoutKey, string> _layoutsCache = new();
public FluidViewRenderer(FluidViewEngineOptions fluidViewEngineOptions)
{
_fluidViewEngineOptions = fluidViewEngineOptions;
_fluidViewEngineOptions.TemplateOptions.FileProvider = _fluidViewEngineOptions.PartialsFileProvider ?? _fluidViewEngineOptions.ViewsFileProvider ?? new NullFileProvider();
}
private readonly FluidViewEngineOptions _fluidViewEngineOptions;
public virtual async Task RenderViewAsync(TextWriter writer, string relativePath, TemplateContext context)
{
// Provide some services to all statements
context.AmbientValues[Constants.ViewPathIndex] = relativePath;
context.AmbientValues[Constants.SectionsIndex] = null; // it is lazily initialized when first used
context.AmbientValues[Constants.RendererIndex] = this;
var template = await GetFluidTemplateAsync(relativePath, _fluidViewEngineOptions.ViewsFileProvider, true);
if (_fluidViewEngineOptions.RenderingViewAsync != null)
{
await _fluidViewEngineOptions.RenderingViewAsync.Invoke(relativePath, context);
}
// The body is rendered and buffered before the Layout since it can contain fragments
// that need to be rendered as part of the Layout.
// Also the body or its _ViewStarts might contain a Layout tag.
// The context is not isolated such that variables can be changed by views
var body = await template.RenderAsync(context, _fluidViewEngineOptions.TextEncoder, isolateContext: false);
// If a layout is specified while rendering a view, execute it
if (context.AmbientValues.TryGetValue(Constants.LayoutIndex, out var layoutPath) && layoutPath is string layoutPathString && !String.IsNullOrEmpty(layoutPathString))
{
layoutPathString = ResolveLayoutPath(relativePath, layoutPathString, _fluidViewEngineOptions.ViewsFileProvider);
context.AmbientValues[Constants.ViewPathIndex] = layoutPathString;
context.AmbientValues[Constants.BodyIndex] = body;
// Parse the Layout file but ignore viewstarts
var layoutTemplate = await GetFluidTemplateAsync(layoutPathString, _fluidViewEngineOptions.ViewsFileProvider, includeViewStarts: false);
await layoutTemplate.RenderAsync(writer, _fluidViewEngineOptions.TextEncoder, context);
}
else
{
await writer.WriteAsync(body);
}
}
public virtual async Task RenderPartialAsync(TextWriter writer, string relativePath, TemplateContext context)
{
// Substitute View Path
context.AmbientValues[Constants.ViewPathIndex] = relativePath;
if (_fluidViewEngineOptions.RenderingViewAsync != null)
{
await _fluidViewEngineOptions.RenderingViewAsync.Invoke(relativePath, context);
}
var template = await GetFluidTemplateAsync(relativePath, _fluidViewEngineOptions.PartialsFileProvider, false);
await template.RenderAsync(writer, _fluidViewEngineOptions.TextEncoder, context);
}
protected virtual List<string> FindViewStarts(string viewPath, IFileProvider fileProvider)
{
var viewStarts = new List<string>();
int index = viewPath.Length - 1;
while (!String.IsNullOrEmpty(viewPath))
{
if (index == -1)
{
return viewStarts;
}
index = viewPath.LastIndexOfAny(PathSeparators, index);
viewPath = viewPath.Substring(0, index + 1);
var viewStartPath = viewPath + Constants.ViewStartFilename;
var viewStartInfo = fileProvider.GetFileInfo(viewStartPath);
if (viewStartInfo.Exists)
{
viewStarts.Add(viewStartPath);
}
index = index - 1;
}
viewStarts.Reverse();
return viewStarts;
}
protected virtual string ResolveLayoutPath(string viewPath, string layoutPath, IFileProvider fileProvider)
{
// When a partial view is referenced by name without a file extension, the following locations are searched in the stated order:
// Currently executing view's folder
// Directory graph above the view's folder
// options.LayoutsLocationFormats
if (layoutPath.EndsWith(Constants.ViewExtension))
{
return Path.Combine(Path.GetDirectoryName(viewPath), layoutPath);
}
var key = new LayoutKey(viewPath, layoutPath);
return _layoutsCache.GetOrAdd(key, k =>
{
var layoutPath = k.LayoutPath;
var viewPath = k.ViewPath;
int index = viewPath.Length - 1;
while (!String.IsNullOrEmpty(viewPath))
{
if (index == -1)
{
return layoutPath;
}
index = viewPath.LastIndexOfAny(PathSeparators, index);
viewPath = viewPath.Substring(0, index + 1);
var layoutPathPath = Path.Combine(viewPath, layoutPath) + Constants.ViewExtension;
var layoutPathInfo = fileProvider.GetFileInfo(layoutPathPath);
if (layoutPathInfo.Exists)
{
return layoutPathPath;
}
index = index - 1;
}
// Not found in hierarchy, fall-back to LayoutsLocationFormats
foreach (var location in _fluidViewEngineOptions.LayoutsLocationFormats)
{
var layoutPathPath = String.Format(location, Path.GetFileName(layoutPath));
var layoutPathInfo = fileProvider.GetFileInfo(layoutPathPath);
if (layoutPathInfo.Exists)
{
return layoutPathPath;
}
}
return layoutPath;
});
}
protected virtual async ValueTask<IFluidTemplate> GetFluidTemplateAsync(string path, IFileProvider fileProvider, bool includeViewStarts)
{
var cache = _cache.GetOrAdd(fileProvider, f =>
{
var cacheEntry = new CacheEntry();
if (_fluidViewEngineOptions.TrackFileChanges)
{
Action<object> callback = null;
callback = c =>
{
// The order here is important. We need to take the token and then apply our changes BEFORE
// registering. This prevents us from possible having two change updates to process concurrently.
//
// If the file changes after we take the token, then we'll process the update immediately upon
// registering the callback.
var entry = (CacheEntry)c;
var previousCallBack = entry.Callback;
previousCallBack?.Dispose();
var token = fileProvider.Watch("**/*" + Constants.ViewExtension);
entry.TemplateCache.Clear();
entry.Callback = token.RegisterChangeCallback(callback, c);
};
cacheEntry.Callback = fileProvider.Watch("**/*" + Constants.ViewExtension).RegisterChangeCallback(callback, cacheEntry);
}
return cacheEntry;
});
if (cache.TemplateCache.TryGetValue(path, out var template))
{
return template;
}
template = await ParseLiquidFileAsync(path, fileProvider, includeViewStarts);
cache.TemplateCache[path] = template;
return template;
}
protected virtual async ValueTask<IFluidTemplate> ParseLiquidFileAsync(string path, IFileProvider fileProvider, bool includeViewStarts)
{
var fileInfo = fileProvider.GetFileInfo(path);
if (!fileInfo.Exists)
{
return new FluidTemplate();
}
var subTemplates = new List<IFluidTemplate>();
if (includeViewStarts)
{
// Add ViewStart files
foreach (var viewStartPath in FindViewStarts(path, fileProvider))
{
// Redefine the current view path while processing ViewStart files
var callbackTemplate = new FluidTemplate(new CallbackStatement((writer, encoder, context) =>
{
context.AmbientValues[Constants.ViewPathIndex] = viewStartPath;
return new ValueTask<Completion>(Completion.Normal);
}));
var viewStartTemplate = await GetFluidTemplateAsync(viewStartPath, fileProvider, false);
subTemplates.Add(callbackTemplate);
subTemplates.Add(viewStartTemplate);
}
}
using (var stream = fileInfo.CreateReadStream())
{
using (var sr = new StreamReader(stream))
{
var fileContent = sr.ReadToEnd();
if (_fluidViewEngineOptions.Parser.TryParse(fileContent, out var template, out var errors))
{
subTemplates.Add(template);
return new CompositeFluidTemplate(subTemplates);
}
else
{
throw new ParseException(errors);
}
}
}
}
}
}