public static void AddFileAndPage(this MarkdownFormat markdown, MarkdownPage markdownPage)
        {
            var pathProvider = (InMemoryVirtualPathProvider)markdown.VirtualPathProvider;

            pathProvider.WriteFile(markdownPage.FilePath, markdownPage.Contents);
            markdown.AddPage(markdownPage);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        /// <summary>Creates a new MarkdownFormat.</summary>
        ///
        /// <param name="pageTemplate">The page template.</param>
        ///
        /// <returns>A MarkdownFormat.</returns>
        public MarkdownFormat Create(string pageTemplate)
        {
            var markdownFormat = new MarkdownFormat();

            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, "/path/to/tpl", PageName, pageTemplate));

            return(markdownFormat);
        }
        public MarkdownFormat Create(string websiteTemplate, string pageTemplate)
        {
            var markdownFormat = new MarkdownFormat();

            markdownFormat.AddTemplate("/path/to/websitetpl", websiteTemplate);
            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, "/path/to/tpl", PageName, pageTemplate)
            {
                TemplatePath = "/path/to/websitetpl",
            });

            return(markdownFormat);
        }
Beispiel #5
0
        /// <summary>Creates a new MarkdownFormat.</summary>
        ///
        /// <param name="websiteTemplate">The website template.</param>
        /// <param name="pageTemplate">   The page template.</param>
        ///
        /// <returns>A MarkdownFormat.</returns>
        public MarkdownFormat Create(string websiteTemplate, string pageTemplate)
        {
            var markdownFormat = new MarkdownFormat {
                VirtualPathProvider = new InMemoryVirtualPathProvider(new BasicAppHost())
            };

            markdownFormat.AddFileAndTemplate("websiteTemplate", websiteTemplate);
            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, "/path/to/tpl", PageName, pageTemplate)
            {
                Template = "websiteTemplate",
            });

            return(markdownFormat);
        }
        public string Parse <TModel>(string markdownTemplate, TModel model, bool isHtml = true)
        {
            if (string.IsNullOrWhiteSpace(markdownTemplate))
            {
                return(string.Empty);
            }

            var file = Path.GetTempFileName();

            lock (SyncLock)
            {
                File.WriteAllText(file, markdownTemplate);
            }

            var fi = new FileInfo(file);

            if (fi.Length == 0)
            {
                fi.Delete();
                return(string.Empty);
            }

            var mdFormat = new MarkdownFormat();
            var mdPage   = new MarkdownPage(mdFormat, file, "_markdown_", markdownTemplate);

            mdFormat.AddPage(mdPage);

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

            var result = mdFormat.RenderDynamicPage(mdPage, scopeArgs, isHtml, false);

            // clean up temp file
            fi.Delete();

            return(result);
        }
        public void Layout_MasterPage_Scenarios_Adding_Sections()
        {
            var websiteTemplate =
                @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""left-menu"">
            <!--@Menu-->
        </div>
        
        <div id=""body"">
            <!--@Body-->
        </div>
        
        <div id=""footer"">
            <!--@Footer-->
        </div>
    
    </body>
</html>".NormalizeNewLines();

            var pageTemplate =
                @"@Layout websiteTemplate

# About this Site

This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.

And obviously I can have code in here too. Here is the
current date/year: @DateTime.Now.Year

@section Menu {
  - About Item 1
  - About Item 2
}

@section Footer {
This is my custom footer for Home
}
".NormalizeNewLines();

            var expectedHtml = @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""left-menu"">
            <ul>
<li>About Item 1</li>
<li>About Item 2</li>
</ul>

        </div>
        
        <div id=""body"">
            <h1>About this Site</h1>
<p>This is some content that will make up the &quot;about&quot; 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>
<p>And obviously I can have code in here too. Here is the
current date/year: 2021</p>

        </div>
        
        <div id=""footer"">
            <p>This is my custom footer for Home</p>

        </div>
    
    </body>
</html>".NormalizeNewLines();


            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, @"C:\path\to\page-tpl", PageName, pageTemplate));

            markdownFormat.AddFileAndTemplate(@"websiteTemplate", websiteTemplate);

            var html = markdownFormat.RenderDynamicPageHtml(PageName);

            Console.WriteLine(html);
            Assert.That(html, Is.EqualTo(expectedHtml));
        }
