Ejemplo n.º 1
0
        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;
		}
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
0
 private string RenderStaticPage(MarkdownPage markdownPage, bool renderHtml)
 {
     //TODO: Optimize if contains no dynamic elements
     return RenderDynamicPage(markdownPage, new Dictionary<string, object>(), renderHtml, true);
 }
Ejemplo n.º 7
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;
     }
 }
Ejemplo n.º 8
0
 public PageContext Create(MarkdownPage markdownPage, bool renderHtml)
 {
     return(new PageContext(markdownPage, ScopeArgs, renderHtml));
 }
Ejemplo n.º 9
0
 private IVirtualFile GetLatestPage(MarkdownPage markdownPage)
 {
     var file = VirtualPathProvider.GetFile(markdownPage.FilePath);
     return file;
 }
Ejemplo n.º 10
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;
 }
Ejemplo n.º 11
0
        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);
            }
        }
Ejemplo n.º 12
0
        public bool ProcessMarkdownPage(IRequest httpReq, MarkdownPage markdownPage, object dto, IResponse 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 = MimeTypes.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 static void AddFileAndPage(this MarkdownFormat markdown, MarkdownPage markdownPage)
 {
     var pathProvider = (InMemoryVirtualPathProvider)markdown.VirtualPathProvider;
     pathProvider.WriteFile(markdownPage.FilePath, markdownPage.Contents);
     markdown.AddPage(markdownPage);
 }
Ejemplo n.º 14
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(SanitizePath(page.FilePath), page);
             break;
     }
 }
Ejemplo n.º 15
0
        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 !NETCORE
            if (typePropertyName == "Html")
            {
                type = markdownPage.ExecutionContext.BaseType.HasGenericType()
                                           ? typeof(HtmlHelper <>)
                                           : typeof(HtmlHelper);
            }
#endif
            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 !NETCORE
                if (mi == null)
                {
                    mi = HtmlHelper.GetMethod(methodName);
                    if (mi == null)
                    {
                        throw new ArgumentException("Unable to resolve method '" + methodExpr + "' on type " + type.GetOperationName());
                    }
                }
#endif

                base.ReturnType = mi.ReturnType;
            }
            catch (Exception)
            {
                throw;
            }

            var isMemberExpr = Condition.IndexOf('(') != -1;
            if (!isMemberExpr || this.WriteRawHtml)
            {
                base.Condition = methodExpr + "(" + Condition + ")";
            }
        }
Ejemplo n.º 16
0
		public void RegisterMarkdownPage(MarkdownPage markdownPage)
        {
            AddPage(markdownPage);
        }
Ejemplo n.º 17
0
 public PageContext(MarkdownPage markdownPage, Dictionary <string, object> scopeArgs, bool renderHtml)
 {
     MarkdownPage = markdownPage;
     ScopeArgs    = scopeArgs ?? new Dictionary <string, object>();
     RenderHtml   = renderHtml;
 }
Ejemplo n.º 18
0
        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);
            }
        }
Ejemplo n.º 19
0
 public static string RenderToHtml(this MarkdownPage markdownPage, Dictionary <string, object> scopeArgs)
 {
     return(RenderToString(markdownPage, scopeArgs, true));
 }
Ejemplo n.º 20
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));
		}