public static void Main(string[] args) { // Get executing path and /example.md full path string exeLocation = Assembly.GetExecutingAssembly().Location; string path = Path.GetDirectoryName( exeLocation ); string template = Path.Combine(path, "example.md"); // Create the markdown-razor template compiler MarkdownFormat format = new MarkdownFormat(); string contents = File.ReadAllText(template); var page = new MarkdownPage(format, path, "example", contents ); format.AddPage(page); // Create our view container (ViewBag) var view = new Dictionary<string, object>() { { "examples", examples } }; // Compile and output. // This can be redirected to html file // e.g. RazorExample.exe > output.html var html = format.RenderDynamicPageHtml("example", view); Console.WriteLine(html); }
public void Init(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs, bool renderHtml, ViewDataDictionary viewData, HtmlHelper htmlHelper) { Init(null, null, markdownPage.Markdown, viewData, htmlHelper); this.RenderHtml = renderHtml; this.MarkdownPage = markdownPage; this.ScopeArgs = scopeArgs; }
public void Does_handle_foreach_when_enumerable_is_empty_first_time() { var content = (string)dynamicListPageContent.Clone(); var markdownPage = new MarkdownPage(new MarkdownFormat(), dynamicListPagePath, "", content); markdownPage.Compile(); var model = new Person { Links = new List<Link>() }; var scopeArgs = new Dictionary<string, object> { { "Model", model } }; markdownPage.RenderToHtml(scopeArgs); // First time the list is empty var expected = "A new list item"; model.Links.Add(new Link { Name = expected } ); var html = markdownPage.RenderToHtml(scopeArgs); // Second time the list has 1 item Console.WriteLine(html); Assert.That(html, Contains.Substring(expected)); }
private string RenderInTemplateIfAny(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs, string pageHtml) { MarkdownTemplate markdownTemplate = null; var directiveTemplatePath = markdownPage.DirectiveTemplatePath; if (directiveTemplatePath != null) { if (!PageTemplates.TryGetValue(directiveTemplatePath, out markdownTemplate)) { if (!File.Exists(directiveTemplatePath)) throw new FileNotFoundException("Could not find template: " + directiveTemplatePath); var templateContents = File.ReadAllText(directiveTemplatePath); markdownTemplate = AddTemplate(directiveTemplatePath, templateContents); } } if (markdownTemplate == null) { var templatePath = markdownPage.TemplatePath; if (templatePath == null) return pageHtml; markdownTemplate = PageTemplates[templatePath]; } if (scopeArgs != null) scopeArgs[MarkdownTemplate.BodyPlaceHolder] = pageHtml; var htmlPage = markdownTemplate.RenderToString(scopeArgs); return htmlPage; }
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; }
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 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 void ReloadModifiedPageAndTemplates(MarkdownPage markdownPage) { var lastWriteTime = File.GetLastWriteTime(markdownPage.FilePath); if (lastWriteTime > markdownPage.LastModified) { markdownPage.Reload(); } MarkdownTemplate template; if (markdownPage.DirectiveTemplatePath != null && this.PageTemplates.TryGetValue(markdownPage.DirectiveTemplatePath, out template)) { lastWriteTime = File.GetLastWriteTime(markdownPage.DirectiveTemplatePath); if (lastWriteTime > template.LastModified) ReloadTemplate(template); } if (markdownPage.TemplatePath != null && this.PageTemplates.TryGetValue(markdownPage.TemplatePath, out template)) { lastWriteTime = File.GetLastWriteTime(markdownPage.TemplatePath); if (lastWriteTime > template.LastModified) ReloadTemplate(template); } }
public bool ProcessMarkdownPage(IHttpRequest httpReq, MarkdownPage markdownPage, object dto, IHttpResponse httpRes) { httpRes.AddHeaderLastModified(markdownPage.GetLastModified()); var renderInTemplate = true; var renderHtml = true; string format; if (httpReq != null && (format = httpReq.QueryString["format"]) != null) { renderHtml = !(format.StartsWithIgnoreCase("markdown") || format.StartsWithIgnoreCase("text") || format.StartsWithIgnoreCase("plain")); renderInTemplate = !httpReq.GetFormatModifier().StartsWithIgnoreCase("bare"); } if (!renderHtml) { httpRes.ContentType = ContentType.PlainText; } var template = httpReq.GetTemplate(); var markup = RenderDynamicPage(markdownPage, markdownPage.Name, dto, renderHtml, renderInTemplate, template); var markupBytes = markup.ToUtf8Bytes(); httpRes.OutputStream.Write(markupBytes, 0, markupBytes.Length); return true; }
public PageContext Create(MarkdownPage markdownPage, bool renderHtml) { return(new PageContext(markdownPage, ScopeArgs, renderHtml)); }
public PageContext(MarkdownPage markdownPage, Dictionary <string, object> scopeArgs, bool renderHtml) { MarkdownPage = markdownPage; ScopeArgs = scopeArgs ?? new Dictionary <string, object>(); RenderHtml = renderHtml; }
private void Prepare(MarkdownPage markdownPage) { var rawMethodExpr = methodExpr.Replace("Html.", ""); if (rawMethodExpr == "Partial") { this.DependentPageName = this.Condition.ExtractContents("\"", "\""); } this.WriteRawHtml = rawMethodExpr == "Raw"; var parts = methodExpr.Split('.'); if (parts.Length > 2) { throw new ArgumentException("Unable to resolve method: " + methodExpr); } var usesBaseType = parts.Length == 1; var typePropertyName = parts[0]; var methodName = usesBaseType ? parts[0] : parts[1]; Type type = null; if (typePropertyName == "Html") { type = markdownPage.ExecutionContext.BaseType.HasGenericType() ? typeof(HtmlHelper <>) : typeof(HtmlHelper); } if (type == null) { type = usesBaseType ? markdownPage.ExecutionContext.BaseType : markdownPage.Markdown.MarkdownGlobalHelpers.TryGetValue(typePropertyName, out type) ? type : null; } if (type == null) { throw new InvalidDataException(string.Format( "Unable to resolve type '{0}'. Check type exists in Config.MarkdownBaseType or Page.Markdown.MarkdownGlobalHelpers", typePropertyName)); } try { var mi = methodName == "Partial" ? type.GetMethods(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault(m => m.GetParameters().Length == 2 && m.Name == methodName) : type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance); if (mi == null) { mi = HtmlHelper.GetMethod(methodName); if (mi == null) { throw new ArgumentException("Unable to resolve method '" + methodExpr + "' on type " + type.Name); } } base.ReturnType = mi.ReturnType; } catch (Exception ex) { throw; } var isMemberExpr = Condition.IndexOf('(') != -1; if (!isMemberExpr || this.WriteRawHtml) { base.Condition = methodExpr + "(" + Condition + ")"; } }
public static string RenderToHtml(this MarkdownPage markdownPage, Dictionary <string, object> scopeArgs) { return(RenderToString(markdownPage, scopeArgs, true)); }
public static void AddFileAndPage(this MarkdownFormat markdown, MarkdownPage markdownPage) { var pathProvider = (InMemoryVirtualPathProvider)markdown.VirtualPathProvider; pathProvider.AddFile(markdownPage.FilePath, markdownPage.Contents); markdown.AddPage(markdownPage); }
public void ReloadModifiedPageAndTemplates(MarkdownPage markdownPage) { if (markdownPage == null || !WatchForModifiedPages) 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); } }
public void AddPage(MarkdownPage page) { try { page.Prepare(); 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(page.FilePath.WithoutExtension(), page); break; } } catch (Exception ex) { Log.Error("AddViewPage() page.Prepare(): " + ex.Message, ex); } var templatePath = page.TemplatePath; if (page.TemplatePath == null) return; if (PageTemplates.ContainsKey(templatePath)) return; AddTemplate(templatePath, File.ReadAllText(templatePath)); }
private MarkdownPage ReloadIfNeeded(MarkdownPage markdownPage) { if (markdownPage == null || !WatchForModifiedPages) 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; }
public void RegisterMarkdownPage(MarkdownPage markdownPage) { AddPage(markdownPage); }
private IVirtualFile GetLatestPage(MarkdownPage markdownPage) { var file = VirtualPathProvider.GetFile(markdownPage.FilePath); return file; }
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; }
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 string RenderInTemplateIfAny(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs, string pageHtml, string templatePath=null) { MarkdownTemplate markdownTemplate = null; if (templatePath != null) PageTemplates.TryGetValue(templatePath, out markdownTemplate); var directiveTemplatePath = markdownPage.DirectiveTemplatePath; if (markdownTemplate == null && directiveTemplatePath != null) { if (!PageTemplates.TryGetValue(directiveTemplatePath, out markdownTemplate)) { var virtualFile = VirtualPathProvider.GetFile(directiveTemplatePath); if (virtualFile == null) throw new FileNotFoundException("Could not find template: " + directiveTemplatePath); var templateContents = virtualFile.ReadAllText(); markdownTemplate = AddTemplate(directiveTemplatePath, templateContents); } } if (markdownTemplate == null) { if (markdownPage.TemplatePath != null) PageTemplates.TryGetValue(markdownPage.TemplatePath, out markdownTemplate); if (markdownTemplate == null) { if (templatePath == null) return null; throw new ArgumentNullException("templatePath", templatePath); } } if (scopeArgs != null) scopeArgs[MarkdownTemplate.BodyPlaceHolder] = pageHtml; var htmlPage = markdownTemplate.RenderToString(scopeArgs); return htmlPage; }
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(page.FilePath.WithoutExtension().TrimStart(DirSeps), page); break; } }
public void Does_transform_escaped_html_start_tags() { var markdownText = @"#### Showing Results 1 - 5 ^<div id=""searchresults""> ### Markdown > [About Docs](http://path.com/to/about) ^</div> Text".NormalizeNewLines(); var expectedHtml = @"<h4>Showing Results 1 - 5</h4> <div id=""searchresults""> <h3>Markdown > <a href=""http://path.com/to/about"">About Docs</a></h3> </div> <p>Text</p> ".NormalizeNewLines(); var textBlock = new TextBlock(""); var page = new MarkdownPage { Markdown = new MarkdownFormat() }; textBlock.DoFirstRun(new PageContext(page, null, true)); var html = textBlock.TransformHtml(markdownText); Console.WriteLine(html); Assert.That(html, Is.EqualTo(expectedHtml)); }
public void AddPage(MarkdownPage page) { try { Log.InfoFormat("Compiling {0}...", page.FilePath); 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; //AddTemplate(templatePath, File.ReadAllText(templatePath)); var templateFile = VirtualPathProvider.GetFile(templatePath); var templateContents = templateFile.ReadAllText(); AddTemplate(templatePath, templateContents); } catch (Exception ex) { Log.Error("Error compiling template " + page.Template + ": " + ex.Message, ex); } }