forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownFormat.cs
More file actions
704 lines (570 loc) · 26.2 KB
/
MarkdownFormat.cs
File metadata and controls
704 lines (570 loc) · 26.2 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ServiceStack.Host.Handlers;
using ServiceStack.Html;
using ServiceStack.IO;
using ServiceStack.Logging;
using ServiceStack.Markdown;
using ServiceStack.Support.Markdown;
using ServiceStack.Web;
namespace ServiceStack.Formats
{
public enum MarkdownPageType
{
ContentPage = 1,
ViewPage = 2,
SharedViewPage = 3,
}
public class MarkdownFormat : IViewEngine, IPlugin
{
private static readonly ILog Log = LogManager.GetLogger(typeof(MarkdownFormat));
private const string ErrorPageNotFound = "Could not find Markdown page '{0}'";
public static string DefaultTemplateName = "_Layout.shtml";
public static string DefaultTemplate = "Views/Shared/_Layout.shtml";
public static string DefaultPage = "default";
public static string TemplatePlaceHolder = "<!--@Body-->";
public static string WebHostUrlPlaceHolder = "~/";
public static string MarkdownExt = "md";
public static string TemplateExt = "shtml";
public static string SharedDir = "Views/Shared";
public static string[] PageExts = new[] { MarkdownExt, TemplateExt };
private static MarkdownFormat instance;
public static MarkdownFormat Instance => instance ?? (instance = new MarkdownFormat());
// ~/View - Dynamic Pages
public Dictionary<string, MarkdownPage> ViewPages = new Dictionary<string, MarkdownPage>(
StringComparer.CurrentCultureIgnoreCase);
// ~/View/Shared - Dynamic Shared Pages
public Dictionary<string, MarkdownPage> ViewSharedPages = new Dictionary<string, MarkdownPage>(
StringComparer.CurrentCultureIgnoreCase);
//Content Pages outside of ~/View
public Dictionary<string, MarkdownPage> ContentPages = new Dictionary<string, MarkdownPage>(
StringComparer.CurrentCultureIgnoreCase);
public Dictionary<string, MarkdownTemplate> MasterPageTemplates = new Dictionary<string, MarkdownTemplate>(
StringComparer.CurrentCultureIgnoreCase);
public Type MarkdownBaseType { get; set; }
public Dictionary<string, Type> MarkdownGlobalHelpers { get; set; }
public Func<string, IEnumerable<MarkdownPage>> FindMarkdownPagesFn { get; set; }
private readonly MarkdownSharp.Markdown markdown;
public IAppHost AppHost { get; set; }
public Dictionary<string, string> ReplaceTokens { get; set; }
public IVirtualPathProvider VirtualPathProvider { get; set; }
public bool CheckLastModifiedForChanges { get; set; }
readonly TemplateProvider templateProvider = new TemplateProvider(DefaultTemplateName);
public MarkdownFormat()
{
markdown = new MarkdownSharp.Markdown(); //Note: by default MarkdownDeep is used
this.MarkdownBaseType = typeof(MarkdownViewBase);
this.MarkdownGlobalHelpers = new Dictionary<string, Type>();
this.FindMarkdownPagesFn = FindMarkdownPages;
this.ReplaceTokens = new Dictionary<string, string>();
}
internal static readonly char[] DirSeps = new[] { '\\', '/' };
static HashSet<string> catchAllPathsNotFound = new HashSet<string>();
public void Register(IAppHost appHost)
{
if (instance == null) instance = this;
this.AppHost = appHost;
appHost.ViewEngines.Add(this);
if (!CheckLastModifiedForChanges)
CheckLastModifiedForChanges = appHost.Config.DebugMode;
foreach (var ns in appHost.Config.RazorNamespaces)
Evaluator.AddAssembly(ns);
this.MarkdownBaseType = this.MarkdownBaseType;
this.MarkdownGlobalHelpers = this.MarkdownGlobalHelpers;
this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary<string, string>();
var webHostUrl = appHost.Config.WebHostUrl;
if (!webHostUrl.IsNullOrEmpty())
this.ReplaceTokens["~/"] = webHostUrl.WithTrailingSlash();
if (VirtualPathProvider == null)
VirtualPathProvider = AppHost.VirtualFileSources;
RegisterMarkdownPages(appHost.Config.WebHostPhysicalPath);
appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
MarkdownPage markdownPage = null;
if (catchAllPathsNotFound.Contains(pathInfo))
return null;
markdownPage = FindByPathInfo(pathInfo);
if (CheckLastModifiedForChanges)
ReloadModifiedPageAndTemplates(markdownPage);
if (markdownPage == null)
{
if (pathInfo.EndsWith(".md"))
{
pathInfo = pathInfo.EndsWithIgnoreCase(DefaultPage + ".md")
? pathInfo.Substring(0, pathInfo.Length - (DefaultPage + ".md").Length)
: pathInfo.WithoutExtension();
return new RedirectHttpHandler {
AbsoluteUrl = webHostUrl.IsNullOrEmpty()
? null
: webHostUrl.CombineWith(pathInfo),
RelativeUrl = webHostUrl.IsNullOrEmpty()
? pathInfo
: null
};
}
if (catchAllPathsNotFound.Count > 1000) //prevent DDOS
catchAllPathsNotFound = new HashSet<string>();
var tmp = new HashSet<string>(catchAllPathsNotFound) { pathInfo };
catchAllPathsNotFound = tmp;
return null;
}
return new MarkdownHandler(pathInfo) {
MarkdownFormat = this,
MarkdownPage = markdownPage,
RequestName = "MarkdownPage"
};
});
appHost.ContentTypes.RegisterAsync(MimeTypes.MarkdownText, SerializeToStreamAsync, null);
appHost.ContentTypes.RegisterAsync(MimeTypes.PlainText, SerializeToStreamAsync, null);
appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.MarkdownText.ToContentFormat());
appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.PlainText.ToContentFormat());
}
public MarkdownPage FindByPathInfo(string pathInfo)
{
var normalizedPathInfo = pathInfo.IsNullOrEmpty() ? DefaultPage : pathInfo.TrimStart(DirSeps);
var markdownPage = GetContentPage(
normalizedPathInfo,
normalizedPathInfo.CombineWith(DefaultPage));
return markdownPage;
}
public Task<bool> ProcessRequestAsync(IRequest req, object dto, Stream outputStream)
{
MarkdownPage markdownPage;
if ((markdownPage = GetViewPageByResponse(dto, req)) == null)
return TypeConstants.FalseTask;
if (CheckLastModifiedForChanges)
ReloadModifiedPageAndTemplates(markdownPage);
return ProcessMarkdownPage(req, markdownPage, dto, outputStream)
? TypeConstants.TrueTask
: TypeConstants.FalseTask;
}
public bool HasView(string viewName, IRequest httpReq = null)
{
return GetViewPage(viewName, httpReq) != null;
}
public string RenderPartial(string pageName, object model, bool renderHtml, StreamWriter writer, IHtmlContext htmlHelper = null)
{
var markdownPage = ReloadIfNeeded(GetViewPage(pageName, htmlHelper.GetHttpRequest()));
var output = RenderDynamicPage(markdownPage, pageName, model, renderHtml, false);
if (writer != null)
{
writer.Write(output);
writer.Flush();
return null;
}
return output;
}
public MarkdownPage GetViewPage(string viewName, IRequest httpReq)
{
var view = GetViewPage(viewName);
if (view != null) return view;
if (httpReq?.PathInfo == null) return null;
var normalizedPathInfo = httpReq.PathInfo;
if (!httpReq.RawUrl.EndsWith("/"))
normalizedPathInfo = normalizedPathInfo.ParentDirectory();
normalizedPathInfo = normalizedPathInfo.CombineWith(viewName).TrimStart(DirSeps);
view = GetContentPage(
normalizedPathInfo,
normalizedPathInfo.CombineWith(DefaultPage));
return view;
}
public bool ProcessMarkdownPage(IRequest httpReq, MarkdownPage markdownPage, object dto, Stream outputStream)
{
var httpRes = httpReq.Response;
httpRes.AddHeaderLastModified(markdownPage.GetLastModified());
var renderInTemplate = true;
var renderHtml = true;
string format;
if ((format = httpReq.QueryString[Keywords.Format]) != null)
{
renderHtml = !(format.StartsWithIgnoreCase("markdown")
|| format.StartsWithIgnoreCase("text")
|| format.StartsWithIgnoreCase("plain"));
renderInTemplate = !httpReq.GetFormatModifier().StartsWithIgnoreCase(Keywords.Bare);
}
if (!renderHtml)
{
httpRes.ContentType = MimeTypes.PlainText;
}
var template = httpReq.GetTemplate();
var markup = RenderDynamicPage(markdownPage, markdownPage.Name, dto, renderHtml, renderInTemplate, template);
var markupBytes = markup.ToUtf8Bytes();
outputStream.Write(markupBytes, 0, markupBytes.Length);
return true;
}
public void ReloadModifiedPageAndTemplates(MarkdownPage markdownPage)
{
if (markdownPage == null || !CheckLastModifiedForChanges) return;
ReloadIfNeeded(markdownPage);
IVirtualFile latestPage;
MarkdownTemplate template;
if (markdownPage.DirectiveTemplate != null
&& this.MasterPageTemplates.TryGetValue(markdownPage.DirectiveTemplate, out template))
{
latestPage = GetLatestPage(markdownPage.DirectiveTemplate);
if (latestPage.LastModified > template.LastModified)
template.Reload(GetPageContents(latestPage), latestPage.LastModified);
}
if (markdownPage.Template != null
&& this.MasterPageTemplates.TryGetValue(markdownPage.Template, out template))
{
latestPage = GetLatestPage(template);
if (latestPage.LastModified > template.LastModified)
template.Reload(GetPageContents(latestPage), latestPage.LastModified);
}
}
private MarkdownPage ReloadIfNeeded(MarkdownPage markdownPage)
{
if (markdownPage == null || !CheckLastModifiedForChanges) return markdownPage;
if (markdownPage.FilePath != null)
{
var latestPage = GetLatestPage(markdownPage);
if (latestPage == null) return markdownPage;
if (latestPage.LastModified > markdownPage.LastModified)
{
markdownPage.Reload(GetPageContents(latestPage), latestPage.LastModified);
}
}
return markdownPage;
}
private IVirtualFile GetLatestPage(MarkdownPage markdownPage)
{
var file = VirtualPathProvider.GetFile(markdownPage.FilePath);
return file;
}
private IVirtualFile GetLatestPage(string markdownPagePath)
{
var file = VirtualPathProvider.GetFile(markdownPagePath);
return file;
}
private IVirtualFile GetLatestPage(MarkdownTemplate markdownPage)
{
var file = VirtualPathProvider.GetFile(markdownPage.FilePath);
return file;
}
/// <summary>
/// Render Markdown for text/markdown and text/plain ContentTypes
/// </summary>
public async Task SerializeToStreamAsync(IRequest request, object response, Stream stream)
{
var dto = response.GetDto();
if (dto is string text)
{
var bytes = text.ToUtf8Bytes();
stream.Write(bytes, 0, bytes.Length);
return;
}
MarkdownPage markdownPage;
if ((markdownPage = GetViewPageByResponse(dto, request)) == null)
{
if (response is ErrorResponse || response is IHttpResult)
{
var html = HostContext.GetPlugin<HtmlFormat>();
if (html != null)
{
await html.SerializeToStreamAsync(request, response, request.Response.OutputStream);
return;
}
}
throw new InvalidDataException(ErrorPageNotFound.FormatWith(GetPageName(dto, request)));
}
ReloadModifiedPageAndTemplates(markdownPage);
const bool renderHtml = false; //i.e. render Markdown
var markup = RenderStaticPage(markdownPage, renderHtml);
var markupBytes = markup.ToUtf8Bytes();
await stream.WriteAsync(markupBytes, 0, markupBytes.Length);
}
public string GetPageName(object dto, IRequest req)
{
if (dto is IHttpResult httpResult)
{
dto = httpResult.Response;
}
return dto != null
? dto.GetType().GetOperationName()
: req?.OperationName;
}
public MarkdownPage GetViewPageByResponse(object dto, IRequest httpReq)
{
if (dto is IHttpResult httpResult)
{
dto = httpResult.Response;
}
//If View was specified don't look for anything else.
var viewName = httpReq.GetView();
if (viewName != null)
return GetViewPage(viewName);
if (dto != null)
{
var responseTypeName = dto.GetType().GetOperationName();
var markdownPage = GetViewPage(responseTypeName);
if (markdownPage != null) return markdownPage;
}
return httpReq != null ? GetViewPage(httpReq.OperationName) : null;
}
public MarkdownPage GetViewPage(string pageName)
{
if (pageName == null) return null;
MarkdownPage markdownPage;
ViewPages.TryGetValue(pageName, out markdownPage);
if (markdownPage != null) return markdownPage;
ViewSharedPages.TryGetValue(pageName, out markdownPage);
return markdownPage;
}
public MarkdownPage GetContentPage(string pageFilePath)
{
MarkdownPage markdownPage;
ContentPages.TryGetValue(pageFilePath, out markdownPage);
return markdownPage;
}
public MarkdownPage GetContentPage(params string[] pageFilePaths)
{
foreach (var pageFilePath in pageFilePaths)
{
var markdownPage = GetContentPage(pageFilePath);
if (markdownPage != null)
return markdownPage;
}
return null;
}
public void RegisterMarkdownPages(string dirPath)
{
foreach (var page in FindMarkdownPagesFn(dirPath))
{
AddPage(page);
}
var templateFiles = VirtualPathProvider.GetAllMatchingFiles("*." + TemplateExt);
foreach (var templateFile in templateFiles)
{
try
{
var templateContents = GetPageContents(templateFile);
AddTemplate(templateFile.VirtualPath, templateContents);
}
catch (Exception ex)
{
Log.Error("AddTemplate(): " + ex.Message, ex);
}
}
}
public IEnumerable<MarkdownPage> FindMarkdownPages(string dirPath)
{
var hasReloadableWebPages = false;
var markDownFiles = VirtualPathProvider.GetAllMatchingFiles("*." + MarkdownExt);
foreach (var markDownFile in markDownFiles)
{
if (markDownFile.ShouldSkipPath()) continue;
if (markDownFile.GetType().GetOperationName() != "ResourceVirtualFile")
hasReloadableWebPages = true;
var pageName = markDownFile.Name.WithoutExtension();
var pageContents = GetPageContents(markDownFile);
var pageType = MarkdownPageType.ContentPage;
if (VirtualPathProvider.IsSharedFile(markDownFile))
pageType = MarkdownPageType.SharedViewPage;
else if (VirtualPathProvider.IsViewFile(markDownFile))
pageType = MarkdownPageType.ViewPage;
var templatePath = pageType == MarkdownPageType.ContentPage
? templateProvider.GetTemplatePath(markDownFile.Directory)
: null;
yield return new MarkdownPage(this, markDownFile.VirtualPath,
pageName, pageContents, pageType) {
Template = templatePath,
LastModified = markDownFile.LastModified,
};
}
if (!hasReloadableWebPages)
CheckLastModifiedForChanges = false;
}
public void RegisterMarkdownPage(MarkdownPage markdownPage)
{
AddPage(markdownPage);
}
public MarkdownPage RefreshPage(string filePath)
{
var markdownPage = GetContentPage(SanitizePath(filePath));
if (markdownPage == null)
throw new ArgumentException("No MarkdownPage found at: " + filePath);
var latestPage = GetLatestPage(markdownPage);
markdownPage.Reload(GetPageContents(latestPage), latestPage.LastModified);
return markdownPage;
}
public void AddPage(MarkdownPage page)
{
try
{
page.Compile();
AddViewPage(page);
}
catch (Exception ex)
{
Log.Error("AddViewPage() page.Prepare(): " + ex.Message, ex);
}
try
{
var templatePath = page.Template;
if (page.Template == null) return;
if (MasterPageTemplates.ContainsKey(templatePath)) return;
var templateFile = VirtualPathProvider.GetFile(templatePath);
var templateContents = GetPageContents(templateFile);
AddTemplate(templatePath, templateContents);
}
catch (Exception ex)
{
Log.Error("Error compiling template " + page.Template + ": " + ex.Message, ex);
}
}
private void AddViewPage(MarkdownPage page)
{
switch (page.PageType)
{
case MarkdownPageType.ViewPage:
ViewPages.Add(page.Name, page);
break;
case MarkdownPageType.SharedViewPage:
ViewSharedPages.Add(page.Name, page);
break;
case MarkdownPageType.ContentPage:
ContentPages.Add(SanitizePath(page.FilePath), page);
break;
}
}
private static string SanitizePath(string filePath)
{
return filePath.WithoutExtension().TrimStart(DirSeps);
}
public MarkdownTemplate AddTemplate(string templatePath, string templateContents)
{
MarkdownTemplate template;
if (MasterPageTemplates.TryGetValue(templatePath, out template))
return template;
var templateFile = VirtualPathProvider.GetFile(templatePath);
var templateName = templateFile.Name.WithoutExtension();
template = new MarkdownTemplate(templatePath, templateName, templateContents) {
LastModified = templateFile.LastModified,
};
MasterPageTemplates.Add(templatePath, template);
try
{
template.Prepare();
return template;
}
catch (Exception ex)
{
Log.Error("AddViewPage() template.Prepare(): " + ex.Message, ex);
return null;
}
}
private string GetPageContents(IVirtualFile page)
{
return ReplaceContentWithRewriteTokens(page.ReadAllText());
}
private string ReplaceContentWithRewriteTokens(string contents)
{
foreach (var replaceToken in ReplaceTokens)
{
contents = contents.Replace(replaceToken.Key, replaceToken.Value);
}
return contents;
}
public string Transform(string template)
{
return markdown.Transform(template);
}
public string Transform(string template, bool renderHtml)
{
return renderHtml ? markdown.Transform(template) : template;
}
public string RenderStaticPageHtml(string filePath)
{
return RenderStaticPage(filePath, true);
}
public string RenderStaticPage(string filePath, bool renderHtml)
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
filePath = filePath.WithoutExtension();
MarkdownPage markdownPage;
if (!ContentPages.TryGetValue(filePath, out markdownPage))
throw new InvalidDataException(ErrorPageNotFound.FormatWith(filePath));
return RenderStaticPage(markdownPage, renderHtml);
}
private string RenderStaticPage(MarkdownPage markdownPage, bool renderHtml)
{
//TODO: Optimize if contains no dynamic elements
return RenderDynamicPage(markdownPage, new Dictionary<string, object>(), renderHtml, true);
}
private string RenderInTemplateIfAny(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs, string pageHtml, string templatePath = null)
{
MarkdownTemplate markdownTemplate = null;
if (templatePath != null)
MasterPageTemplates.TryGetValue(templatePath, out markdownTemplate);
var directiveTemplate = markdownPage.DirectiveTemplate;
if (markdownTemplate == null && directiveTemplate != null)
{
if (!MasterPageTemplates.TryGetValue(directiveTemplate, out markdownTemplate))
{
var templateInSharedPath = "{0}/{1}.shtml".Fmt(SharedDir, directiveTemplate);
if (!MasterPageTemplates.TryGetValue(templateInSharedPath, out markdownTemplate))
{
var virtualFile = VirtualPathProvider.GetFile(directiveTemplate);
if (virtualFile == null)
throw new FileNotFoundException("Could not find template: " + directiveTemplate);
var templateContents = GetPageContents(virtualFile);
markdownTemplate = AddTemplate(directiveTemplate, templateContents);
}
}
}
if (markdownTemplate == null)
{
if (markdownPage.Template != null)
MasterPageTemplates.TryGetValue(markdownPage.Template, out markdownTemplate);
if (markdownTemplate == null && templatePath == null)
MasterPageTemplates.TryGetValue(DefaultTemplate, out markdownTemplate);
if (markdownTemplate == null)
{
if (templatePath == null)
return pageHtml;
throw new Exception("No template found for page: " + markdownPage.FilePath);
}
}
if (scopeArgs != null)
scopeArgs[MarkdownTemplate.BodyPlaceHolder] = pageHtml;
var htmlPage = markdownTemplate.RenderToString(scopeArgs);
return htmlPage;
}
public string RenderDynamicPageHtml(string pageName, object model)
{
return RenderDynamicPage(pageName, model, true);
}
public string RenderDynamicPageHtml(string pageName)
{
return RenderDynamicPage(GetViewPage(pageName), new Dictionary<string, object>(), true, true);
}
public string RenderDynamicPageHtml(string pageName, Dictionary<string, object> scopeArgs)
{
return RenderDynamicPage(GetViewPage(pageName), scopeArgs, true, true);
}
public string RenderDynamicPage(string pageName, object model, bool renderHtml)
{
return RenderDynamicPage(GetViewPage(pageName), pageName, model, renderHtml, true);
}
private string RenderDynamicPage(MarkdownPage markdownPage, string pageName, object model, bool renderHtml, bool renderTemplate, string templatePath = null)
{
if (markdownPage == null)
throw new InvalidDataException(ErrorPageNotFound.FormatWith(pageName));
var scopeArgs = new Dictionary<string, object> { { MarkdownPage.ModelName, model } };
return RenderDynamicPage(markdownPage, scopeArgs, renderHtml, renderTemplate, templatePath);
}
public string RenderDynamicPage(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs,
bool renderHtml, bool renderTemplate, string templatePath = null)
{
scopeArgs = scopeArgs ?? new Dictionary<string, object>();
var htmlPage = markdownPage.RenderToString(scopeArgs, renderHtml);
if (!renderTemplate) return htmlPage;
var html = RenderInTemplateIfAny(
markdownPage, scopeArgs, htmlPage, templatePath);
return html;
}
}
}