Beispiel #8
0
        public void Can_Render_Template_with_section_and_variable_placeholders()
        {
            var template = @"## Welcome to Razor!

@var lastName = Model.LastName;

Hello @Upper(lastName), @Model.FirstName,

@section Breadcrumbs {
### Breadcrumbs
@Combine("" / "", Model.FirstName, lastName)
}

@section Menus {
### Menus
@foreach (var link in Model.Links) {
  - @link.Name - @link.Href
  @var labels = link.Labels
  @foreach (var label in labels) { 
    - @label
  }
}
}";

            var websiteTemplate = @"<!doctype html>
<html lang=""en-us"">
<head>
    <title><!--@lastName--> page</title>
</head>
<body>

    <header>
        <!--@Menus-->
    </header>

    <h1>Website Template</h1>

    <div id=""content""><!--@Body--></div>

    <footer>
        <!--@Breadcrumbs-->
    </footer>

</body>
</html>";

            var expectedHtml = @"<!doctype html>
<html lang=""en-us"">
<head>
    <title>Bellot page</title>
</head>
<body>

    <header>
        <h3>Menus</h3>

<ul>
<li>ServiceStack - http://www.servicestack.net
<ul>
<li>REST</li>
<li>JSON</li>
<li>XML</li>
</ul></li>
<li>AjaxStack - http://www.ajaxstack.com
<ul>
<li>HTML5</li>
<li>AJAX</li>
<li>SPA</li>
</ul></li>
</ul>

    </header>

    <h1>Website Template</h1>

    <div id=""content""><h2>Welcome to Razor!</h2>


<p>Hello  BELLOT, Demis,</p>


</div>

    <footer>
        <h3>Breadcrumbs</h3>

<p>Demis / Bellot</p>

    </footer>

</body>
</html>".Replace("\r\n", "\n");

            markdownFormat.AddTemplate("/path/to/tpl", websiteTemplate);

            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, "/path/to/page-tpl", "DynamicModelTpl", template)
            {
                TemplatePath = "/path/to/tpl"
            });

            var templateOutput = markdownFormat.RenderDynamicPageHtml("DynamicModelTpl", person);

            templateOutput = templateOutput.Replace("\r\n", "\n");

            Console.WriteLine(templateOutput);

            Assert.That(templateOutput, Is.EqualTo(expectedHtml));
        }
        public void Simple_Layout_Example()
        {
            var websiteTemplate =
                @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""body"">
            <!--@Body-->
        </div>
    
    </body>
</html>".NormalizeNewLines();

            var pageTemplate =
                @"@Layout ~/websiteTemplate

# About this Site

This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.

And obviously I can have code in here too. Here is the
current date/year: @DateTime.Now.Year
";

            var expectedHtml = @"<!DOCTYPE html>
<html>
    <head>
        <title>Simple Site</title>
    </head>
    <body>
    
        <div id=""header"">
            <a href=""/"">Home</a>
            <a href=""/About"">About</a>
        </div>
        
        <div id=""body"">
            
<h1>About this Site</h1>

<p>This is some content that will make up the ""about"" 
page of our web-site. We'll use this in conjunction
with a layout template. The content you are seeing here
comes from ^^^websiteTemplate.</p>

<p>And obviously I can have code in here too. Here is the
current date/year: 2011</p>

        </div>
    
    </body>
</html>".NormalizeNewLines();


            var markdownFormat = new MarkdownFormat();

            markdownFormat.AddPage(
                new MarkdownPage(markdownFormat, @"C:\path\to\page-tpl", PageName, pageTemplate));

            markdownFormat.AddTemplate(@"C:\path\to\websiteTemplate", websiteTemplate);

            var html = markdownFormat.RenderDynamicPageHtml(PageName);

            Console.WriteLine(html);
            Assert.That(html, Is.EqualTo(expectedHtml));
        }