Ejemplo n.º 1
0
        public void RenderHeadings()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.AppendLine("# heading 1");
            markdown.AppendLine("## heading 2");
            markdown.AppendLine("### heading 3");
            markdown.AppendLine("Heading");
            markdown.AppendLine("=======");
            markdown.AppendLine("Heading");
            markdown.AppendLine("-------");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<h1>heading 1</h1>");
            expectedHtml.Append("<h2>heading 2</h2>");
            expectedHtml.Append("<h3>heading 3</h3>");
            expectedHtml.Append("<h1>Heading</h1>");
            expectedHtml.Append("<h2>Heading</h2>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 2
0
        public void RenderCodeblocksWithSyntaxName()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.AppendLine("```cs");
            markdown.AppendLine("string name = \"test\";");
            markdown.AppendLine("```");

            var expectedHtml = new StringBuilder();

            // it looks like the syntax name is prefixed with 'lang' by convention
            expectedHtml.Append("<pre><code class=\"lang-cs\">");
            expectedHtml.Append("string name = &quot;test&quot;;\n");
            expectedHtml.Append("</code></pre>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            var document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document);

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
	    public override Article Parse(string fileName, string content)
	    {
			var parser = new MarkdownParser();
			string t = RemoveMetaData(content);
			var document = parser.Parse(t);
			var renderer = new MarkdownHtmlRenderer();
	
	        var article = new Article
	        {
	            FullFileName = fileName,
	            Content = renderer.Render(document),
				MetaData = ParseMetaData(content)
	        };
	
			return article;
	    }
Ejemplo n.º 4
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                ShowHelp();
            }

            var fileName = args[0];

            if (!File.Exists(fileName))
            {
                ShowHelp();
                return;
            }

            var content = File.ReadAllText(fileName);

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            try
            {
                Console.WriteLine("Starting parsing of {0}", fileName);
                var stopwatch = Stopwatch.StartNew();
                var document = parser.Parse(content);
                stopwatch.Stop();
                Console.WriteLine("Finished parsing in {0} seconds", stopwatch.Elapsed.TotalSeconds);

                stopwatch.Reset();
                stopwatch.Start();
                var html = renderer.Render(document);
                stopwatch.Stop();
                Console.WriteLine("Finished rendering in {0} seconds", stopwatch.Elapsed.TotalSeconds);

                var outputFile = Path.GetFileNameWithoutExtension(fileName) + ".html";
                Console.WriteLine("Writing output to {0}", outputFile);
                File.WriteAllText(outputFile, html);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.ToString());
            }

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        public void RenderCodeblocks()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.Append("```\n");
            markdown.Append("string name = \"test\";\n");
            markdown.Append("```\n");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<pre><code>");
            expectedHtml.Append("string name = &quot;test&quot;;\n");
            expectedHtml.Append("</code></pre>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 6
0
        public void RenderOrderedLists()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.AppendLine("1. item");
            markdown.AppendLine("2. item");
            markdown.AppendLine("   this is item two");
            markdown.AppendLine("3. item");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<ol>");
            expectedHtml.Append("<li><p>item</p></li>");
            expectedHtml.Append("<li><p>item this is item two</p></li>");
            expectedHtml.Append("<li><p>item</p></li>");
            expectedHtml.Append("</ol>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document);

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 7
0
        public void NoInlineHtmlPreventsInlineHtml()
        {
            var markdown = new StringBuilder();
            markdown.AppendLine("Just a paragraph <img src=x />");
            markdown.AppendLine();
            markdown.AppendLine("* item 1 <img src=x />");
            markdown.AppendLine("* item 2 <img src=x />");
            markdown.AppendLine();
            markdown.AppendLine("1. item 1 <img src=x />");
            markdown.AppendLine("1. item 2 <img src=x />");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();
            // act
            Document document = parser.Parse(markdown.ToString());

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<p>").Append("Just a paragraph <img src=x />").Append("</p>");
            expectedHtml.Append("<ul>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 1 <img src=x />").Append("</p>").Append("</li>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 2 <img src=x />").Append("</p>").Append("</li>");
            expectedHtml.Append("</ul>");
            expectedHtml.Append("<ol>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 1 <img src=x />").Append("</p>").Append("</li>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 2 <img src=x />").Append("</p>").Append("</li>");
            expectedHtml.Append("</ol>");

            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());

            expectedHtml = new StringBuilder();
            expectedHtml.Append("<p>").Append("Just a paragraph &lt;img src=x /&gt;").Append("</p>");
            expectedHtml.Append("<ul>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 1 &lt;img src=x /&gt;").Append("</p>").Append("</li>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 2 &lt;img src=x /&gt;").Append("</p>").Append("</li>");
            expectedHtml.Append("</ul>");
            expectedHtml.Append("<ol>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 1 &lt;img src=x /&gt;").Append("</p>").Append("</li>");
            expectedHtml.Append("<li>").Append("<p>").Append("item 2 &lt;img src=x /&gt;").Append("</p>").Append("</li>");
            expectedHtml.Append("</ol>");

            var noInlineRenderer = new MarkdownHtmlRenderer(HtmlRendererOptions.NoInlineHtml);
            document = parser.Parse(markdown.ToString());
            string noInlineHtml = noInlineRenderer.Render(document).Replace("\r\n", "");

            // assert
            noInlineHtml.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 8
0
        public void ThrowExceptionOnRenderingError()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.AppendLine("Title");
            markdown.AppendLine("======");
            markdown.AppendLine();

            markdown.Append("1234567890");

            var parser = new MarkdownParser();

            var renderer = new MarkdownHtmlRenderer();
            renderer.Options.Renderers.Insert(
                0,
                new ThrowErrorOnBlockRenderer(typeof(Paragraph)));

            // act
            Document document = parser.Parse(markdown.ToString());
            
            var exception = Assert.Throws<RenderingException>(
                ()=>renderer.Render(document));

            // assert
            exception.Block.Should().BeOfType<Paragraph>();
            exception.Renderer.Should().BeOfType<ThrowErrorOnBlockRenderer>();
            exception.InnerException.Should().BeOfType<ArgumentNullException>();
        }
Ejemplo n.º 9
0
        public void RenderEmpty()
        {
            // arrange
            var markdown = new StringBuilder();
            var expectedHtml = new StringBuilder();
         
            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document);

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 10
0
        public void RenderParagraphWithOnlyStrongEmphasis()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.Append("**text**");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<p>");
            expectedHtml.Append("<strong>text</strong>");
            expectedHtml.Append("</p>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 11
0
        public void FixParagraphDisappears()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.Append("#### Paragraphs\n");
            markdown.Append("\n");
            markdown.Append("Paragraphs are lines of text followed by empty line.\n");
            markdown.Append("\n");
            markdown.Append("Second paragraph\n");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<h4>Paragraphs</h4>");
            expectedHtml.Append("<p>");
            expectedHtml.Append("Paragraphs are lines of text followed by empty line.");
            expectedHtml.Append("</p>");
            expectedHtml.Append("<p>");
            expectedHtml.Append("Second paragraph");
            expectedHtml.Append("</p>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }
Ejemplo n.º 12
0
        public void RenderParagraph()
        {
            // arrange
            var markdown = new StringBuilder();
            markdown.AppendLine("some text here");
            markdown.AppendLine("a link here [here](http://www.123.com)");
            markdown.AppendLine("a inline image here ![alt text](/images/sample.jpg)");
            markdown.AppendLine("emphasis of *text* or strong emphasis of **text**");
            markdown.AppendLine("inline code block `var test = 123;` should be supported");

            var expectedHtml = new StringBuilder();
            expectedHtml.Append("<p>");
            expectedHtml.Append("some text here ");
            expectedHtml.Append("a link here <a href=\"http://www.123.com\">here</a> ");
            expectedHtml.Append("a inline image here <img src=\"/images/sample.jpg\" alt=\"alt text\" /> ");
            expectedHtml.Append("emphasis of <em>text</em> or strong emphasis of <strong>text</strong> ");
            expectedHtml.Append("inline code block <code>var test = 123;</code> should be supported");
            expectedHtml.Append("</p>");

            var parser = new MarkdownParser();
            var renderer = new MarkdownHtmlRenderer();

            // act
            Document document = parser.Parse(markdown.ToString());
            string html = renderer.Render(document).Replace("\r\n", "");

            // assert
            html.ShouldBeEquivalentTo(expectedHtml.ToString());
        }