Пример #1
0
        public void Can_Render_MarkdownPage_with_foreach()
        {
            var person = new Person
            {
                FirstName = "Demis",
                LastName = "Bellot",
                Links = new List<Link>
                {
                    new Link { Name = "ServiceStack", Href = "http://www.servicestack.net" },
                    new Link { Name = "AjaxStack", Href = "http://www.ajaxstack.net" },
                },
            };

            var markdownHtml = MarkdownFormat.Instance.Transform(dynamicListPageContent);
            Console.WriteLine(markdownHtml);

            var dynamicPage = new MarkdownPage(
                dynamicListPagePath, "DynamicListTpl", dynamicListPageContent);

            dynamicPage.Prepare();

            Assert.That(dynamicPage.Blocks.Count, Is.EqualTo(9));

            var expectedHtml = markdownHtml
                .Replace("@model.FirstName", person.FirstName)
                .Replace("@model.LastName", person.LastName);

            var templateArgs = new Dictionary<string, object> { { "model", person } };
            var templateOutput = dynamicPage.RenderToString(templateArgs);

            Console.WriteLine("Template Output: " + templateOutput);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Пример #2
0
        public void Can_Render_MarkdownPage()
        {
            var person = new Person { FirstName = "Demis", LastName = "Bellot" };

            var dynamicPage = new MarkdownPage(dynamicPageContent, "DynamicTpl", dynamicListPageContent);
            dynamicPage.Prepare();

            Assert.That(dynamicPage.Blocks.Count, Is.EqualTo(9));

            var expectedHtml = MarkdownFormat.Instance.Transform(dynamicListPageContent)
                .Replace("@model.FirstName", person.FirstName)
                .Replace("@model.LastName", person.LastName);

            var templateArgs = new Dictionary<string, object> { { "model", person } };
            var templateOutput = dynamicPage.RenderToString(templateArgs);

            Console.WriteLine("Template Output: " + templateOutput);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
Пример #3
0
 private string RenderStaticPage(MarkdownPage markdownPage, bool renderHtml)
 {
     //TODO: Optimize if contains no dynamic elements
     return RenderDynamicPage(markdownPage, new Dictionary<string, object>(), renderHtml, true);
 }
Пример #4
0
        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;
        }
Пример #5
0
        private string RenderDynamicPage(MarkdownPage markdownPage, string pageName, object model, bool renderHtml, bool renderTemplate)
        {
            if (markdownPage == null)
                throw new InvalidDataException(ErrorPageNotFound.FormatWith(pageName));

            var scopeArgs = new Dictionary<string, object> { { MarkdownPage.ModelName, model } };

            return RenderDynamicPage(markdownPage, scopeArgs, renderHtml, renderTemplate);
        }
Пример #6
0
        public string RenderDynamicPage(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs,
            bool renderHtml, bool renderTemplate)
        {
            scopeArgs = scopeArgs ?? new Dictionary<string, object>();
            var htmlPage = markdownPage.RenderToString(scopeArgs, renderHtml);
            if (!renderTemplate) return htmlPage;

            var html = RenderInTemplateIfAny(
                markdownPage, scopeArgs, htmlPage);

            return html;
        }
Пример #7
0
        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);
            }
        }
Пример #8
0
 public PageContext(MarkdownPage markdownPage, Dictionary <string, object> scopeArgs, bool renderHtml)
 {
     MarkdownPage = markdownPage;
     ScopeArgs    = scopeArgs ?? new Dictionary <string, object>();
     RenderHtml   = renderHtml;
 }
Пример #9
0
        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));
        }
Пример #10
0
        private string RenderStaticPage(MarkdownPage markdownPage, bool renderHtml)
        {
            var pageHtml = Transform(markdownPage.Contents, renderHtml);

            return RenderInTemplateIfAny(markdownPage, null, pageHtml);
        }
Пример #11
0
        private string RenderStaticPage(MarkdownPage markdownPage)
        {
            var pageHtml = markdown.Transform(markdownPage.Contents);
            var templatePath = markdownPage.GetTemplatePath();

            return RenderInTemplateIfAny(templatePath, pageHtml);
        }
Пример #12
0
        private void AddPage(MarkdownPage page)
        {
            try
            {
                page.Prepare();
                Pages.Add(page.Name, page);
            }
            catch (Exception ex)
            {
                log.Error("AddPage() page.Prepare(): " + ex.Message, ex);
            }

            var templatePath = page.GetTemplatePath();

            if (!PageTemplates.ContainsKey(templatePath))
            {
                var templateFile = new FileInfo(templatePath);

                if (!templateFile.Exists)
                {
                    PageTemplates.Add(templateFile.FullName, null);
                    return;
                }

                var pageContents = File.ReadAllText(templatePath);
                var template = new MarkdownTemplate(
                    templatePath, templateFile.Name, pageContents);

                try
                {
                    template.Prepare();
                    PageTemplates.Add(template.FilePath, template);
                }
                catch (Exception ex)
                {
                    log.Error("AddPage() template.Prepare(): " + ex.Message, ex);
                }
            }
        }
Пример #13
0
        private string RenderDynamicPage(MarkdownPage markdownPage, object model, bool renderHtml, bool renderTemplate)
        {
            if (markdownPage == null)
                throw new InvalidDataException(ErrorPageNotFound.FormatWith(markdownPage.Name));

            var scopeArgs = new Dictionary<string, object> { { MarkdownPage.ModelName, model } };

            var htmlPage = markdownPage.RenderToString(scopeArgs, renderHtml);
            if (!renderTemplate) return htmlPage;

            var html = RenderInTemplateIfAny(markdownPage.TemplatePath, htmlPage);

            return html;
        }
Пример #14
0
        private 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;

            var templateName = Path.GetFileName(templatePath).WithoutExtension();
            var pageContents = File.ReadAllText(templatePath);
            var template = new MarkdownTemplate(templatePath, templateName, pageContents);

            PageTemplates.Add(templatePath, template);

            try
            {
                template.Prepare();
                PageTemplates.Add(template.FilePath, template);
            }
            catch (Exception ex)
            {
                Log.Error("AddViewPage() template.Prepare(): " + ex.Message, ex);
            }
        }
Пример #15
0
 public static string RenderToHtml(this MarkdownPage markdownPage, Dictionary <string, object> scopeArgs)
 {
     return(RenderToString(markdownPage, scopeArgs, true));
 }
Пример #16
0
        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 markup = RenderDynamicPage(markdownPage, markdownPage.Name, dto, renderHtml, renderInTemplate);
            httpRes.Write(markup);

            return true;
        }
Пример #17
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 (typePropertyName == "Html")
            {
                type = Common.ReflectionExtensions.IsGenericType(markdownPage.ExecutionContext.BaseType)
                                           ? 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));
            }

            var mi = 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;

            var isMemberExpr = Condition.IndexOf('(') != -1;

            if (!isMemberExpr || this.WriteRawHtml)
            {
                base.Condition = methodExpr + "(" + Condition + ")";
            }
        }
Пример #18
0
 public void RegisterMarkdownPage(MarkdownPage markdownPage)
 {
     AddPage(markdownPage);
 }
Пример #19
0
 public PageContext Create(MarkdownPage markdownPage, bool renderHtml)
 {
     return(new PageContext(markdownPage, ScopeArgs, renderHtml));
 }
Пример #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));
        }