Exemple #1
0
        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));
        }
Exemple #2
0
        /// <summary>Initialises this object.</summary>
        ///
        /// <param name="markdownPage">The markdown page.</param>
        /// <param name="scopeArgs">   The scope arguments.</param>
        /// <param name="renderHtml">  true to render HTML.</param>
        /// <param name="viewData">    Information describing the view.</param>
        /// <param name="htmlHelper">  The HTML helper.</param>
        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;
		}
 /// <summary>A MarkdownFormat extension method that adds a file and page to 'markdownPage'.</summary>
 ///
 /// <param name="markdown">    The markdown to act on.</param>
 /// <param name="markdownPage">The markdown page.</param>
 public static void AddFileAndPage(this MarkdownFormat markdown, MarkdownPage markdownPage)
 {
     var pathProvider = (InMemoryVirtualPathProvider)markdown.VirtualPathProvider;
     pathProvider.AddFile(markdownPage.FilePath, markdownPage.Contents);
     markdown.AddPage(markdownPage);
 }
Exemple #4
0
        /// <summary>Renders the dynamic page.</summary>
        ///
        /// <param name="markdownPage">  The markdown page.</param>
        /// <param name="scopeArgs">     The scope arguments.</param>
        /// <param name="renderHtml">    true to render HTML.</param>
        /// <param name="renderTemplate">true to render template.</param>
        /// <param name="templatePath">  Full pathname of the template file.</param>
        ///
        /// <returns>A string.</returns>
        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;
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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;
        }
 /// <summary>
 /// Creates the specified markdown page.
 /// </summary>
 /// <param name="markdownPage">The markdown page.</param>
 /// <param name="renderHtml">if set to <c>true</c> [render HTML].</param>
 /// <returns></returns>
 public PageContext Create(MarkdownPage markdownPage, bool renderHtml)
 {
     return(new PageContext(markdownPage, ScopeArgs, renderHtml));
 }
Exemple #8
0
		public void Does_transform_escaped_html_start_tags()
		{
			var markdownText =
			@"#### Showing Results 1 - 5

^<div id=""searchresults"">

### Markdown &gt; [About Docs](http://path.com/to/about)

^</div>

Text".NormalizeNewLines();

			var expectedHtml =
			@"<h4>Showing Results 1 - 5</h4>
<div id=""searchresults"">
<h3>Markdown &gt; <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));
		}
Exemple #9
0
        /// <summary>Adds a page.</summary>
        ///
        /// <param name="page">The page.</param>
        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);
            }
        }
Exemple #10
0
        /// <summary>Registers the markdown page described by markdownPage.</summary>
        ///
        /// <param name="markdownPage">The markdown page.</param>
		public void RegisterMarkdownPage(MarkdownPage markdownPage)
        {
            AddPage(markdownPage);
        }
Exemple #11
0
 private IVirtualFile GetLatestPage(MarkdownPage markdownPage)
 {
     var file = VirtualPathProvider.GetFile(markdownPage.FilePath);
     return file;
 }
Exemple #12
0
 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;
 }
Exemple #13
0
        /// <summary>Reload modified page and templates.</summary>
        ///
        /// <param name="markdownPage">The markdown page.</param>
        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);
            }
        }
Exemple #14
0
        /// <summary>Process the markdown page.</summary>
        ///
        /// <param name="httpReq">     The HTTP request.</param>
        /// <param name="markdownPage">The markdown page.</param>
        /// <param name="dto">         The dto.</param>
        /// <param name="httpRes">     The HTTP resource.</param>
        ///
        /// <returns>true if it succeeds, false if it fails.</returns>
        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;
        }
 /// <summary>
 /// Renders to HTML.
 /// </summary>
 /// <param name="markdownPage">The markdown page.</param>
 /// <param name="scopeArgs">The scope arguments.</param>
 /// <returns></returns>
 public static string RenderToHtml(this MarkdownPage markdownPage, Dictionary <string, object> scopeArgs)
 {
     return(RenderToString(markdownPage, scopeArgs, true));
 }
Exemple #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageContext"/> class.
 /// </summary>
 /// <param name="markdownPage">The markdown page.</param>
 /// <param name="scopeArgs">The scope arguments.</param>
 /// <param name="renderHtml">if set to <c>true</c> [render HTML].</param>
 public PageContext(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs, bool renderHtml)
 {
     MarkdownPage = markdownPage;
     ScopeArgs = scopeArgs ?? new Dictionary<string, object>();
     RenderHtml = renderHtml;
 }
Exemple #17
0
 /// <summary>
 /// Creates the specified markdown page.
 /// </summary>
 /// <param name="markdownPage">The markdown page.</param>
 /// <param name="renderHtml">if set to <c>true</c> [render HTML].</param>
 /// <returns></returns>
 public PageContext Create(MarkdownPage markdownPage, bool renderHtml)
 {
     return new PageContext(markdownPage, ScopeArgs, renderHtml);
 }
Exemple #18
0
 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;
     }
 }
Exemple #19
0
 private string RenderStaticPage(MarkdownPage markdownPage, bool renderHtml)
 {
     //TODO: Optimize if contains no dynamic elements
     return RenderDynamicPage(markdownPage, new Dictionary<string, object>(), renderHtml, true);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PageContext"/> class.
 /// </summary>
 /// <param name="markdownPage">The markdown page.</param>
 /// <param name="scopeArgs">The scope arguments.</param>
 /// <param name="renderHtml">if set to <c>true</c> [render HTML].</param>
 public PageContext(MarkdownPage markdownPage, Dictionary <string, object> scopeArgs, bool renderHtml)
 {
     MarkdownPage = markdownPage;
     ScopeArgs    = scopeArgs ?? new Dictionary <string, object>();
     RenderHtml   = renderHtml;
 }