diff --git a/src/Markdig.Tests/Markdig.Tests.csproj b/src/Markdig.Tests/Markdig.Tests.csproj
index a506836a..d792a17c 100644
--- a/src/Markdig.Tests/Markdig.Tests.csproj
+++ b/src/Markdig.Tests/Markdig.Tests.csproj
@@ -61,6 +61,7 @@
+
diff --git a/src/Markdig.Tests/TestNormalize.cs b/src/Markdig.Tests/TestNormalize.cs
new file mode 100644
index 00000000..f4802479
--- /dev/null
+++ b/src/Markdig.Tests/TestNormalize.cs
@@ -0,0 +1,339 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+
+using System;
+using NUnit.Framework;
+using Markdig.Syntax;
+using Markdig.Syntax.Inlines;
+using System.IO;
+using Markdig.Renderers.Normalize;
+using Markdig.Helpers;
+
+namespace Markdig.Tests
+{
+ [TestFixture]
+ public class TestNormalize
+ {
+ [Test]
+ public void TestNormalizeSyntaxCodeBlock()
+ {
+ AssertSyntax("````csharp\npublic void HelloWorld()\n{\n}\n````", new FencedCodeBlock(null)
+ {
+ FencedChar = '`',
+ FencedCharCount = 4,
+ Info = "csharp",
+ Lines = new StringLineGroup(4)
+ {
+ new StringSlice("public void HelloWorld()"),
+ new StringSlice("{"),
+ new StringSlice("}"),
+ }
+ });
+
+ AssertSyntax(" public void HelloWorld()\n {\n }", new CodeBlock(null)
+ {
+ Lines = new StringLineGroup(4)
+ {
+ new StringSlice("public void HelloWorld()"),
+ new StringSlice("{"),
+ new StringSlice("}"),
+ }
+ });
+ }
+
+ [Test]
+ public void TestNormalizeSyntaxHeadline()
+ {
+ AssertSyntax("## Headline", new HeadingBlock(null)
+ {
+ HeaderChar = '#',
+ Level = 2,
+ Inline = new ContainerInline().AppendChild(new LiteralInline("Headline")),
+ });
+ }
+
+ [Test]
+ public void TestNormalizeSyntaxParagraph()
+ {
+ AssertSyntax("This is a normal paragraph", new ParagraphBlock()
+ {
+ Inline = new ContainerInline()
+ .AppendChild(new LiteralInline("This is a normal paragraph")),
+ });
+
+ AssertSyntax("This is a\nnormal\nparagraph", new ParagraphBlock()
+ {
+ Inline = new ContainerInline()
+ .AppendChild(new LiteralInline("This is a"))
+ .AppendChild(new LineBreakInline())
+ .AppendChild(new LiteralInline("normal"))
+ .AppendChild(new LineBreakInline())
+ .AppendChild(new LiteralInline("paragraph")),
+ });
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripCodeBlock()
+ {
+ AssertNormalizeNoTrim(" public void HelloWorld();\n {\n }");
+ AssertNormalizeNoTrim(" public void HelloWorld();\n {\n }\n\ntext after two newlines");
+ AssertNormalizeNoTrim("````\npublic void HelloWorld();\n{\n}\n````\n\ntext after two newlines");
+ AssertNormalizeNoTrim("````csharp\npublic void HelloWorld();\n{\n}\n````");
+ AssertNormalizeNoTrim("````csharp hideNewKeyword=true\npublic void HelloWorld();\n{\n}\n````");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripHeading()
+ {
+ AssertNormalizeNoTrim("# Heading");
+ AssertNormalizeNoTrim("## Heading");
+ AssertNormalizeNoTrim("### Heading");
+ AssertNormalizeNoTrim("#### Heading");
+ AssertNormalizeNoTrim("##### Heading");
+ AssertNormalizeNoTrim("###### Heading");
+ AssertNormalizeNoTrim("###### Heading\n\ntext after two newlines");
+
+ AssertNormalizeNoTrim("Heading\n=======\n\ntext after two newlines", "# Heading\n\ntext after two newlines");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripHtml()
+ {
+ /*AssertNormalizeNoTrim(@"
+ div >");*/ // TODO: Bug: Throws Exception during emit
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripParagraph()
+ {
+ AssertNormalizeNoTrim("This is a plain paragraph");
+ AssertNormalizeNoTrim(@"This
+is
+a
+plain
+paragraph");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripParagraphMulti()
+ {
+ AssertNormalizeNoTrim(@"line1
+
+line2
+
+line3");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripListUnordered()
+ {
+ AssertNormalizeNoTrim(@"- a
+- b
+- c");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripListOrdered()
+ {
+ AssertNormalizeNoTrim(@"1. a
+2. b
+3. c");
+ }
+
+
+ [Test]
+ public void TestNormalizeRoundtripListOrderedAndIntended()
+ {
+ AssertNormalizeNoTrim(@"1. a
+2. b
+ - foo
+ - bar
+ a) 1234
+ b) 1324
+3. c
+4. c
+5. c
+6. c
+7. c
+8. c
+9. c
+10. c
+ - Foo
+ - Bar
+11. c
+12. c");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripHeaderAndParagraph()
+ {
+ AssertNormalizeNoTrim(@"# heading
+
+paragraph
+
+paragraph2 without newlines");
+ }
+
+
+ [Test]
+ public void TestNormalizeRoundtripQuoteBlock()
+ {
+ AssertNormalizeNoTrim(@"> test1
+>
+> test2");
+
+ AssertNormalizeNoTrim(@"> test1
+> -foobar
+
+asdf
+
+> test2
+> -foobar sen.");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripThematicBreak()
+ {
+ AssertNormalizeNoTrim("***\n");
+
+ AssertNormalizeNoTrim("* * *\n", "***\n");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripAutolinkInline()
+ {
+ AssertNormalizeNoTrim("This has a
");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripCodeInline()
+ {
+ AssertNormalizeNoTrim("This has a `HelloWorld()` in it");
+ AssertNormalizeNoTrim(@"This has a ``Hello`World()`` in it");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripEmphasisInline()
+ {
+ AssertNormalizeNoTrim("This is a plain **paragraph**");
+ AssertNormalizeNoTrim("This is a plain *paragraph*");
+ AssertNormalizeNoTrim("This is a plain _paragraph_");
+ AssertNormalizeNoTrim("This is a plain __paragraph__");
+ AssertNormalizeNoTrim("This is a pl*ai*n **paragraph**");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripLineBreakInline()
+ {
+ AssertNormalizeNoTrim("normal\nline break");
+ AssertNormalizeNoTrim("hard \nline break");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripLinkInline()
+ {
+ AssertNormalizeNoTrim("This is a [link](http://company.com)");
+ AssertNormalizeNoTrim("This is an ");
+
+ AssertNormalizeNoTrim(@"This is a [link](http://company.com ""Crazy Company"")");
+ AssertNormalizeNoTrim(@"This is a [link](http://company.com ""Crazy \"" Company"")");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripHtmlEntityInline()
+ {
+ AssertNormalizeNoTrim("This is a ä blank");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripHtmlInline()
+ {
+ AssertNormalizeNoTrim("foo
bar");
+ AssertNormalizeNoTrim(@"foo
bar");
+ }
+
+
+ [Test]
+ public void TestNormalizeRoundtripSpaceBetweenNodes()
+ {
+ AssertNormalizeNoTrim("# Hello World\nFoobar is a better bar.",
+ "# Hello World\n\nFoobar is a better bar.");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripSpaceBetweenNodesEvenForHeadlines()
+ {
+ AssertNormalizeNoTrim("# Hello World\n## Chapter 1\nFoobar is a better bar.",
+ "# Hello World\n\n## Chapter 1\n\nFoobar is a better bar.");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripSpaceRemoveAtStartAndEnd()
+ {
+ AssertNormalizeNoTrim("\n\n# Hello World\n## Chapter 1\nFoobar is a better bar.\n\n",
+ "# Hello World\n\n## Chapter 1\n\nFoobar is a better bar.");
+ }
+
+ [Test]
+ public void TestNormalizeRoundtripSpaceShortenBetweenNodes()
+ {
+ AssertNormalizeNoTrim("# Hello World\n\n\n\nFoobar is a better bar.",
+ "# Hello World\n\nFoobar is a better bar.");
+ }
+
+ private static void AssertSyntax(string expected, MarkdownObject syntax)
+ {
+ var writer = new StringWriter();
+ var normalizer = new NormalizeRenderer(writer);
+ var document = new MarkdownDocument();
+ if (syntax is Block)
+ {
+ document.Add(syntax as Block);
+ }
+ else
+ {
+ throw new InvalidOperationException();
+ }
+ normalizer.Render(document);
+
+ var actual = writer.ToString();
+
+ Assert.AreEqual(expected, actual);
+ }
+
+ public void AssertNormalizeNoTrim(string input, string expected = null)
+ => AssertNormalize(input, expected, false);
+
+ public void AssertNormalize(string input, string expected = null, bool trim = true)
+ {
+ expected = expected ?? input;
+ input = NormText(input, trim);
+ expected = NormText(expected, trim);
+
+ var result = Markdown.Normalize(input);
+ result = NormText(result, trim);
+
+ Console.WriteLine("```````````````````Source");
+ Console.WriteLine(TestParser.DisplaySpaceAndTabs(input));
+ Console.WriteLine("```````````````````Result");
+ Console.WriteLine(TestParser.DisplaySpaceAndTabs(result));
+ Console.WriteLine("```````````````````Expected");
+ Console.WriteLine(TestParser.DisplaySpaceAndTabs(expected));
+ Console.WriteLine("```````````````````");
+ Console.WriteLine();
+
+ TextAssert.AreEqual(expected, result);
+ }
+
+ private static string NormText(string text, bool trim)
+ {
+ if (trim)
+ {
+ text = text.Trim();
+ }
+ return text.Replace("\r\n", "\n").Replace('\r', '\n');
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Markdown.cs b/src/Markdig/Markdown.cs
index 61cc8f32..406a7557 100644
--- a/src/Markdig/Markdown.cs
+++ b/src/Markdig/Markdown.cs
@@ -7,6 +7,7 @@ using System.Reflection;
using Markdig.Extensions.SelfPipeline;
using Markdig.Parsers;
using Markdig.Renderers;
+using Markdig.Renderers.Normalize;
using Markdig.Syntax;
namespace Markdig
@@ -22,6 +23,42 @@ namespace Markdig
public static readonly string Version = ((AssemblyFileVersionAttribute) typeof(Markdown).Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)[0]).Version;
#endif
+ ///
+ /// Normalizes the specified markdown to a normalized markdown text.
+ ///
+ /// The markdown.
+ /// The pipeline.
+ /// A normalized markdown text.
+ public static string Normalize(string markdown, MarkdownPipeline pipeline = null)
+ {
+ var writer = new StringWriter();
+ Normalize(markdown, writer, pipeline);
+ return writer.ToString();
+ }
+
+ ///
+ /// Normalizes the specified markdown to a normalized markdown text.
+ ///
+ /// The markdown.
+ /// The destination that will receive the result of the conversion.
+ /// The pipeline.
+ /// A normalized markdown text.
+ public static MarkdownDocument Normalize(string markdown, TextWriter writer, MarkdownPipeline pipeline = null)
+ {
+ pipeline = pipeline ?? new MarkdownPipelineBuilder().Build();
+ pipeline = CheckForSelfPipeline(pipeline, markdown);
+
+ // We override the renderer with our own writer
+ var renderer = new NormalizeRenderer(writer);
+ pipeline.Setup(renderer);
+
+ var document = Parse(markdown, pipeline);
+ renderer.Render(document);
+ writer.Flush();
+
+ return document;
+ }
+
///
/// Converts a Markdown string to HTML.
///
diff --git a/src/Markdig/Parsers/ThematicBreakParser.cs b/src/Markdig/Parsers/ThematicBreakParser.cs
index f73eece2..f0f45fee 100644
--- a/src/Markdig/Parsers/ThematicBreakParser.cs
+++ b/src/Markdig/Parsers/ThematicBreakParser.cs
@@ -88,7 +88,9 @@ namespace Markdig.Parsers
processor.NewBlocks.Push(new ThematicBreakBlock(this)
{
Column = processor.Column,
- Span = new SourceSpan(startPosition, line.End)
+ Span = new SourceSpan(startPosition, line.End),
+ ThematicChar = breakChar,
+ ThematicCharCount = breakCharCount
});
return BlockState.BreakDiscard;
}
diff --git a/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs b/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs
new file mode 100644
index 00000000..3c92121c
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs
@@ -0,0 +1,57 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+
+using Markdig.Renderers.Html;
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// An Normalize renderer for a and .
+ ///
+ ///
+ public class CodeBlockRenderer : NormalizeObjectRenderer
+ {
+ public bool OutputAttributesOnPre { get; set; }
+
+ protected override void Write(NormalizeRenderer renderer, CodeBlock obj)
+ {
+ var fencedCodeBlock = obj as FencedCodeBlock;
+ if (fencedCodeBlock != null)
+ {
+ var opening = new string(fencedCodeBlock.FencedChar, fencedCodeBlock.FencedCharCount);
+ renderer.Write(opening);
+ if (fencedCodeBlock.Info != null)
+ {
+ renderer.Write(fencedCodeBlock.Info);
+ }
+ if (string.IsNullOrEmpty(fencedCodeBlock.Arguments) == false)
+ {
+ renderer.Write(" ").Write(fencedCodeBlock.Arguments);
+ }
+
+ /* TODO do we need this causes a empty space and would render html attributes to markdown.
+ var attributes = obj.TryGetAttributes();
+ if (attributes != null)
+ {
+ renderer.Write(" ");
+ renderer.Write(attributes);
+ }
+ */
+ renderer.WriteLine();
+
+ renderer.WriteLeafRawLines(obj, true);
+ renderer.Write(opening);
+
+ renderer.FinishBlock();
+ }
+ else
+ {
+ renderer.WriteLeafRawLines(obj, false, true);
+
+ renderer.FinishBlock();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/HeadingRenderer.cs b/src/Markdig/Renderers/Normalize/HeadingRenderer.cs
new file mode 100644
index 00000000..6a467a4a
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/HeadingRenderer.cs
@@ -0,0 +1,36 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using System.Globalization;
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// An Normalize renderer for a .
+ ///
+ ///
+ public class HeadingRenderer : NormalizeObjectRenderer
+ {
+ private static readonly string[] HeadingTexts = {
+ "#",
+ "##",
+ "###",
+ "####",
+ "#####",
+ "######",
+ };
+
+ protected override void Write(NormalizeRenderer renderer, HeadingBlock obj)
+ {
+ var headingText = obj.Level > 0 && obj.Level <= 6
+ ? HeadingTexts[obj.Level - 1]
+ : new string('#', obj.Level);
+
+ renderer.Write(headingText).Write(' ');
+ renderer.WriteLeafInline(obj);
+
+ renderer.FinishBlock();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/AutolinkInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/AutolinkInlineRenderer.cs
new file mode 100644
index 00000000..6dede810
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/AutolinkInlineRenderer.cs
@@ -0,0 +1,19 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for an .
+ ///
+ ///
+ public class AutolinkInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, AutolinkInline obj)
+ {
+ renderer.Write('<').Write(obj.Url).Write('>');
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/CodeInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/CodeInlineRenderer.cs
new file mode 100644
index 00000000..e213fe98
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/CodeInlineRenderer.cs
@@ -0,0 +1,23 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class CodeInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, CodeInline obj)
+ {
+ var delimiter = obj.Content.Contains(obj.Delimiter + "") ? new string(obj.Delimiter, 2) : obj.Delimiter + "";
+
+ renderer.Write(delimiter);
+ renderer.Write(obj.Content);
+ renderer.Write(delimiter);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/DelimiterInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/DelimiterInlineRenderer.cs
new file mode 100644
index 00000000..54709ea4
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/DelimiterInlineRenderer.cs
@@ -0,0 +1,20 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class DelimiterInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, DelimiterInline obj)
+ {
+ renderer.Write(obj.ToLiteral());
+ renderer.WriteChildren(obj);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/EmphasisInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/EmphasisInlineRenderer.cs
new file mode 100644
index 00000000..36633f99
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/EmphasisInlineRenderer.cs
@@ -0,0 +1,22 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for an .
+ ///
+ ///
+ public class EmphasisInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, EmphasisInline obj)
+ {
+ var emphasisText = new string(obj.DelimiterChar, obj.IsDouble ? 2 : 1);
+ renderer.Write(emphasisText);
+ renderer.WriteChildren(obj);
+ renderer.Write(emphasisText);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/LineBreakInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/LineBreakInlineRenderer.cs
new file mode 100644
index 00000000..7a6670b0
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/LineBreakInlineRenderer.cs
@@ -0,0 +1,29 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class LineBreakInlineRenderer : NormalizeObjectRenderer
+ {
+ ///
+ /// Gets or sets a value indicating whether to render this softline break as a Normalize hardline break tag (<br />)
+ ///
+ public bool RenderAsHardlineBreak { get; set; }
+
+ protected override void Write(NormalizeRenderer renderer, LineBreakInline obj)
+ {
+ if (obj.IsHard)
+ {
+ renderer.Write(" ");
+ }
+
+ renderer.WriteLine();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/LinkInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/LinkInlineRenderer.cs
new file mode 100644
index 00000000..046aed0b
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/LinkInlineRenderer.cs
@@ -0,0 +1,38 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class LinkInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, LinkInline link)
+ {
+ if (link.IsImage)
+ {
+ renderer.Write('!');
+ }
+ renderer.Write('[');
+ renderer.WriteChildren(link);
+ renderer.Write(']');
+ if (!string.IsNullOrEmpty(link.Url))
+ {
+ renderer.Write('(').Write(link.Url);
+
+ if (!string.IsNullOrEmpty(link.Title))
+ {
+ renderer.Write(" \"");
+ renderer.Write(link.Title.Replace(@"""", @"\"""));
+ renderer.Write("\"");
+ }
+
+ renderer.Write(')');
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/LiteralInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/LiteralInlineRenderer.cs
new file mode 100644
index 00000000..3b4e410f
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/LiteralInlineRenderer.cs
@@ -0,0 +1,19 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class LiteralInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, LiteralInline obj)
+ {
+ renderer.Write(ref obj.Content);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlEntityInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlEntityInlineRenderer.cs
new file mode 100644
index 00000000..54ea1c51
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlEntityInlineRenderer.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ public class NormalizeHtmlEntityInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, HtmlEntityInline obj)
+ {
+ renderer.Write(obj.Original);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlInlineRenderer.cs b/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlInlineRenderer.cs
new file mode 100644
index 00000000..33bc8536
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlInlineRenderer.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax.Inlines;
+
+namespace Markdig.Renderers.Normalize.Inlines
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ public class NormalizeHtmlInlineRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, HtmlInline obj)
+ {
+ renderer.Write(obj.Tag);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/ListRenderer.cs b/src/Markdig/Renderers/Normalize/ListRenderer.cs
new file mode 100644
index 00000000..c898f590
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/ListRenderer.cs
@@ -0,0 +1,81 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using System.Globalization;
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class ListRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, ListBlock listBlock)
+ {
+ renderer.EnsureLine();
+ var compact = renderer.CompactParagraph;
+ renderer.CompactParagraph = !listBlock.IsLoose;
+ if (listBlock.IsOrdered)
+ {
+ int index = 0;
+ if (listBlock.OrderedStart != null)
+ {
+ switch (listBlock.BulletType)
+ {
+ case '1':
+ int.TryParse(listBlock.OrderedStart, out index);
+ break;
+ }
+ }
+ foreach (var item in listBlock)
+ {
+ var listItem = (ListItemBlock)item;
+ renderer.EnsureLine();
+
+ renderer.Write(index + "");
+ renderer.Write(listBlock.OrderedDelimiter);
+ renderer.Write(' ');
+ renderer.PushIndent(new string(' ', IntLog10Fast(index) + 3));
+ renderer.WriteChildren(listItem);
+ renderer.PopIndent();
+ switch (listBlock.BulletType)
+ {
+ case '1':
+ index++;
+ break;
+ }
+ }
+ }
+ else
+ {
+ foreach (var item in listBlock)
+ {
+ var listItem = (ListItemBlock)item;
+ renderer.EnsureLine();
+ renderer.Write("- ");
+ renderer.PushIndent(" ");
+ renderer.WriteChildren(listItem);
+ renderer.PopIndent();
+ }
+ }
+
+ renderer.CompactParagraph = compact;
+
+ renderer.FinishBlock();
+ }
+
+
+ private static int IntLog10Fast(int input) =>
+ (input < 10) ? 0 :
+ (input < 100) ? 1 :
+ (input < 1000) ? 2 :
+ (input < 10000) ? 3 :
+ (input < 100000) ? 4 :
+ (input < 1000000) ? 5 :
+ (input < 10000000) ? 6 :
+ (input < 100000000) ? 7 :
+ (input < 1000000000) ? 8 : 9;
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/NormalizeObjectRenderer.cs b/src/Markdig/Renderers/Normalize/NormalizeObjectRenderer.cs
new file mode 100644
index 00000000..f24daef2
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/NormalizeObjectRenderer.cs
@@ -0,0 +1,16 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// A base class for Normalize rendering and Markdown objects.
+ ///
+ /// The type of the object.
+ ///
+ public abstract class NormalizeObjectRenderer : MarkdownObjectRenderer where TObject : MarkdownObject
+ {
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/NormalizeRenderer.cs b/src/Markdig/Renderers/Normalize/NormalizeRenderer.cs
new file mode 100644
index 00000000..cc833c75
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/NormalizeRenderer.cs
@@ -0,0 +1,156 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+
+using System;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Text;
+using Markdig.Helpers;
+using Markdig.Renderers.Html;
+using Markdig.Syntax;
+using Markdig.Renderers.Normalize.Inlines;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// Default HTML renderer for a Markdown object.
+ ///
+ ///
+ public class NormalizeRenderer : TextRendererBase
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The writer.
+ public NormalizeRenderer(TextWriter writer) : base(writer)
+ {
+ // Default block renderers
+ ObjectRenderers.Add(new CodeBlockRenderer());
+ ObjectRenderers.Add(new ListRenderer());
+ ObjectRenderers.Add(new HeadingRenderer());
+ ObjectRenderers.Add(new HtmlBlockRenderer());
+ ObjectRenderers.Add(new ParagraphRenderer());
+ ObjectRenderers.Add(new QuoteBlockRenderer());
+ ObjectRenderers.Add(new ThematicBreakRenderer());
+
+ // Default inline renderers
+ ObjectRenderers.Add(new AutolinkInlineRenderer());
+ ObjectRenderers.Add(new CodeInlineRenderer());
+ ObjectRenderers.Add(new DelimiterInlineRenderer());
+ ObjectRenderers.Add(new EmphasisInlineRenderer());
+ ObjectRenderers.Add(new LineBreakInlineRenderer());
+ ObjectRenderers.Add(new NormalizeHtmlInlineRenderer());
+ ObjectRenderers.Add(new NormalizeHtmlEntityInlineRenderer());
+ ObjectRenderers.Add(new LinkInlineRenderer());
+ ObjectRenderers.Add(new LiteralInlineRenderer());
+ }
+
+ public bool CompactParagraph { get; set; }
+
+ public void FinishBlock()
+ {
+ if (!IsLastInContainer && !CompactParagraph)
+ {
+ WriteLine();
+ WriteLine();
+ }
+ }
+
+ /////
+ ///// Writes the attached on the specified .
+ /////
+ ///// The object.
+ /////
+ //public NormalizeRenderer WriteAttributes(MarkdownObject obj)
+ //{
+ // if (obj == null) throw new ArgumentNullException(nameof(obj));
+ // return WriteAttributes(obj.TryGetAttributes());
+ //}
+
+ /////
+ ///// Writes the specified .
+ /////
+ ///// The attributes to render.
+ ///// This instance
+ //public NormalizeRenderer WriteAttributes(HtmlAttributes attributes)
+ //{
+ // if (attributes == null)
+ // {
+ // return this;
+ // }
+
+ // if (attributes.Id != null)
+ // {
+ // Write(" id=\"").WriteEscape(attributes.Id).Write("\"");
+ // }
+
+ // if (attributes.Classes != null && attributes.Classes.Count > 0)
+ // {
+ // Write(" class=\"");
+ // for (int i = 0; i < attributes.Classes.Count; i++)
+ // {
+ // var cssClass = attributes.Classes[i];
+ // if (i > 0)
+ // {
+ // Write(" ");
+ // }
+ // WriteEscape(cssClass);
+ // }
+ // Write("\"");
+ // }
+
+ // if (attributes.Properties != null && attributes.Properties.Count > 0)
+ // {
+ // foreach (var property in attributes.Properties)
+ // {
+ // Write(" ").Write(property.Key);
+ // if (property.Value != null)
+ // {
+ // Write("=").Write("\"");
+ // WriteEscape(property.Value);
+ // Write("\"");
+ // }
+ // }
+ // }
+
+ // return this;
+ //}
+
+ ///
+ /// Writes the lines of a
+ ///
+ /// The leaf block.
+ /// if set to true write end of lines.
+ /// This instance
+ public NormalizeRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool indent = false)
+ {
+ if (leafBlock == null) throw new ArgumentNullException(nameof(leafBlock));
+ if (leafBlock.Lines.Lines != null)
+ {
+ var lines = leafBlock.Lines;
+ var slices = lines.Lines;
+ for (int i = 0; i < lines.Count; i++)
+ {
+ if (!writeEndOfLines && i > 0)
+ {
+ WriteLine();
+ }
+
+ if (indent)
+ {
+ Write(" ");
+ }
+
+ Write(ref slices[i].Slice);
+
+ if (writeEndOfLines)
+ {
+ WriteLine();
+ }
+ }
+ }
+ return this;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/ParagraphRenderer.cs b/src/Markdig/Renderers/Normalize/ParagraphRenderer.cs
new file mode 100644
index 00000000..d0a5cef6
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/ParagraphRenderer.cs
@@ -0,0 +1,25 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class ParagraphRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, ParagraphBlock obj)
+ {
+ if (!renderer.CompactParagraph)
+ {
+ renderer.EnsureLine();
+ }
+ renderer.WriteLeafInline(obj);
+
+ renderer.FinishBlock();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/QuoteBlockRenderer.cs b/src/Markdig/Renderers/Normalize/QuoteBlockRenderer.cs
new file mode 100644
index 00000000..ef478522
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/QuoteBlockRenderer.cs
@@ -0,0 +1,23 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class QuoteBlockRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, QuoteBlock obj)
+ {
+ renderer.PushIndent(obj.QuoteChar + " ");
+ renderer.WriteChildren(obj);
+ renderer.PopIndent();
+
+ renderer.FinishBlock();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs b/src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs
new file mode 100644
index 00000000..aee32e0c
--- /dev/null
+++ b/src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs
@@ -0,0 +1,21 @@
+// Copyright (c) Alexandre Mutel. All rights reserved.
+// This file is licensed under the BSD-Clause 2 license.
+// See the license.txt file in the project root for more information.
+using Markdig.Syntax;
+
+namespace Markdig.Renderers.Normalize
+{
+ ///
+ /// A Normalize renderer for a .
+ ///
+ ///
+ public class ThematicBreakRenderer : NormalizeObjectRenderer
+ {
+ protected override void Write(NormalizeRenderer renderer, ThematicBreakBlock obj)
+ {
+ renderer.WriteLine(new string(obj.ThematicChar, obj.ThematicCharCount));
+
+ renderer.FinishBlock();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Markdig/Renderers/TextRendererBase.cs b/src/Markdig/Renderers/TextRendererBase.cs
index b4ac3649..8c24758b 100644
--- a/src/Markdig/Renderers/TextRendererBase.cs
+++ b/src/Markdig/Renderers/TextRendererBase.cs
@@ -2,6 +2,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using Markdig.Helpers;
@@ -70,6 +71,7 @@ namespace Markdig.Renderers
{
private bool previousWasLine;
private char[] buffer;
+ private readonly List indents;
///
/// Initializes a new instance of the class.
@@ -80,6 +82,7 @@ namespace Markdig.Renderers
buffer = new char[1024];
// We assume that we are starting as if we had previously a newline
previousWasLine = true;
+ indents = new List();
}
///
@@ -95,6 +98,31 @@ namespace Markdig.Renderers
return (T)this;
}
+ public void PushIndent(string indent)
+ {
+ if (indent == null) throw new ArgumentNullException(nameof(indent));
+ indents.Add(indent);
+ }
+
+ public void PopIndent()
+ {
+ // TODO: Check
+ indents.RemoveAt(indents.Count - 1);
+ }
+
+ private void WriteIndent()
+ {
+ if (previousWasLine)
+ {
+ previousWasLine = false;
+ for (int i = 0; i < indents.Count; i++)
+ {
+ Writer.Write(indents[i]);
+ }
+ }
+ }
+
+
///
/// Writes the specified content.
///
@@ -103,6 +131,7 @@ namespace Markdig.Renderers
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public T Write(string content)
{
+ WriteIndent();
previousWasLine = false;
Writer.Write(content);
return (T) this;
@@ -142,6 +171,7 @@ namespace Markdig.Renderers
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public T Write(char content)
{
+ WriteIndent();
previousWasLine = content == '\n';
Writer.Write(content);
return (T) this;
@@ -161,6 +191,7 @@ namespace Markdig.Renderers
return (T) this;
}
+ WriteIndent();
previousWasLine = false;
if (offset == 0 && content.Length == length)
{
@@ -189,6 +220,7 @@ namespace Markdig.Renderers
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public T WriteLine()
{
+ WriteIndent();
Writer.WriteLine();
previousWasLine = true;
return (T) this;
@@ -202,6 +234,7 @@ namespace Markdig.Renderers
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public T WriteLine(string content)
{
+ WriteIndent();
previousWasLine = true;
Writer.WriteLine(content);
return (T) this;
diff --git a/src/Markdig/Syntax/ThematicBreakBlock.cs b/src/Markdig/Syntax/ThematicBreakBlock.cs
index ebf66bb1..c6839373 100644
--- a/src/Markdig/Syntax/ThematicBreakBlock.cs
+++ b/src/Markdig/Syntax/ThematicBreakBlock.cs
@@ -17,5 +17,9 @@ namespace Markdig.Syntax
public ThematicBreakBlock(BlockParser parser) : base(parser)
{
}
+
+ public char ThematicChar { get; set; }
+
+ public int ThematicCharCount { get; set; }
}
}
\ No newline at end of file