diff --git a/changelog.md b/changelog.md new file mode 100644 index 00000000..58bb8781 --- /dev/null +++ b/changelog.md @@ -0,0 +1,115 @@ +# Changelog + +## 0.14.8 (05 Dec 2017) +- Fix potential StackOverflow exception when processing deep nested `|` delimiters (#179) +## 0.14.7 (25 Nov 2017) +- Fix autolink attached attributes not being displayed properly (#175) +## 0.14.6 (21 Nov 2017) +- Fix yaml frontmatter issue when ending with a empty line (#170) +## 0.14.5 (18 Nov 2017) +- Fix changelog link from nuget package +## 0.14.4 (18 Nov 2017) +- Add changelog.md +- Fix bug when a thematic break is inside a fenced code block inside a pending list (#164) +- Add support for GFM autolinks (#165, #169) +- Better handle YAML frontmatter in case the opening `---` is never actually closed (#160) +- Fix link conflict between a link to an image definition and heading auto-identifiers (#159) +## 0.14.3 +- Make EmojiExtension.EnableSmiley public +## 0.14.2 +- Fix issue with emphasis preceded/followed by an HTML entity (#157) +- Add support for link reference definitions for Normalize renderer (#155) +- Add option to disable smiley parsing in EmojiAndSmiley extension +## 0.14.1 +- Fix crash in Markdown.Normalize to handle HtmlBlock correctly +- Add better handling of bullet character for lists in Markdown.Normalize +## 0.14.0 +- Add Markdown.ToPlainText, Add option HtmlRenderer.EnableHtmlForBlock. +- Add Markdown.Normalize, to allow to normalize a markdown document. Add NormalizeRenderer, to render a MarkdownDocument back to markdown. +## 0.13.4 +- Add support for single table header row without a table body rows (#141) +- ADd support for `nomnoml` diagrams +## 0.13.3 +- Add support for Pandoc YAML frontmatter (#138) +## 0.13.2 +- Add support for UAP10.0 (#137) +## 0.13.1 +- Fix indenting issue after a double digit list block using a tab (#134) +## 0.13.0 +- Update to latest CommonMark specs 0.28 +## 0.12.3 + - Fix issue with HTML blocks for heading h2,h3,h4,h5,h6 that were not correctly identified as HTML blocks as per CommonMark spec +## 0.12.2 + - Fix issue with generic attributes used just before a pipe table (issue #121) +## 0.12.1 + - Fix issue with media links extension when a URL to video is used, an unexpected closing `</iframe>` was inserted (issue #119) +## 0.12.0 + - Add new extension JiraLink support (thanks to @clarkd) + - Fix issue in html attributes not parsing correctly properties (thanks to @meziantou) + - Fix issues detected by an automatic static code analysis tool +## 0.11.0 + - Fix issue with math extension and $$ block parsing not handling correctly beginning of a $$ as a inline math instead (issue #107) + - Fix issue with custom attributes for emphasis + - Add support for new special custom arrows emoji (`->` `<-` `<->` `<=` `=>` `<==>`) +## 0.10.7 + - Fix issue when an url ends by a dot `.` +## 0.10.6 + - Fix emphasis with HTML entities +## 0.10.5 + - Several minor fixes +## 0.10.4 + - Fix issue with autolinks + - Normalize number of columns for tables +## 0.10.3 + - Fix issue with pipetables shifting a cell to a new column (issue #73) +## 0.10.2 + - Fix exception when trying to urlize an url with an unicode character outside the supported range by NormD (issue #75) +## 0.10.1 +- Update to latest CommonMark specs +- Fix source span for LinkReferenceDefinition +## 0.10.0 +- Breaking change of the IMarkdownExtension to allow to receive the MarkdownPipeline for the renderers setup +## 0.9.1 +- Fix regression bug with conflicts between autolink extension and html inline/regular links +## 0.9.0 +- Add new Autolink extension +## 0.8.5 +- Allow to force table column alignment to left +## 0.8.4 +- Fix issue when calculating the span of an indented code block within a list. Make sure to include first whitespace on the line +## 0.8.3 +- fix NullReferenceException with Gridtables extension when a single `+` is entered on a line +## 0.8.2 +- fix potential cast exception with Abreviation extension and empty literals +## 0.8.1 +- new extension to disable URI escaping for non-US-ASCII characters to workaround a bug in Edge/IE +- Fix an issue with abbreviations with left/right multiple non-punctuation/space characters +## 0.8.0 +- Update to latest CommonMark specs +- Fix empty literal +- Add YAML frontmatter extension +## 0.7.5 +- several bug fixes (pipe tables, disable HTML, special attributes, inline maths, abbreviations...) +- add support for rowspan in grid tables +## 0.7.4 +- Fix bug with strong emphasis starting at the beginning of a line +## 0.7.3 +- Fix threading issue with pipeline +## 0.7.2 +- Fix rendering of table colspan with non english locale +- Fix grid table colspan parsing +- Add nofollow extension for links +## 0.7.1 +- Fix issue in smarty pants which could lead to an InvalidCastException +- Update parsers to latest CommonMark specs +## 0.7.0 +- Update to latest NETStandard.Library 1.6.0 +- Fix issue with digits in auto-identifier extension +- Fix incorrect start of span calculated for code indented blocks +## 0.6.2 +- Handle latest CommonMark specs for corner cases for emphasis (See https://talk.commonmark.org/t/emphasis-strong-emphasis-corner-cases/2123/1 ) +## 0.6.1: +- Fix issue with autoidentifier extension overriding manual HTML attributes id on headings +## 0.6.0 +- Fix conflicts between PipeTables and SmartyPants extensions +- Add SelfPipeline extension diff --git a/src/Markdig.Tests/Markdig.Tests.csproj b/src/Markdig.Tests/Markdig.Tests.csproj index a506836a..9e7fc968 100644 --- a/src/Markdig.Tests/Markdig.Tests.csproj +++ b/src/Markdig.Tests/Markdig.Tests.csproj @@ -61,7 +61,9 @@ + + diff --git a/src/Markdig.Tests/Specs/AutoIdentifierSpecs.md b/src/Markdig.Tests/Specs/AutoIdentifierSpecs.md index 733da96e..82e1aaeb 100644 --- a/src/Markdig.Tests/Specs/AutoIdentifierSpecs.md +++ b/src/Markdig.Tests/Specs/AutoIdentifierSpecs.md @@ -96,3 +96,14 @@ The text of the link can be changed:

With a new text

This is a heading

```````````````````````````````` + +An autoidentifier should not conflict with an existing link: + +```````````````````````````````` example +![scenario image][scenario] +## Scenario +[scenario]: ./scenario.png +. +

scenario image

+

Scenario

+```````````````````````````````` diff --git a/src/Markdig.Tests/Specs/AutoLinks.md b/src/Markdig.Tests/Specs/AutoLinks.md index 293736d8..27b38973 100644 --- a/src/Markdig.Tests/Specs/AutoLinks.md +++ b/src/Markdig.Tests/Specs/AutoLinks.md @@ -76,3 +76,67 @@ Check **http://www.a.com** or __http://www.b.com__ .

Check http://www.a.com or http://www.b.com

```````````````````````````````` + +### GFM Support + +Extract from [GFM Autolinks extensions specs](https://github.github.com/gfm/#autolinks-extension-) + +```````````````````````````````` example +www.commonmark.org +. +

www.commonmark.org

+```````````````````````````````` + +```````````````````````````````` example +Visit www.commonmark.org/help for more information. +. +

Visit www.commonmark.org/help for more information.

+```````````````````````````````` + +```````````````````````````````` example +Visit www.commonmark.org. + +Visit www.commonmark.org/a.b. +. +

Visit www.commonmark.org.

+

Visit www.commonmark.org/a.b.

+```````````````````````````````` + + +```````````````````````````````` example +www.google.com/search?q=Markup+(business) + +(www.google.com/search?q=Markup+(business)) +. +

www.google.com/search?q=Markup+(business)

+

(www.google.com/search?q=Markup+(business))

+```````````````````````````````` + + +```````````````````````````````` example +www.google.com/search?q=commonmark&hl=en + +www.google.com/search?q=commonmark&hl; +. +

www.google.com/search?q=commonmark&hl=en

+

www.google.com/search?q=commonmark&hl;

+```````````````````````````````` + + +```````````````````````````````` example +www.commonmark.org/hewww.commonmark.org/he<lp

+```````````````````````````````` + +```````````````````````````````` example +http://commonmark.org + +(Visit https://encrypted.google.com/search?q=Markup+(business)) + +Anonymous FTP is available at ftp://foo.bar.baz. +. +

http://commonmark.org

+

(Visit https://encrypted.google.com/search?q=Markup+(business))

+

Anonymous FTP is available at ftp://foo.bar.baz.

+```````````````````````````````` diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index e290af69..a55aae54 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -20328,6 +20328,29 @@ namespace Markdig.Tests Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 10, "Extensions Heading Auto Identifiers"); TestParser.TestSpec("[With a new text][This is a heading]\n# This is a heading", "

With a new text

\n

This is a heading

", "autoidentifiers|advanced"); } + } + // An autoidentifier should not conflict with an existing link: + [TestFixture] + public partial class TestExtensionsHeadingAutoIdentifiers + { + [Test] + public void Example011() + { + // Example 11 + // Section: Extensions Heading Auto Identifiers + // + // The following CommonMark: + // ![scenario image][scenario] + // ## Scenario + // [scenario]: ./scenario.png + // + // Should be rendered as: + //

scenario image

+ //

Scenario

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 11, "Extensions Heading Auto Identifiers"); + TestParser.TestSpec("![scenario image][scenario]\n## Scenario\n[scenario]: ./scenario.png", "

\"scenario

\n

Scenario

", "autoidentifiers|advanced"); + } } // # Extensions // @@ -20607,6 +20630,7 @@ namespace Markdig.Tests // The following CommonMark: // --- // this: is a frontmatter + // // ... // This is a text // @@ -20614,10 +20638,10 @@ namespace Markdig.Tests //

This is a text

Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, "Extensions YAML frontmatter discard"); - TestParser.TestSpec("---\nthis: is a frontmatter\n...\nThis is a text", "

This is a text

", "yaml"); + TestParser.TestSpec("---\nthis: is a frontmatter\n\n...\nThis is a text", "

This is a text

", "yaml"); } } - // It expects exactly three dots `...`: + // If the end front matter marker (`...` or `---`) is not present, it will render the `---` has a `
`: [TestFixture] public partial class TestExtensionsYAMLfrontmatterdiscard { @@ -20630,17 +20654,18 @@ namespace Markdig.Tests // The following CommonMark: // --- // this: is a frontmatter - // .... // This is a text // // Should be rendered as: - // + //
+ //

this: is a frontmatter + // This is a text

Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, "Extensions YAML frontmatter discard"); - TestParser.TestSpec("---\nthis: is a frontmatter\n....\nThis is a text", "", "yaml"); + TestParser.TestSpec("---\nthis: is a frontmatter\nThis is a text", "
\n

this: is a frontmatter\nThis is a text

", "yaml"); } } - // Front matter ends with the first line containing three dots `...` or three dashes `...`: + // It expects exactly three dots `...`: [TestFixture] public partial class TestExtensionsYAMLfrontmatterdiscard { @@ -20654,6 +20679,32 @@ namespace Markdig.Tests // --- // this: is a frontmatter // .... + // This is a text + // + // Should be rendered as: + //
+ //

this: is a frontmatter + // .... + // This is a text

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, "Extensions YAML frontmatter discard"); + TestParser.TestSpec("---\nthis: is a frontmatter\n....\nThis is a text", "
\n

this: is a frontmatter\n....\nThis is a text

", "yaml"); + } + } + // Front matter ends with the first line containing three dots `...` or three dashes `...`: + [TestFixture] + public partial class TestExtensionsYAMLfrontmatterdiscard + { + [Test] + public void Example007() + { + // Example 7 + // Section: Extensions YAML frontmatter discard + // + // The following CommonMark: + // --- + // this: is a frontmatter + // .... // // Hello // --- @@ -20662,7 +20713,7 @@ namespace Markdig.Tests // Should be rendered as: //

This is a text

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, "Extensions YAML frontmatter discard"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions YAML frontmatter discard"); TestParser.TestSpec("---\nthis: is a frontmatter\n....\n\nHello\n---\nThis is a text", "

This is a text

", "yaml"); } } @@ -20673,7 +20724,7 @@ namespace Markdig.Tests [Test] public void ExtensionsYAMLfrontmatterdiscard_Example007() { - // Example 7 + // Example 8 // Section: Extensions YAML frontmatter discard // // The following CommonMark: @@ -20685,7 +20736,7 @@ namespace Markdig.Tests // Should be rendered as: //

This is a text

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions YAML frontmatter discard"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, "Extensions YAML frontmatter discard"); TestParser.TestSpec("--- \nthis: is a frontmatter\n...\nThis is a text", "

This is a text

", "yaml"); } } @@ -20696,7 +20747,7 @@ namespace Markdig.Tests [Test] public void ExtensionsYAMLfrontmatterdiscard_Example008() { - // Example 8 + // Example 9 // Section: Extensions YAML frontmatter discard // // The following CommonMark: @@ -20708,7 +20759,7 @@ namespace Markdig.Tests // Should be rendered as: //

This is a text

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, "Extensions YAML frontmatter discard"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 9, "Extensions YAML frontmatter discard"); TestParser.TestSpec("---\nthis: is a frontmatter\n... \nThis is a text", "

This is a text

", "yaml"); } } @@ -20874,6 +20925,157 @@ namespace Markdig.Tests Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions AutoLinks"); TestParser.TestSpec("Check **http://www.a.com** or __http://www.b.com__", "

Check http://www.a.com or http://www.b.com

", "autolinks|advanced"); } + } + // ### GFM Support + // + // Extract from [GFM Autolinks extensions specs](https://github.github.com/gfm/#autolinks-extension-) + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example008() + { + // Example 8 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // www.commonmark.org + // + // Should be rendered as: + //

www.commonmark.org

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("www.commonmark.org", "

www.commonmark.org

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example009() + { + // Example 9 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // Visit www.commonmark.org/help for more information. + // + // Should be rendered as: + //

Visit www.commonmark.org/help for more information.

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 9, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("Visit www.commonmark.org/help for more information.", "

Visit www.commonmark.org/help for more information.

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example010() + { + // Example 10 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // Visit www.commonmark.org. + // + // Visit www.commonmark.org/a.b. + // + // Should be rendered as: + //

Visit www.commonmark.org.

+ //

Visit www.commonmark.org/a.b.

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 10, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("Visit www.commonmark.org.\n\nVisit www.commonmark.org/a.b.", "

Visit www.commonmark.org.

\n

Visit www.commonmark.org/a.b.

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example011() + { + // Example 11 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // www.google.com/search?q=Markup+(business) + // + // (www.google.com/search?q=Markup+(business)) + // + // Should be rendered as: + //

www.google.com/search?q=Markup+(business)

+ //

(www.google.com/search?q=Markup+(business))

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 11, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("www.google.com/search?q=Markup+(business)\n\n(www.google.com/search?q=Markup+(business))", "

www.google.com/search?q=Markup+(business)

\n

(www.google.com/search?q=Markup+(business))

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example012() + { + // Example 12 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // www.google.com/search?q=commonmark&hl=en + // + // www.google.com/search?q=commonmark&hl; + // + // Should be rendered as: + //

www.google.com/search?q=commonmark&hl=en

+ //

www.google.com/search?q=commonmark&hl;

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 12, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("www.google.com/search?q=commonmark&hl=en\n\nwww.google.com/search?q=commonmark&hl;", "

www.google.com/search?q=commonmark&hl=en

\n

www.google.com/search?q=commonmark&hl;

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example013() + { + // Example 13 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // www.commonmark.org/hewww.commonmark.org/he<lp

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 13, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("www.commonmark.org/hewww.commonmark.org/he<lp

", "autolinks|advanced"); + } + } + [TestFixture] + public partial class TestExtensionsAutoLinksGFMSupport + { + [Test] + public void Example014() + { + // Example 14 + // Section: Extensions AutoLinks GFM Support + // + // The following CommonMark: + // http://commonmark.org + // + // (Visit https://encrypted.google.com/search?q=Markup+(business)) + // + // Anonymous FTP is available at ftp://foo.bar.baz. + // + // Should be rendered as: + //

http://commonmark.org

+ //

(Visit https://encrypted.google.com/search?q=Markup+(business))

+ //

Anonymous FTP is available at ftp://foo.bar.baz.

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 14, "Extensions AutoLinks GFM Support"); + TestParser.TestSpec("http://commonmark.org\n\n(Visit https://encrypted.google.com/search?q=Markup+(business))\n\nAnonymous FTP is available at ftp://foo.bar.baz.", "

http://commonmark.org

\n

(Visit https://encrypted.google.com/search?q=Markup+(business))

\n

Anonymous FTP is available at ftp://foo.bar.baz.

", "autolinks|advanced"); + } } // ## Jira Links // diff --git a/src/Markdig.Tests/Specs/YamlSpecs.md b/src/Markdig.Tests/Specs/YamlSpecs.md index b6ff478b..2233efcd 100644 --- a/src/Markdig.Tests/Specs/YamlSpecs.md +++ b/src/Markdig.Tests/Specs/YamlSpecs.md @@ -47,12 +47,25 @@ It can end with three dots `...`: ```````````````````````````````` example --- this: is a frontmatter + ... This is a text .

This is a text

```````````````````````````````` +If the end front matter marker (`...` or `---`) is not present, it will render the `---` has a `
`: + +```````````````````````````````` example +--- +this: is a frontmatter +This is a text +. +
+

this: is a frontmatter +This is a text

+```````````````````````````````` + It expects exactly three dots `...`: ```````````````````````````````` example @@ -61,6 +74,10 @@ this: is a frontmatter .... This is a text . +
+

this: is a frontmatter +.... +This is a text

```````````````````````````````` Front matter ends with the first line containing three dots `...` or three dashes `...`: diff --git a/src/Markdig.Tests/TestLinkHelper.cs b/src/Markdig.Tests/TestLinkHelper.cs index a1b0dc0b..f35f1a82 100644 --- a/src/Markdig.Tests/TestLinkHelper.cs +++ b/src/Markdig.Tests/TestLinkHelper.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -39,7 +39,7 @@ namespace Markdig.Tests { var text = new StringSlice(uri); string link; - Assert.True(LinkHelper.TryParseUrl(ref text, out link)); + Assert.True(LinkHelper.TryParseUrl(ref text, out link, true)); Assert.AreEqual("http://google.com", link); Assert.AreEqual('.', text.CurrentChar); } diff --git a/src/Markdig.Tests/TestNormalize.cs b/src/Markdig.Tests/TestNormalize.cs new file mode 100644 index 00000000..804f680f --- /dev/null +++ b/src/Markdig.Tests/TestNormalize.cs @@ -0,0 +1,495 @@ +// 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 SyntaxCodeBlock() + { + 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 SyntaxHeadline() + { + AssertSyntax("## Headline", new HeadingBlock(null) + { + HeaderChar = '#', + Level = 2, + Inline = new ContainerInline().AppendChild(new LiteralInline("Headline")), + }); + } + + [Test] + public void SyntaxParagraph() + { + 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 CodeBlock() + { + 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 Heading() + { + AssertNormalizeNoTrim("# Heading"); + AssertNormalizeNoTrim("## Heading"); + AssertNormalizeNoTrim("### Heading"); + AssertNormalizeNoTrim("#### Heading"); + AssertNormalizeNoTrim("##### Heading"); + AssertNormalizeNoTrim("###### Heading"); + AssertNormalizeNoTrim("###### Heading\n\ntext after two newlines"); + AssertNormalizeNoTrim("# Heading\nAnd Text1\n\nAndText2", options: new NormalizeOptions() { EmptyLineAfterHeading = false }); + + AssertNormalizeNoTrim("Heading\n=======\n\ntext after two newlines", "# Heading\n\ntext after two newlines"); + } + + [Test] + public void Backslash() + { + AssertNormalizeNoTrim("This is a hardline \nAnd this is another hardline\\\nThis is standard newline"); + AssertNormalizeNoTrim("This is a line\nWith another line\nAnd a last line"); + } + + [Test] + public void HtmlBlock() + { + /*AssertNormalizeNoTrim(@"
+");*/ // TODO: Bug: Throws Exception during emit + } + + [Test] + public void Paragraph() + { + AssertNormalizeNoTrim("This is a plain paragraph"); + AssertNormalizeNoTrim(@"This +is +a +plain +paragraph"); + } + + [Test] + public void ParagraphMulti() + { + AssertNormalizeNoTrim(@"line1 + +line2 + +line3"); + } + + [Test] + public void ListUnordered() + { + AssertNormalizeNoTrim(@"- a +- b +- c"); + } + + [Test] + public void ListUnorderedLoose() + { + AssertNormalizeNoTrim(@"- a + +- b + +- c"); + } + + [Test] + public void ListOrderedLooseAndCodeBlock() + { + AssertNormalizeNoTrim(@"1. ``` + foo + ``` + + bar"); + } + + [Test, Ignore("Not sure this is the correct normalize for this one. Need to check the specs")] + public void ListUnorderedLooseTop() + { + AssertNormalizeNoTrim(@"* foo + * bar + + baz", options: new NormalizeOptions() { ListItemCharacter = '*' }); + } + + [Test] + public void ListUnorderedLooseMultiParagraph() + { + AssertNormalizeNoTrim( +@"- a + + And another paragraph a + +- b + + And another paragraph b + +- c"); + } + + + [Test] + public void ListOrdered() + { + AssertNormalizeNoTrim(@"1. a +2. b +3. c"); + } + + + [Test] + public void ListOrderedAndIntended() + { + 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 HeaderAndParagraph() + { + AssertNormalizeNoTrim(@"# heading + +paragraph + +paragraph2 without newlines"); + } + + + [Test] + public void QuoteBlock() + { + AssertNormalizeNoTrim(@"> test1 +> +> test2"); + + AssertNormalizeNoTrim(@"> test1 +This is a continuation +> test2", + @"> test1 +> This is a continuation +> test2" +); + + AssertNormalizeNoTrim(@"> test1 +> -foobar + +asdf + +> test2 +> -foobar sen."); + } + + [Test] + public void ThematicBreak() + { + AssertNormalizeNoTrim("***\n"); + + AssertNormalizeNoTrim("* * *\n", "***\n"); + } + + [Test] + public void AutolinkInline() + { + AssertNormalizeNoTrim("This has a "); + } + + [Test] + public void CodeInline() + { + AssertNormalizeNoTrim("This has a `HelloWorld()` in it"); + AssertNormalizeNoTrim(@"This has a ``Hello`World()`` in it"); + } + + [Test] + public void EmphasisInline() + { + 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 LineBreakInline() + { + AssertNormalizeNoTrim("normal\nline break"); + AssertNormalizeNoTrim("hard \nline break"); + AssertNormalizeNoTrim("This is a hardline \nAnd this is another hardline\\\nThis is standard newline"); + AssertNormalizeNoTrim("This is a line\nWith another line\nAnd a last line"); + } + + [Test] + public void LinkInline() + { + AssertNormalizeNoTrim("This is a [link](http://company.com)"); + AssertNormalizeNoTrim("This is an ![image](http://company.com)"); + + AssertNormalizeNoTrim(@"This is a [link](http://company.com ""Crazy Company"")"); + AssertNormalizeNoTrim(@"This is a [link](http://company.com ""Crazy \"" Company"")"); + } + + [Test] + public void LinkReferenceDefinition() + { + // Full link + AssertNormalizeNoTrim("This is a [link][MyLink]\n\n[MyLink]: http://company.com"); + + AssertNormalizeNoTrim("[MyLink]: http://company.com\nThis is a [link][MyLink]", + "This is a [link][MyLink]\n\n[MyLink]: http://company.com"); + + AssertNormalizeNoTrim("This is a [link][MyLink] a normal link [link](http://google.com) and another def link [link2][MyLink2]\n\n[MyLink]: http://company.com\n[MyLink2]: http://company2.com"); + + // Collapsed link + AssertNormalizeNoTrim("This is a [link][]\n\n[link]: http://company.com"); + + // Shortcut link + AssertNormalizeNoTrim("This is a [link]\n\n[link]: http://company.com"); + } + + [Test] + public void EscapeInline() + { + AssertNormalizeNoTrim("This is an escape \\* with another \\["); + } + + [Test] + public void HtmlEntityInline() + { + AssertNormalizeNoTrim("This is a ä blank"); + } + + [Test] + public void HtmlInline() + { + AssertNormalizeNoTrim("foo
bar"); + AssertNormalizeNoTrim(@"foo
bar"); + } + + + [Test] + public void SpaceBetweenNodes() + { + AssertNormalizeNoTrim("# Hello World\nFoobar is a better bar.", + "# Hello World\n\nFoobar is a better bar."); + } + + [Test] + public void SpaceBetweenNodesEvenForHeadlines() + { + 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 SpaceRemoveAtStartAndEnd() + { + 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 SpaceShortenBetweenNodes() + { + AssertNormalizeNoTrim("# Hello World\n\n\n\nFoobar is a better bar.", + "# Hello World\n\nFoobar is a better bar."); + } + + [Test] + public void BiggerSample() + { + var input = @"# Heading 1 + +This is a paragraph + +This is another paragraph + +- This is a list item 1 +- This is a list item 2 +- This is a list item 3 + +```C# +This is a code block +``` + +> This is a quote block + + This is an indented code block + line 2 of indented + +This is a last line"; + AssertNormalizeNoTrim(input); + } + + [Test] + public void TaskLists() + { + AssertNormalizeNoTrim("- [X] This is done"); + AssertNormalizeNoTrim("- [x] This is done", + "- [X] This is done"); + AssertNormalizeNoTrim("- [ ] This is not done"); + + // ignore + AssertNormalizeNoTrim("[x] This is not a task list"); + AssertNormalizeNoTrim("[ ] This is not a task list"); + } + + + + [Test] + public void JiraLinks() + { + AssertNormalizeNoTrim("FOO-1234"); + AssertNormalizeNoTrim("AB-1"); + + AssertNormalizeNoTrim("**Hello World AB-1**"); + } + + [Test] + public void AutoLinks() + { + AssertNormalizeNoTrim("Hello from http://example.com/foo", "Hello from [http://example.com/foo](http://example.com/foo)", new NormalizeOptions() { ExpandAutoLinks = true, }); + AssertNormalizeNoTrim("Hello from www.example.com/foo", "Hello from [www.example.com/foo](http://www.example.com/foo)", new NormalizeOptions() { ExpandAutoLinks = true, }); + AssertNormalizeNoTrim("Hello from ftp://example.com", "Hello from [ftp://example.com](ftp://example.com)", new NormalizeOptions() { ExpandAutoLinks = true, }); + AssertNormalizeNoTrim("Hello from mailto:hello@example.com", "Hello from [mailto:hello@example.com](mailto:hello@example.com)", new NormalizeOptions() { ExpandAutoLinks = true, }); + + AssertNormalizeNoTrim("Hello from http://example.com/foo", "Hello from http://example.com/foo", new NormalizeOptions() { ExpandAutoLinks = false, }); + AssertNormalizeNoTrim("Hello from www.example.com/foo", "Hello from http://www.example.com/foo", new NormalizeOptions() { ExpandAutoLinks = false, }); + AssertNormalizeNoTrim("Hello from mailto:hello@example.com", "Hello from mailto:hello@example.com", new NormalizeOptions() { ExpandAutoLinks = false, }); + } + + 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, NormalizeOptions options = null) + => AssertNormalize(input, expected, false, options); + + public void AssertNormalize(string input, string expected = null, bool trim = true, NormalizeOptions options = null) + { + expected = expected ?? input; + input = NormText(input, trim); + expected = NormText(expected, trim); + + var pipeline = new MarkdownPipelineBuilder() + .UseAutoLinks() + .UseJiraLinks(new Extensions.JiraLinks.JiraLinkOptions("https://jira.example.com")) + .UseTaskLists() + .Build(); + + var result = Markdown.Normalize(input, options, pipeline: pipeline); + 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.Tests/TestParser.cs b/src/Markdig.Tests/TestParser.cs index e11a4b0d..9fcdf1c9 100644 --- a/src/Markdig.Tests/TestParser.cs +++ b/src/Markdig.Tests/TestParser.cs @@ -1,16 +1,43 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// 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.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using Markdig.Extensions.JiraLinks; +using NUnit.Framework; namespace Markdig.Tests { public class TestParser { + [Test] + public void TestEmphasisAndHtmlEntity() + { + var markdownText = "*Unlimited-Fun®*®"; + TestSpec(markdownText, "

Unlimited-Fun®®

"); + } + + [Test] + public void TestThematicInsideCodeBlockInsideList() + { + var input = @"1. In the : + + ``` + Id DisplayName Description + -- ----------- ----------- + 62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified ... + ```"; + TestSpec(input, @"
    +
  1. In the :

    +
    Id                                   DisplayName         Description
    +--                                   -----------         -----------
    +62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified       ...
    +
  2. +
"); + } + public static void TestSpec(string inputText, string expectedOutputText, string extensions = null) { foreach (var pipeline in GetPipeline(extensions)) diff --git a/src/Markdig.Tests/TestPlainText.cs b/src/Markdig.Tests/TestPlainText.cs new file mode 100644 index 00000000..f21b68fc --- /dev/null +++ b/src/Markdig.Tests/TestPlainText.cs @@ -0,0 +1,17 @@ +using NUnit.Framework; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestPlainText + { + [Test] + public void TestPlain() + { + var markdownText = "*Hello*, [world](http://example.com)!"; + var expected = "Hello, world!"; + var actual = Markdown.ToPlainText(markdownText); + Assert.AreEqual(expected, actual); + } + } +} diff --git a/src/Markdig.Tests/TestPlayParser.cs b/src/Markdig.Tests/TestPlayParser.cs index f62bceb1..1181f910 100644 --- a/src/Markdig.Tests/TestPlayParser.cs +++ b/src/Markdig.Tests/TestPlayParser.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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; @@ -12,6 +12,14 @@ namespace Markdig.Tests [TestFixture] public class TestPlayParser { + [Test] + public void TestLink() + { + var doc = Markdown.Parse("There is a ![link](/yoyo)"); + var link = doc.Descendants().SelectMany(x => x.Inline.Descendants()).FirstOrDefault(l => l.IsImage); + Assert.AreEqual("/yoyo", link?.Url); + } + [Test] public void TestListBug2() { diff --git a/src/Markdig.Tests/TextAssert.cs b/src/Markdig.Tests/TextAssert.cs index 27e39f77..5f9c7895 100644 --- a/src/Markdig.Tests/TextAssert.cs +++ b/src/Markdig.Tests/TextAssert.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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; @@ -86,7 +86,7 @@ namespace Markdig.Tests //output.WriteLine(); } - Assert.True(string.CompareOrdinal(expectedValue, actualValue) == 0, "strings are differing"); + Assert.AreEqual(expectedValue, actualValue); } private static string ToSafeString(this char c) diff --git a/src/Markdig.WebApp/ApiController.cs b/src/Markdig.WebApp/ApiController.cs index 2ff1ea52..832ed012 100644 --- a/src/Markdig.WebApp/ApiController.cs +++ b/src/Markdig.WebApp/ApiController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text; using Microsoft.AspNetCore.Mvc; @@ -6,6 +6,13 @@ namespace Markdig.WebApp { public class ApiController : Controller { + [HttpGet()] + [Route("")] + public string Empty() + { + return string.Empty; + } + // GET api/to_html?text=xxx&extensions=advanced [Route("api/to_html")] [HttpGet()] diff --git a/src/Markdig.WebApp/Connected Services/Application Insights/ConnectedService.json b/src/Markdig.WebApp/Connected Services/Application Insights/ConnectedService.json new file mode 100644 index 00000000..996eed9c --- /dev/null +++ b/src/Markdig.WebApp/Connected Services/Application Insights/ConnectedService.json @@ -0,0 +1,7 @@ +{ + "ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider", + "Version": "8.9.809.2", + "GettingStartedDocument": { + "Uri": "https://go.microsoft.com/fwlink/?LinkID=798432" + } +} \ No newline at end of file diff --git a/src/Markdig.WebApp/Markdig.WebApp.csproj b/src/Markdig.WebApp/Markdig.WebApp.csproj index e63154d5..1b2deb5b 100644 --- a/src/Markdig.WebApp/Markdig.WebApp.csproj +++ b/src/Markdig.WebApp/Markdig.WebApp.csproj @@ -1,13 +1,14 @@ - netcoreapp1.0 + netcoreapp2.0 true Markdig.WebApp Exe Markdig.WebApp - 1.0.4 - $(PackageTargetFallback);dotnet5.6;dnxcore50;portable-net45+win8 + 2.0.0 + /subscriptions/b6745039-70e7-4641-994b-5457cb220e2a/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Markdig.WebApp + /subscriptions/b6745039-70e7-4641-994b-5457cb220e2a/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Markdig.WebApp @@ -21,16 +22,20 @@ - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/src/Markdig.WebApp/Program.cs b/src/Markdig.WebApp/Program.cs index fcc43656..805a92b1 100644 --- a/src/Markdig.WebApp/Program.cs +++ b/src/Markdig.WebApp/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -13,6 +13,7 @@ namespace Markdig.WebApp public static void Main(string[] args) { var host = new WebHostBuilder() + .UseApplicationInsights() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() diff --git a/src/Markdig.WebApp/Startup.cs b/src/Markdig.WebApp/Startup.cs index ddcd083d..108217ab 100644 --- a/src/Markdig.WebApp/Startup.cs +++ b/src/Markdig.WebApp/Startup.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -46,10 +46,6 @@ namespace Markdig.WebApp loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); - app.UseApplicationInsightsRequestTelemetry(); - - app.UseApplicationInsightsExceptionTelemetry(); - app.UseMvc(); } } diff --git a/src/Markdig.WebApp/appsettings.json b/src/Markdig.WebApp/appsettings.json index fa8ce71a..a1568abb 100644 --- a/src/Markdig.WebApp/appsettings.json +++ b/src/Markdig.WebApp/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "IncludeScopes": false, "LogLevel": { @@ -6,5 +6,8 @@ "System": "Information", "Microsoft": "Information" } + }, + "ApplicationInsights": { + "InstrumentationKey": "5d12f113-76b2-41fe-a35a-db454b104bf9" } -} +} \ No newline at end of file diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs index e2134795..7fc79131 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.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 Markdig.Helpers; @@ -89,13 +90,41 @@ namespace Markdig.Extensions.AutoIdentifiers Heading = headingBlock, CreateLinkInline = CreateLinkInlineForHeading }; - processor.Document.SetLinkReferenceDefinition(text, linkRef); + + var doc = processor.Document; + var dictionary = doc.GetData(this) as Dictionary; + if (dictionary == null) + { + dictionary = new Dictionary(); + doc.SetData(this, dictionary); + doc.ProcessInlinesBegin += DocumentOnProcessInlinesBegin; + } + dictionary[text] = linkRef; } // Then we register after inline have been processed to actually generate the proper #id headingBlock.ProcessInlinesEnd += HeadingBlock_ProcessInlinesEnd; } + private void DocumentOnProcessInlinesBegin(InlineProcessor processor, Inline inline) + { + var doc = processor.Document; + doc.ProcessInlinesBegin -= DocumentOnProcessInlinesBegin; + var dictionary = (Dictionary)doc.GetData(this); + foreach (var keyPair in dictionary) + { + // Here we make sure that auto-identifiers will not override an existing link definition + // defined in the document + // If it is the case, we skip the auto identifier for the Heading + if (!doc.TryGetLinkReferenceDefinition(keyPair.Key, out var linkDef)) + { + doc.SetLinkReferenceDefinition(keyPair.Key, keyPair.Value); + } + } + // Once we are done, we don't need to keep the intermediate dictionary arround + doc.RemoveData(this); + } + /// /// Callback when there is a reference to found to a heading. /// Note that reference are only working if they are declared after. @@ -144,13 +173,15 @@ namespace Markdig.Extensions.AutoIdentifiers var headingText = headingWriter.ToString(); headingWriter.GetStringBuilder().Length = 0; - // TODO: Should we have a struct with more configure optionss for LinkHelper.Urilize? - headingText = LinkHelper.Urilize(headingText, - (options & AutoIdentifierOptions.AllowOnlyAscii) != 0, - (options & AutoIdentifierOptions.KeepOpeningDigits) != 0, - (options & AutoIdentifierOptions.DiscardDots) != 0); + // Urilize the link + headingText = (options & AutoIdentifierOptions.GitHub) != 0 + ? LinkHelper.UrilizeAsGfm(headingText) + : LinkHelper.Urilize(headingText, (options & AutoIdentifierOptions.AllowOnlyAscii) != 0); + // If the heading is empty, use the word "section" instead var baseHeadingId = string.IsNullOrEmpty(headingText) ? "section" : headingText; + + // Add a trailing -1, -2, -3...etc. in case of collision int index = 0; var headingId = baseHeadingId; var headingBuffer = StringBuilderCache.Local(); diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs index dde55ecc..16cce6c2 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs @@ -21,11 +21,6 @@ namespace Markdig.Extensions.AutoIdentifiers /// Default = AutoLink | AllowOnlyAscii, - /// - /// Renders auto identifiers like GitHub. - /// - GitHub = Default | KeepOpeningDigits | DiscardDots, - /// /// Allows to link to a header by using the same text as the header for the link label. Default is true /// @@ -37,13 +32,8 @@ namespace Markdig.Extensions.AutoIdentifiers AllowOnlyAscii = 2, /// - /// Allows to keep digits starting a heading (by default, it keeps only characters starting from the first letter) + /// Renders auto identifiers like GitHub. /// - KeepOpeningDigits = 4, - - /// - /// Discard dots when computing an identifier. - /// - DiscardDots = 8 + GitHub = 4, } } \ No newline at end of file diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs index 0d5a1fd8..113f92a9 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs @@ -1,9 +1,10 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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; -using Markdig.Syntax.Inlines; +using Markdig.Renderers.Normalize; +using Markdig.Renderers.Normalize.Inlines; namespace Markdig.Extensions.AutoLinks { @@ -24,6 +25,11 @@ namespace Markdig.Extensions.AutoLinks public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { + var normalizeRenderer = renderer as NormalizeRenderer; + if (normalizeRenderer != null && !normalizeRenderer.ObjectRenderers.Contains()) + { + normalizeRenderer.ObjectRenderers.InsertBefore(new NormalizeAutoLinkRenderer()); + } } } } \ No newline at end of file diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs index eaded567..fe937873 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -30,11 +30,18 @@ namespace Markdig.Extensions.AutoLinks }; } + + private static bool IsValidPreviousCharacter(char c) + { + // All such recognized autolinks can only come at the beginning of a line, after whitespace, or any of the delimiting characters *, _, ~, and (. + return c.IsWhiteSpaceOrZero() || c == '*' || c == '_' || c == '~' || c == '('; + } + public override bool Match(InlineProcessor processor, ref StringSlice slice) { // Previous char must be a whitespace or a punctuation var previousChar = slice.PeekCharExtra(-1); - if (!previousChar.IsAsciiPunctuation() && !previousChar.IsWhiteSpaceOrZero()) + if (!IsValidPreviousCharacter(previousChar)) { return false; } @@ -72,7 +79,7 @@ namespace Markdig.Extensions.AutoLinks break; case 'w': - if (!slice.MatchLowercase("ww.", 1) || previousChar == '/') // We won't match http:/www. or /www.xxx + if (!slice.MatchLowercase("ww.", 1)) // We won't match http:/www. or /www.xxx { return false; } @@ -81,7 +88,7 @@ namespace Markdig.Extensions.AutoLinks // Parse URL string link; - if (!LinkHelper.TryParseUrl(ref slice, out link)) + if (!LinkHelper.TryParseUrl(ref slice, out link, true)) { return false; } @@ -151,6 +158,7 @@ namespace Markdig.Extensions.AutoLinks Column = column, Url = c == 'w' ? "http://" + link : link, IsClosed = true, + IsAutoLink = true, }; inline.Span.End = inline.Span.Start + link.Length - 1; inline.UrlSpan = inline.Span; diff --git a/src/Markdig/Extensions/AutoLinks/NormalizeAutoLinkRenderer.cs b/src/Markdig/Extensions/AutoLinks/NormalizeAutoLinkRenderer.cs new file mode 100644 index 00000000..39d1630a --- /dev/null +++ b/src/Markdig/Extensions/AutoLinks/NormalizeAutoLinkRenderer.cs @@ -0,0 +1,29 @@ +using Markdig.Renderers; +using Markdig.Renderers.Normalize; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; + +namespace Markdig.Extensions.AutoLinks +{ + public class NormalizeAutoLinkRenderer : NormalizeObjectRenderer + { + public override bool Accept(RendererBase renderer, MarkdownObject obj) + { + if (base.Accept(renderer, obj)) + { + var normalizeRenderer = renderer as NormalizeRenderer; + var link = obj as LinkInline; + + return normalizeRenderer != null && link != null && !normalizeRenderer.Options.ExpandAutoLinks && link.IsAutoLink; + } + else + { + return false; + } + } + protected override void Write(NormalizeRenderer renderer, LinkInline obj) + { + renderer.Write(obj.Url); + } + } +} diff --git a/src/Markdig/Extensions/Emoji/EmojiExtension.cs b/src/Markdig/Extensions/Emoji/EmojiExtension.cs index eee63777..e4255d28 100644 --- a/src/Markdig/Extensions/Emoji/EmojiExtension.cs +++ b/src/Markdig/Extensions/Emoji/EmojiExtension.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -12,12 +12,19 @@ namespace Markdig.Extensions.Emoji /// public class EmojiExtension : IMarkdownExtension { + public EmojiExtension(bool enableSmiley = true) + { + EnableSmiley = enableSmiley; + } + + public bool EnableSmiley { get; set; } + public void Setup(MarkdownPipelineBuilder pipeline) { if (!pipeline.InlineParsers.Contains()) { // Insert the parser before any other parsers - pipeline.InlineParsers.Insert(0, new EmojiParser()); + pipeline.InlineParsers.Insert(0, new EmojiParser(EnableSmiley)); } } diff --git a/src/Markdig/Extensions/Emoji/EmojiParser.cs b/src/Markdig/Extensions/Emoji/EmojiParser.cs index b4ece1f9..62c797e0 100644 --- a/src/Markdig/Extensions/Emoji/EmojiParser.cs +++ b/src/Markdig/Extensions/Emoji/EmojiParser.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -24,13 +24,19 @@ namespace Markdig.Extensions.Emoji /// /// Initializes a new instance of the class. /// - public EmojiParser() + public EmojiParser(bool enableSmiley = true) { + EnableSmiley = enableSmiley; OpeningCharacters = null; EmojiToUnicode = new Dictionary(EmojiToUnicodeDefault); SmileyToEmoji = new Dictionary(SmileyToEmojiDefault); } + /// + /// Gets or sets a boolean indicating whether to process smiley. + /// + public bool EnableSmiley { get; set; } + /// /// Gets the emoji to unicode mapping. This can be modified before this parser is initialized. /// @@ -81,11 +87,14 @@ namespace Markdig.Extensions.Emoji return false; } - // If we have a smiley, we decode it to emoji - string emoji; - if (!SmileyToEmoji.TryGetValue(match, out emoji)) + string emoji = match; + if (EnableSmiley) { - emoji = match; + // If we have a smiley, we decode it to emoji + if (!SmileyToEmoji.TryGetValue(match, out emoji)) + { + emoji = match; + } } // Decode the eomji to unicode diff --git a/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs b/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs index 0c30594b..169970fa 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs @@ -1,8 +1,10 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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.Parsers.Inlines; using Markdig.Renderers; +using Markdig.Renderers.Normalize.Inlines; +using Markdig.Renderers.Normalize; namespace Markdig.Extensions.JiraLinks { @@ -30,7 +32,13 @@ namespace Markdig.Extensions.JiraLinks public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { - // Nothing to setup, JiraLinks used a normal LinkInlineRenderer + // No HTML renderer required, since JiraLink type derives from InlineLink (which already has an HTML renderer) + + var normalizeRenderer = renderer as NormalizeRenderer; + if (normalizeRenderer != null && !normalizeRenderer.ObjectRenderers.Contains()) + { + normalizeRenderer.ObjectRenderers.InsertBefore(new NormalizeJiraLinksRenderer()); + } } } diff --git a/src/Markdig/Extensions/JiraLinks/NormalizeJiraLinksRenderer.cs b/src/Markdig/Extensions/JiraLinks/NormalizeJiraLinksRenderer.cs new file mode 100644 index 00000000..754bc961 --- /dev/null +++ b/src/Markdig/Extensions/JiraLinks/NormalizeJiraLinksRenderer.cs @@ -0,0 +1,18 @@ +using Markdig.Renderers.Normalize; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Markdig.Extensions.JiraLinks +{ + public class NormalizeJiraLinksRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, JiraLink obj) + { + renderer.Write(obj.ProjectKey); + renderer.Write("-"); + renderer.Write(obj.Issue); + } + } +} diff --git a/src/Markdig/Extensions/MediaLinks/MediaOptions.cs b/src/Markdig/Extensions/MediaLinks/MediaOptions.cs index 81cf78c4..5f4c03b1 100644 --- a/src/Markdig/Extensions/MediaLinks/MediaOptions.cs +++ b/src/Markdig/Extensions/MediaLinks/MediaOptions.cs @@ -68,6 +68,7 @@ namespace Markdig.Extensions.MediaLinks {".wma", "audio/x-ms-wma"}, {".wax", "audio/x-ms-wax"}, {".mid", "audio/midi"}, + {".mp3", "audio/mpeg"}, {".mpga", "audio/mpeg"}, {".mp4a", "audio/mp4"}, {".ecelp4800", "audio/vnd.nuera.ecelp4800"}, @@ -88,4 +89,4 @@ namespace Markdig.Extensions.MediaLinks public Dictionary ExtensionToMimeType { get; } } -} \ No newline at end of file +} diff --git a/src/Markdig/Extensions/TaskLists/NormalizeTaskListRenderer.cs b/src/Markdig/Extensions/TaskLists/NormalizeTaskListRenderer.cs new file mode 100644 index 00000000..73b757ff --- /dev/null +++ b/src/Markdig/Extensions/TaskLists/NormalizeTaskListRenderer.cs @@ -0,0 +1,14 @@ +using Markdig.Renderers.Normalize; + +namespace Markdig.Extensions.TaskLists +{ + public class NormalizeTaskListRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, TaskList obj) + { + renderer.Write("["); + renderer.Write(obj.Checked ? "X" : " "); + renderer.Write("]"); + } + } +} diff --git a/src/Markdig/Extensions/TaskLists/TaskListExtension.cs b/src/Markdig/Extensions/TaskLists/TaskListExtension.cs index 3d770e49..caf81934 100644 --- a/src/Markdig/Extensions/TaskLists/TaskListExtension.cs +++ b/src/Markdig/Extensions/TaskLists/TaskListExtension.cs @@ -1,9 +1,10 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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.Parsers.Inlines; using Markdig.Renderers; +using Markdig.Renderers.Normalize; namespace Markdig.Extensions.TaskLists { @@ -28,6 +29,12 @@ namespace Markdig.Extensions.TaskLists { htmlRenderer.ObjectRenderers.AddIfNotAlready(); } + + var normalizeRenderer = renderer as NormalizeRenderer; + if (normalizeRenderer != null) + { + normalizeRenderer.ObjectRenderers.AddIfNotAlready(); + } } } } \ No newline at end of file diff --git a/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs b/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs index 6ffb0d0f..062b1099 100644 --- a/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs +++ b/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs @@ -67,17 +67,55 @@ namespace Markdig.Extensions.Yaml // this is a YAML front matter blcok if (count == 3 && (c == '\0' || c.IsWhitespace()) && line.TrimEnd()) { - // Create a front matter block - var block = this.CreateFrontMatterBlock(processor); - block.Column = processor.Column; - block.Span.Start = 0; - block.Span.End = line.Start; + bool hasFullYamlFrontMatter = false; + // We make sure that there is a closing frontmatter somewhere in the document + // so here we work on the full document instead of just the line + var fullLine = new StringSlice(line.Text, line.Start, line.Text.Length - 1); + c = fullLine.CurrentChar; + while (c != '\0') + { + c = fullLine.NextChar(); + if (c == '\n' || c == '\r') + { + var nc = fullLine.PeekChar(); + if (c == '\r' && nc == '\n') + { + c = fullLine.NextChar(); + } + nc = fullLine.PeekChar(); + if (nc == '-') + { + if (fullLine.NextChar() == '-' && fullLine.NextChar() == '-' && fullLine.NextChar() == '-' && (fullLine.NextChar() == '\0' || fullLine.SkipSpacesToEndOfLineOrEndOfDocument())) + { + hasFullYamlFrontMatter = true; + break; + } + } + else if (nc == '.') + { + if (fullLine.NextChar() == '.' && fullLine.NextChar() == '.' && fullLine.NextChar() == '.' && (fullLine.NextChar() == '\0' || fullLine.SkipSpacesToEndOfLineOrEndOfDocument())) + { + hasFullYamlFrontMatter = true; + break; + } + } + } + } - // Store the number of matched string into the context - processor.NewBlocks.Push(block); + if (hasFullYamlFrontMatter) + { + // Create a front matter block + var block = this.CreateFrontMatterBlock(processor); + block.Column = processor.Column; + block.Span.Start = 0; + block.Span.End = line.Start; - // Discard the current line as it is already parsed - return BlockState.ContinueDiscard; + // Store the number of matched string into the context + processor.NewBlocks.Push(block); + + // Discard the current line as it is already parsed + return BlockState.ContinueDiscard; + } } return BlockState.None; diff --git a/src/Markdig/Helpers/HtmlHelper.cs b/src/Markdig/Helpers/HtmlHelper.cs index bdedce3e..da86ac8a 100644 --- a/src/Markdig/Helpers/HtmlHelper.cs +++ b/src/Markdig/Helpers/HtmlHelper.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -472,10 +472,10 @@ namespace Markdig.Helpers } else if (c == '&') { - string namedEntity; + int entityNameStart; + int entityNameLength; int numericEntity; - var match = ScanEntity(text, searchPos, text.Length - searchPos, out namedEntity, - out numericEntity); + var match = ScanEntity(new StringSlice(text, searchPos, text.Length - 1), out numericEntity, out entityNameStart, out entityNameLength); if (match == 0) { searchPos++; @@ -484,9 +484,10 @@ namespace Markdig.Helpers { searchPos += match; - if (namedEntity != null) + if (entityNameLength > 0) { - var decoded = EntityHelper.DecodeEntity(namedEntity); + var namedEntity = new StringSlice(text, entityNameStart, entityNameStart + entityNameLength - 1); + var decoded = EntityHelper.DecodeEntity(namedEntity.ToString()); if (decoded != null) { sb.Append(text, lastPos, searchPos - match - lastPos); @@ -533,7 +534,7 @@ namespace Markdig.Helpers /// Scans an entity. /// Returns number of chars matched. /// - public static int ScanEntity(string s, int pos, int length, out string namedEntity, out int numericEntity) + public static int ScanEntity(T slice, out int numericEntity, out int namedEntityStart, out int namedEntityLength) where T : ICharIterator { // Credits: code from CommonMark.NET // Copyright (c) 2014, Kārlis Gaņģis All rights reserved. @@ -545,29 +546,29 @@ namespace Markdig.Helpers .? { return 0; } */ - var lastPos = pos + length; - - namedEntity = null; numericEntity = 0; + namedEntityStart = 0; + namedEntityLength = 0; - if (pos + 3 >= lastPos) - return 0; - - if (s[pos] != '&') - return 0; - - char c; - int i; - int counter = 0; - if (s[pos + 1] == '#') + if (slice.CurrentChar != '&' || slice.PeekChar(3) == '\0') { - c = s[pos + 2]; + return 0; + } + + var start = slice.Start; + char c = slice.NextChar(); + int counter = 0; + + if (c == '#') + { + c = slice.PeekChar(); if (c == 'x' || c == 'X') { + c = slice.NextChar(); // skip # // expect 1-8 hex digits starting from pos+3 - for (i = pos + 3; i < lastPos; i++) + while (c != '\0') { - c = s[i]; + c = slice.NextChar(); if (c >= '0' && c <= '9') { if (++counter == 9) return 0; @@ -588,7 +589,7 @@ namespace Markdig.Helpers } if (c == ';') - return counter == 0 ? 0 : i - pos + 1; + return counter == 0 ? 0 : slice.Start - start + 1; return 0; } @@ -596,9 +597,10 @@ namespace Markdig.Helpers else { // expect 1-8 digits starting from pos+2 - for (i = pos + 2; i < lastPos; i++) + while (c != '\0') { - c = s[i]; + c = slice.NextChar(); + if (c >= '0' && c <= '9') { if (++counter == 9) return 0; @@ -607,7 +609,7 @@ namespace Markdig.Helpers } if (c == ';') - return counter == 0 ? 0 : i - pos + 1; + return counter == 0 ? 0 : slice.Start - start + 1; return 0; } @@ -616,25 +618,26 @@ namespace Markdig.Helpers else { // expect a letter and 1-31 letters or digits - c = s[pos + 1]; if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) return 0; - for (i = pos + 2; i < lastPos; i++) + namedEntityStart = slice.Start; + namedEntityLength++; + + while (c != '\0') { - c = s[i]; + c = slice.NextChar(); if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { if (++counter == 32) return 0; - + namedEntityLength++; continue; } if (c == ';') { - namedEntity = s.Substring(pos + 1, counter + 1); - return counter == 0 ? 0 : i - pos + 1; + return counter == 0 ? 0 : slice.Start - start + 1; } return 0; diff --git a/src/Markdig/Helpers/ICharIterator.cs b/src/Markdig/Helpers/ICharIterator.cs index 8cd5fc46..2ea1f32f 100644 --- a/src/Markdig/Helpers/ICharIterator.cs +++ b/src/Markdig/Helpers/ICharIterator.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -34,8 +34,9 @@ namespace Markdig.Helpers /// /// Peeks at the next character, without incrementing the position. /// + /// /// The next character. `\0` is end of the iteration. - char PeekChar(); + char PeekChar(int offset = 1); /// /// Gets a value indicating whether this instance is empty. diff --git a/src/Markdig/Helpers/LinkHelper.cs b/src/Markdig/Helpers/LinkHelper.cs index 875cf8c7..5dcb337d 100644 --- a/src/Markdig/Helpers/LinkHelper.cs +++ b/src/Markdig/Helpers/LinkHelper.cs @@ -4,6 +4,7 @@ using System; using System.Runtime.CompilerServices; using System.Text; +using Markdig.Parsers.Inlines; using Markdig.Syntax; namespace Markdig.Helpers @@ -18,7 +19,7 @@ namespace Markdig.Helpers return TryParseAutolink(ref text, out link, out isEmail); } - public static string Urilize(string headingText, bool allowOnlyAscii, bool keepOpeningDigits = false, bool discardDots = false) + public static string Urilize(string headingText, bool allowOnlyAscii, bool keepOpeningDigits = false) { var headingBuffer = StringBuilderCache.Local(); bool hasLetter = keepOpeningDigits && headingText.Length > 0 && char.IsLetterOrDigit(headingText[0]); @@ -47,7 +48,7 @@ namespace Markdig.Helpers } else if (hasLetter) { - if (IsReservedPunctuation(c, discardDots)) + if (IsReservedPunctuation(c)) { if (previousIsSpace) { @@ -67,7 +68,7 @@ namespace Markdig.Helpers else if (!previousIsSpace && c.IsWhitespace()) { var pc = headingBuffer[headingBuffer.Length - 1]; - if (!IsReservedPunctuation(pc, discardDots)) + if (!IsReservedPunctuation(pc)) { headingBuffer.Append('-'); } @@ -81,7 +82,7 @@ namespace Markdig.Helpers while (headingBuffer.Length > 0) { var c = headingBuffer[headingBuffer.Length - 1]; - if (IsReservedPunctuation(c, false)) + if (IsReservedPunctuation(c)) { headingBuffer.Length--; } @@ -96,10 +97,27 @@ namespace Markdig.Helpers return text; } - [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] - private static bool IsReservedPunctuation(char c, bool discardDots) + public static string UrilizeAsGfm(string headingText) { - return c == '_' || c == '-' || (!discardDots && c == '.'); + // Following https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb + var headingBuffer = StringBuilderCache.Local(); + for (int i = 0; i < headingText.Length; i++) + { + var c = char.ToLowerInvariant(headingText[i]); + if (char.IsLetterOrDigit(c) || c == ' ' || c == '-' || c == '_') + { + headingBuffer.Append(c == ' ' ? '-' : c); + } + } + var result = headingBuffer.ToString(); + headingBuffer.Length = 0; + return result; + } + + [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] + private static bool IsReservedPunctuation(char c) + { + return c == '_' || c == '-' || c == '.'; } public static bool TryParseAutolink(ref StringSlice text, out string link, out bool isEmail) @@ -496,7 +514,7 @@ namespace Markdig.Helpers return TryParseUrl(ref text, out link); } - public static bool TryParseUrl(ref T text, out string link) where T : ICharIterator + public static bool TryParseUrl(ref T text, out string link, bool isAutoLink = false) where T : ICharIterator { bool isValid = false; var buffer = StringBuilderCache.Local(); @@ -593,16 +611,30 @@ namespace Markdig.Helpers hasEscape = false; - if (IsEndOfUri(c)) + if (IsEndOfUri(c, isAutoLink)) { isValid = true; break; } - if (c == '.' && IsEndOfUri(text.PeekChar())) + if (isAutoLink) { - isValid = true; - break; + if (c == '&') + { + int entityNameStart; + int entityNameLength; + int entityValue; + if (HtmlHelper.ScanEntity(text, out entityValue, out entityNameStart, out entityNameLength) > 0) + { + isValid = true; + break; + } + } + if (IsTrailingUrlStopCharacter(c) && IsEndOfUri(text.PeekChar(), true)) + { + isValid = true; + break; + } } buffer.Append(c); @@ -621,9 +653,17 @@ namespace Markdig.Helpers return isValid; } - private static bool IsEndOfUri(char c) + [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] + private static bool IsTrailingUrlStopCharacter(char c) { - return c == '\0' || c.IsSpaceOrTab() || c.IsControl(); // TODO: specs unclear. space is strict or relaxed? (includes tabs?) + // Trailing punctuation (specifically, ?, !, ., ,, :, *, _, and ~) will not be considered part of the autolink, though they may be included in the interior of the link: + return c == '?' || c == '!' || c == '.' || c == ',' || c == ':' || c == '*' || c == '*' || c == '_' || c == '~'; + } + + [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] + private static bool IsEndOfUri(char c, bool isAutoLink) + { + return c == '\0' || c.IsSpaceOrTab() || c.IsControl() || (isAutoLink && c == '<'); // TODO: specs unclear. space is strict or relaxed? (includes tabs?) } public static bool TryParseLinkReferenceDefinition(T text, out string label, out string url, diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index b28ebe2f..493dc66f 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -197,14 +197,14 @@ namespace Markdig.Helpers /// public struct Iterator : ICharIterator { - private readonly StringLineGroup lines; - private int offset; + private readonly StringLineGroup _lines; + private int _offset; public Iterator(StringLineGroup lines) { - this.lines = lines; + this._lines = lines; Start = -1; - offset = -1; + _offset = -1; SliceIndex = 0; CurrentChar = '\0'; End = -2; @@ -228,45 +228,47 @@ namespace Markdig.Helpers public char NextChar() { Start++; - offset++; + _offset++; if (Start <= End) { - var slice = (StringSlice)lines.Lines[SliceIndex]; - if (offset < slice.Length) + var slice = (StringSlice)_lines.Lines[SliceIndex]; + if (_offset < slice.Length) { - CurrentChar = slice[slice.Start + offset]; + CurrentChar = slice[slice.Start + _offset]; } else { CurrentChar = '\n'; SliceIndex++; - offset = -1; + _offset = -1; } } else { CurrentChar = '\0'; Start = End + 1; - SliceIndex = lines.Count; - offset--; + SliceIndex = _lines.Count; + _offset--; } return CurrentChar; } - public char PeekChar() + public char PeekChar(int offset = 1) { - if (Start + 1 > End) + if (offset < 0) throw new ArgumentOutOfRangeException("Negative offset are not supported for StringLineGroup", nameof(offset)); + + if (Start + offset > End) { return '\0'; } - var slice = (StringSlice)lines.Lines[SliceIndex]; - if (offset + 1 >= slice.Length) + var slice = (StringSlice)_lines.Lines[SliceIndex]; + if (_offset + offset >= slice.Length) { return '\n'; } - return slice[slice.Start + offset + 1]; + return slice[slice.Start + _offset + offset]; } public bool TrimStart() diff --git a/src/Markdig/Helpers/StringSlice.cs b/src/Markdig/Helpers/StringSlice.cs index 1a245cb3..04c49f1c 100644 --- a/src/Markdig/Helpers/StringSlice.cs +++ b/src/Markdig/Helpers/StringSlice.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -106,23 +106,12 @@ namespace Markdig.Helpers /// The offset. /// The character at offset, returns `\0` if none. [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] - public char PeekChar(int offset) + public char PeekChar(int offset = 1) { var index = Start + offset; return index >= Start && index <= End ? Text[index] : (char) 0; } - /// - /// Peeks the character immediately after the current position - /// or returns `\0` if after the position. - /// - /// The next character, returns `\0` if none. - [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] - public char PeekChar() - { - return PeekChar(1); - } - /// /// Peeks a character at the specified offset from the current beginning of the string, without taking into account and /// @@ -179,6 +168,28 @@ namespace Markdig.Helpers return i == text.Length; } + /// + /// Expect spaces until a end of line. Return false otherwise. + /// + /// true if whitespaces where matched until a end of line + public bool SkipSpacesToEndOfLineOrEndOfDocument() + { + for (int i = Start; i <= End; i++) + { + var c = Text[i]; + if (c.IsWhitespace()) + { + if (c == '\0' || c == '\n' || (c == '\r' && i + 1 <= End && Text[i + 1] != '\n')) + { + return true; + } + continue; + } + return false; + } + return true; + } + /// /// Matches the specified text using lowercase comparison. /// diff --git a/src/Markdig/Markdig.csproj b/src/Markdig/Markdig.csproj index 5cdbd671..cee88ffb 100644 --- a/src/Markdig/Markdig.csproj +++ b/src/Markdig/Markdig.csproj @@ -5,31 +5,19 @@ Alexandre Mutel Markdig en-US - 0.13.4 + 0.14.8 Alexandre Mutel net35;net40;portable40-net40+sl5+win8+wp8+wpa81;netstandard1.1;uap10.0 Markdig Markdig Markdig.Signed Markdown CommonMark md html md2html - -> 0.13.4 -- Add support for single table header row without a table body rows (#141) -- ADd support for `nomnoml` diagrams -> 0.13.3 -- Add support for Pandoc YAML frontmatter (#138) -> 0.13.2 -- Add support for UAP10.0 (#137) -> 0.13.1 -- Fix indenting issue after a double digit list block using a tab (#134) -> 0.13.0 -- Update to latest CommonMark specs 0.28 - + https://github.com/lunet-io/markdig/blob/master/changelog.md https://raw.githubusercontent.com/lunet-io/markdig/master/img/markdig.png https://github.com/lunet-io/markdig https://github.com/lunet-io/markdig/blob/master/license.txt 1.6.0 - true + true @@ -62,21 +50,21 @@ $(DefineConstants);NETSTANDARD_11;SUPPORT_UNSAFE - + UAP 10.0.10240.0 - 10.0.10240.0 + 10.0.10240.0 $(DefineConstants);NETSTANDARD_11;SUPPORT_UNSAFE - + .NETPortable v4.0 Profile328 $(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets - - + + $(NoWarn);CS1591 true diff --git a/src/Markdig/Markdown.cs b/src/Markdig/Markdown.cs index 61cc8f32..9e11388c 100644 --- a/src/Markdig/Markdown.cs +++ b/src/Markdig/Markdown.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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; @@ -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,44 @@ 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 normalize options + /// The pipeline. + /// A normalized markdown text. + public static string Normalize(string markdown, NormalizeOptions options = null, MarkdownPipeline pipeline = null) + { + var writer = new StringWriter(); + Normalize(markdown, writer, options, 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 normalize options + /// The pipeline. + /// A normalized markdown text. + public static MarkdownDocument Normalize(string markdown, TextWriter writer, NormalizeOptions options = null, 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, options); + pipeline.Setup(renderer); + + var document = Parse(markdown, pipeline); + renderer.Render(document); + writer.Flush(); + + return document; + } + /// /// Converts a Markdown string to HTML. /// @@ -119,5 +158,50 @@ namespace Markdig } return pipeline; } + + /// + /// Converts a Markdown string to Plain text and output to the specified writer. + /// + /// A Markdown text. + /// The destination that will receive the result of the conversion. + /// The pipeline used for the conversion. + /// The Markdown document that has been parsed + /// if reader or writer variable are null + public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, MarkdownPipeline pipeline = null) + { + if (markdown == null) throw new ArgumentNullException(nameof(markdown)); + if (writer == null) throw new ArgumentNullException(nameof(writer)); + pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); + pipeline = CheckForSelfPipeline(pipeline, markdown); + + // We override the renderer with our own writer + var renderer = new HtmlRenderer(writer) + { + EnableHtmlForBlock = false, + EnableHtmlForInline = false + }; + pipeline.Setup(renderer); + + var document = Parse(markdown, pipeline); + renderer.Render(document); + writer.Flush(); + + return document; + } + + /// + /// Converts a Markdown string to HTML. + /// + /// A Markdown text. + /// The pipeline used for the conversion. + /// The result of the conversion + /// if markdown variable is null + public static string ToPlainText(string markdown, MarkdownPipeline pipeline = null) + { + if (markdown == null) throw new ArgumentNullException(nameof(markdown)); + var writer = new StringWriter(); + ToPlainText(markdown, writer, pipeline); + return writer.ToString(); + } } } \ No newline at end of file diff --git a/src/Markdig/MarkdownExtensions.cs b/src/Markdig/MarkdownExtensions.cs index 95535b86..519b9cd7 100644 --- a/src/Markdig/MarkdownExtensions.cs +++ b/src/Markdig/MarkdownExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -409,10 +409,14 @@ namespace Markdig /// Uses the emoji and smiley extension. /// /// The pipeline. + /// Enable smiley in addition to Emoji, true by default. /// The modified pipeline - public static MarkdownPipelineBuilder UseEmojiAndSmiley(this MarkdownPipelineBuilder pipeline) + public static MarkdownPipelineBuilder UseEmojiAndSmiley(this MarkdownPipelineBuilder pipeline, bool enableSmiley = true) { - pipeline.Extensions.AddIfNotAlready(); + if (!pipeline.Extensions.Contains()) + { + pipeline.Extensions.Add(new EmojiExtension(enableSmiley)); + } return pipeline; } diff --git a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs index 58a1fa24..7820d282 100644 --- a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs @@ -101,7 +101,7 @@ namespace Markdig.Parsers.Inlines } // Move current_position forward in the delimiter stack (if needed) until - // we find the first potential closer with delimiter * or _. (This will be the potential closer closest to the beginning of the input – the first one in parse order.) + // we find the first potential closer with delimiter * or _. (This will be the potential closer closest to the beginning of the input – the first one in parse order.) var child = container.LastChild; while (child != null) { @@ -138,12 +138,24 @@ namespace Markdig.Parsers.Inlines var delimiterChar = slice.CurrentChar; var emphasisDesc = emphasisMap[delimiterChar]; - var pc = slice.PeekCharExtra(-1); - if (pc == delimiterChar && slice.PeekCharExtra(-2) != '\\') - { - return false; - } + var pc = (char)0; + if (processor.Inline is HtmlEntityInline) + { + var htmlEntityInline = (HtmlEntityInline) processor.Inline; + if (htmlEntityInline.Transcoded.Length > 0) + { + pc = htmlEntityInline.Transcoded[htmlEntityInline.Transcoded.End]; + } + } + if (pc == 0) + { + pc = slice.PeekCharExtra(-1); + if (pc == delimiterChar && slice.PeekCharExtra(-2) != '\\') + { + return false; + } + } var startPosition = slice.Start; int delimiterCount = 0; @@ -161,6 +173,14 @@ namespace Markdig.Parsers.Inlines return false; } + // The following character is actually an entity, we need to decode it + int htmlLength; + string htmlString; + if (HtmlEntityParser.TryParse(ref slice, out htmlString, out htmlLength)) + { + c = htmlString[0]; + } + // Calculate Open-Close for current character bool canOpen; bool canClose; @@ -204,7 +224,7 @@ namespace Markdig.Parsers.Inlines // at the end of the CommonMark specs. // Move current_position forward in the delimiter stack (if needed) until - // we find the first potential closer with delimiter * or _. (This will be the potential closer closest to the beginning of the input – the first one in parse order.) + // we find the first potential closer with delimiter * or _. (This will be the potential closer closest to the beginning of the input – the first one in parse order.) for (int i = 0; i < delimiters.Count; i++) { var closeDelimiter = delimiters[i]; @@ -219,7 +239,7 @@ namespace Markdig.Parsers.Inlines while (true) { // Now, look back in the stack (staying above stack_bottom and the openers_bottom for this delimiter type) - // for the first matching potential opener (“matching” means same delimiter). + // for the first matching potential opener (“matching” means same delimiter). EmphasisDelimiterInline openDelimiter = null; int openDelimiterIndex = -1; for (int j = i - 1; j >= 0; j--) diff --git a/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs b/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs index 7cbbfeba..15b5ad6c 100644 --- a/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs @@ -28,10 +28,11 @@ namespace Markdig.Parsers.Inlines { processor.Inline = new LiteralInline() { - Content = new StringSlice(new string(c, 1)), + Content = new StringSlice(slice.Text, slice.Start, slice.Start), Span = { Start = processor.GetSourcePosition(startPosition, out line, out column) }, Line = line, - Column = column + Column = column, + IsFirstCharacterEscaped = true, }; processor.Inline.Span.End = processor.Inline.Span.Start + 1; slice.NextChar(); @@ -44,6 +45,7 @@ namespace Markdig.Parsers.Inlines processor.Inline = new LineBreakInline() { IsHard = true, + IsBackslash = true, Span = { Start = processor.GetSourcePosition(startPosition, out line, out column) }, Line = line, Column = column diff --git a/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs b/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs index b7ba6e8b..3aac8b7d 100644 --- a/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs +++ b/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs @@ -1,6 +1,8 @@ // 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.Text; using Markdig.Helpers; using Markdig.Syntax; using Markdig.Syntax.Inlines; @@ -21,26 +23,40 @@ namespace Markdig.Parsers.Inlines OpeningCharacters = new[] {'&'}; } - public override bool Match(InlineProcessor processor, ref StringSlice slice) + + public static bool TryParse(ref StringSlice slice, out string literal, out int match) { - string entityName; + literal = null; + int entityNameStart; + int entityNameLength; int entityValue; - var startPosition = slice.Start; - int match = HtmlHelper.ScanEntity(slice.Text, slice.Start, slice.Length, out entityName, out entityValue); + match = HtmlHelper.ScanEntity(slice, out entityValue, out entityNameStart, out entityNameLength); if (match == 0) { return false; } - string literal = null; - if (entityName != null) + if (entityNameLength > 0) { - literal = EntityHelper.DecodeEntity(entityName); + literal = EntityHelper.DecodeEntity(new StringSlice(slice.Text, entityNameStart, entityNameStart + entityNameLength - 1).ToString()); } else if (entityValue >= 0) { literal = (entityValue == 0 ? null : EntityHelper.DecodeEntity(entityValue)) ?? CharHelper.ZeroSafeString; } + return true; + } + + public override bool Match(InlineProcessor processor, ref StringSlice slice) + { + int match; + string literal; + if (!TryParse(ref slice, out literal, out match)) + { + return false; + } + + var startPosition = slice.Start; if (literal != null) { diff --git a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs index 2ac41bb9..21ebcee4 100644 --- a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs @@ -86,7 +86,7 @@ namespace Markdig.Parsers.Inlines } } - // If we don’t find one, we return a literal slice node ]. + // If we don’t find one, we return a literal slice node ]. // (Done after by the LiteralInline parser) return false; } @@ -95,7 +95,7 @@ namespace Markdig.Parsers.Inlines return false; } - private bool ProcessLinkReference(InlineProcessor state, string label, SourceSpan labelSpan, LinkDelimiterInline parent, int endPosition) + private bool ProcessLinkReference(InlineProcessor state, string label, bool isShortcut, SourceSpan labelSpan, LinkDelimiterInline parent, int endPosition) { bool isValidLink = false; LinkReferenceDefinition linkRef; @@ -120,6 +120,7 @@ namespace Markdig.Parsers.Inlines LabelSpan = labelSpan, UrlSpan = linkRef.UrlSpan, IsImage = parent.IsImage, + IsShortcut = isShortcut, Reference = linkRef, Span = new SourceSpan(parent.Span.Start, endPosition), Line = parent.Line, @@ -189,7 +190,7 @@ namespace Markdig.Parsers.Inlines if (openParent != null) { - // If we do find one, but it’s not active, + // If we do find one, but it’s not active, // we remove the inactive delimiter from the stack, // and return a literal text node ]. if (!openParent.IsActive) @@ -205,7 +206,7 @@ namespace Markdig.Parsers.Inlines return false; } - // If we find one and it’s active, + // If we find one and it’s active, // then we parse ahead to see if we have // an inline link/image, reference link/image, // compact reference link/image, @@ -261,6 +262,8 @@ namespace Markdig.Parsers.Inlines var labelSpan = SourceSpan.Empty; string label = null; bool isLabelSpanLocal = true; + + bool isShortcut = false; // Handle Collapsed links if (text.CurrentChar == '[') { @@ -276,6 +279,7 @@ namespace Markdig.Parsers.Inlines else { label = openParent.Label; + isShortcut = true; } if (label != null || LinkHelper.TryParseLabel(ref text, true, out label, out labelSpan)) @@ -285,7 +289,7 @@ namespace Markdig.Parsers.Inlines labelSpan = inlineState.GetSourcePositionFromLocalSpan(labelSpan); } - if (ProcessLinkReference(inlineState, label, labelSpan, openParent, inlineState.GetSourcePosition(text.Start - 1))) + if (ProcessLinkReference(inlineState, label, isShortcut, labelSpan, openParent, inlineState.GetSourcePosition(text.Start - 1))) { // Remove the open parent openParent.Remove(); diff --git a/src/Markdig/Parsers/ListBlockParser.cs b/src/Markdig/Parsers/ListBlockParser.cs index 1cca8848..6825232d 100644 --- a/src/Markdig/Parsers/ListBlockParser.cs +++ b/src/Markdig/Parsers/ListBlockParser.cs @@ -86,7 +86,7 @@ namespace Markdig.Parsers // interpretations of a line, the thematic break takes precedence BlockState result; var thematicParser = ThematicBreakParser.Default; - if (thematicParser.HasOpeningCharacter(processor.CurrentChar)) + if (!(processor.LastBlock is FencedCodeBlock) && thematicParser.HasOpeningCharacter(processor.CurrentChar)) { result = thematicParser.TryOpen(processor); if (result.IsBreak()) 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/Html/Inlines/AutolinkInlineRenderer.cs b/src/Markdig/Renderers/Html/Inlines/AutolinkInlineRenderer.cs index c0423650..8c019548 100644 --- a/src/Markdig/Renderers/Html/Inlines/AutolinkInlineRenderer.cs +++ b/src/Markdig/Renderers/Html/Inlines/AutolinkInlineRenderer.cs @@ -26,6 +26,7 @@ namespace Markdig.Renderers.Html.Inlines renderer.Write("mailto:"); } renderer.WriteEscapeUrl(obj.Url); + renderer.Write('"'); renderer.WriteAttributes(obj); if (!obj.IsEmail && AutoRelNoFollow) @@ -33,7 +34,7 @@ namespace Markdig.Renderers.Html.Inlines renderer.Write(" rel=\"nofollow\""); } - renderer.Write("\">"); + renderer.Write(">"); } renderer.WriteEscape(obj.Url); diff --git a/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs b/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs new file mode 100644 index 00000000..88ff325c --- /dev/null +++ b/src/Markdig/Renderers/Normalize/CodeBlockRenderer.cs @@ -0,0 +1,55 @@ +// 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)) + { + 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); + } + else + { + renderer.WriteLeafRawLines(obj, false, true); + } + + renderer.FinishBlock(renderer.Options.EmptyLineAfterCodeBlock); + } + } +} \ 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..21e961e8 --- /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(renderer.Options.EmptyLineAfterHeading); + } + } +} \ No newline at end of file diff --git a/src/Markdig/Renderers/Normalize/HtmlBlockRenderer.cs b/src/Markdig/Renderers/Normalize/HtmlBlockRenderer.cs new file mode 100644 index 00000000..0cc59dc9 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/HtmlBlockRenderer.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 +{ + public class HtmlBlockRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, HtmlBlock obj) + { + renderer.WriteLeafRawLines(obj, true, false); + } + } +} \ 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..e72da850 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/Inlines/LineBreakInlineRenderer.cs @@ -0,0 +1,28 @@ +// 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(obj.IsBackslash ? "\\" : " "); + } + 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..6b12ea8c --- /dev/null +++ b/src/Markdig/Renderers/Normalize/Inlines/LinkInlineRenderer.cs @@ -0,0 +1,60 @@ +// 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 (link.Label != null) + { + + var literal = link.FirstChild as LiteralInline; + if (literal != null && literal.Content.Match(link.Label) && literal.Content.Length == link.Label.Length) + { + // collapsed reference and shortcut links + if (!link.IsShortcut) + { + renderer.Write("[]"); + } + } + else + { + // full link + renderer.Write('[').Write(link.Label).Write(']'); + } + } + else + { + 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..0e06ec7e --- /dev/null +++ b/src/Markdig/Renderers/Normalize/Inlines/LiteralInlineRenderer.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.Helpers; +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) + { + if (obj.IsFirstCharacterEscaped && obj.Content.Length > 0 && obj.Content[obj.Content.Start].IsAsciiPunctuation()) + { + renderer.Write('\\'); + } + 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/LinkReferenceDefinitionGroupRenderer.cs b/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionGroupRenderer.cs new file mode 100644 index 00000000..8d1a7ca2 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionGroupRenderer.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; + +namespace Markdig.Renderers.Normalize +{ + public class LinkReferenceDefinitionGroupRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, LinkReferenceDefinitionGroup obj) + { + renderer.EnsureLine(); + renderer.WriteChildren(obj); + renderer.FinishBlock(false); + } + } +} \ No newline at end of file diff --git a/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionRenderer.cs b/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionRenderer.cs new file mode 100644 index 00000000..a5152d39 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionRenderer.cs @@ -0,0 +1,28 @@ +// 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 +{ + public class LinkReferenceDefinitionRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, LinkReferenceDefinition linkDef) + { + renderer.EnsureLine(); + renderer.Write('['); + renderer.Write(linkDef.Label); + renderer.Write("]: "); + + renderer.Write(linkDef.Url); + + if (linkDef.Title != null) + { + renderer.Write(" \""); + renderer.Write(linkDef.Title.Replace("\"", "\\\"")); + renderer.Write('"'); + } + renderer.FinishBlock(false); + } + } +} \ 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..f487d939 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/ListRenderer.cs @@ -0,0 +1,93 @@ +// 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; + } + } + for (var i = 0; i < listBlock.Count; i++) + { + var item = listBlock[i]; + var listItem = (ListItemBlock) item; + renderer.EnsureLine(); + + renderer.Write(index.ToString(CultureInfo.InvariantCulture)); + 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; + } + if (i + 1 < listBlock.Count && listBlock.IsLoose) + { + renderer.EnsureLine(); + renderer.WriteLine(); + } + } + } + else + { + for (var i = 0; i < listBlock.Count; i++) + { + var item = listBlock[i]; + var listItem = (ListItemBlock) item; + renderer.EnsureLine(); + renderer.Write(renderer.Options.ListItemCharacter ?? listBlock.BulletType); + renderer.Write(' '); + renderer.PushIndent(" "); + renderer.WriteChildren(listItem); + renderer.PopIndent(); + if (i + 1 < listBlock.Count && listBlock.IsLoose) + { + renderer.EnsureLine(); + renderer.WriteLine(); + } + } + } + renderer.CompactParagraph = compact; + + renderer.FinishBlock(true); + } + + + 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/NormalizeOptions.cs b/src/Markdig/Renderers/Normalize/NormalizeOptions.cs new file mode 100644 index 00000000..7e826553 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/NormalizeOptions.cs @@ -0,0 +1,54 @@ +// 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. +namespace Markdig.Renderers.Normalize +{ + /// + /// Defines the options used by + /// + public class NormalizeOptions + { + /// + /// Initialize a new instance of + /// + public NormalizeOptions() + { + SpaceAfterQuoteBlock = true; + EmptyLineAfterCodeBlock = true; + EmptyLineAfterHeading = true; + EmptyLineAfterThematicBreak = true; + ExpandAutoLinks = true; + ListItemCharacter = null; + } + + /// + /// Adds a space after a QuoteBlock >. Default is true + /// + public bool SpaceAfterQuoteBlock { get; set; } + + /// + /// Adds an empty line after a code block (fenced and tabbed). Default is true + /// + public bool EmptyLineAfterCodeBlock { get; set; } + + /// + /// Adds an empty line after an heading. Default is true + /// + public bool EmptyLineAfterHeading { get; set; } + + /// + /// Adds an empty line after an thematic break. Default is true + /// + public bool EmptyLineAfterThematicBreak { get; set; } + + /// + /// The bullet character used for list items. Default is null leaving the original bullet character as-is. + /// + public char? ListItemCharacter { get; set; } + + /// + /// Expands AutoLinks to the normal inline representation. Default is true + /// + public bool ExpandAutoLinks { get; set; } + } +} \ 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..dcb36436 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/NormalizeRenderer.cs @@ -0,0 +1,161 @@ +// 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 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. + /// The normalize options + public NormalizeRenderer(TextWriter writer, NormalizeOptions options = null) : base(writer) + { + Options = options ?? new NormalizeOptions(); + // 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()); + ObjectRenderers.Add(new LinkReferenceDefinitionGroupRenderer()); + ObjectRenderers.Add(new LinkReferenceDefinitionRenderer()); + + // 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 NormalizeOptions Options { get; } + + public bool CompactParagraph { get; set; } + + public void FinishBlock(bool emptyLine) + { + if (!IsLastInContainer) + { + WriteLine(); + if (emptyLine) + { + 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..0632f914 --- /dev/null +++ b/src/Markdig/Renderers/Normalize/ParagraphRenderer.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; + +namespace Markdig.Renderers.Normalize +{ + /// + /// A Normalize renderer for a . + /// + /// + public class ParagraphRenderer : NormalizeObjectRenderer + { + protected override void Write(NormalizeRenderer renderer, ParagraphBlock obj) + { + renderer.WriteLeafInline(obj); + renderer.FinishBlock(!renderer.CompactParagraph); + } + } +} \ 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..422db3ec --- /dev/null +++ b/src/Markdig/Renderers/Normalize/QuoteBlockRenderer.cs @@ -0,0 +1,24 @@ +// 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) + { + var quoteIndent = renderer.Options.SpaceAfterQuoteBlock ? obj.QuoteChar + " " : obj.QuoteChar.ToString(); + renderer.PushIndent(quoteIndent); + renderer.WriteChildren(obj); + renderer.PopIndent(); + + renderer.FinishBlock(true); + } + } +} \ 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..edcc41c7 --- /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(renderer.Options.EmptyLineAfterThematicBreak); + } + } +} \ 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/Inlines/Inline.cs b/src/Markdig/Syntax/Inlines/Inline.cs index 4d405a7f..43311fdc 100644 --- a/src/Markdig/Syntax/Inlines/Inline.cs +++ b/src/Markdig/Syntax/Inlines/Inline.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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; @@ -192,17 +192,16 @@ namespace Markdig.Syntax.Inlines /// true if this instance contains a parent of the specified type; false otherwise public bool ContainsParentOfType() where T : Inline { - var delimiter = this as T; - if (delimiter != null) + var inline = this; + while (inline != null) { - return true; + var delimiter = inline as T; + if (delimiter != null) + { + return true; + } + inline = inline.Parent; } - - if (Parent != null) - { - return Parent.ContainsParentOfType(); - } - return false; } @@ -213,19 +212,15 @@ namespace Markdig.Syntax.Inlines /// An enumeration on the parents of the specified type public IEnumerable FindParentOfType() where T : Inline { - var parent = Parent; - var delimiter = this as T; - if (delimiter != null) + var inline = this; + while (inline != null) { - yield return delimiter; - } - - if (parent != null) - { - foreach (var previous in parent.FindParentOfType()) + var delimiter = inline as T; + if (delimiter != null) { - yield return previous; + yield return delimiter; } + inline = inline.Parent; } } diff --git a/src/Markdig/Syntax/Inlines/LineBreakInline.cs b/src/Markdig/Syntax/Inlines/LineBreakInline.cs index ad18ab1a..0372ef60 100644 --- a/src/Markdig/Syntax/Inlines/LineBreakInline.cs +++ b/src/Markdig/Syntax/Inlines/LineBreakInline.cs @@ -10,5 +10,7 @@ namespace Markdig.Syntax.Inlines public class LineBreakInline : LeafInline { public bool IsHard { get; set; } + + public bool IsBackslash { get; set; } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/Inlines/LinkInline.cs b/src/Markdig/Syntax/Inlines/LinkInline.cs index f189ef41..88ffc5bb 100644 --- a/src/Markdig/Syntax/Inlines/LinkInline.cs +++ b/src/Markdig/Syntax/Inlines/LinkInline.cs @@ -64,6 +64,16 @@ namespace Markdig.Syntax.Inlines /// public bool IsImage { get; set; } + /// + /// Gets or sets a boolean indicating if this link is a shortcut link to a + /// + public bool IsShortcut { get; set; } + + /// + /// Gets or sets a boolean indicating whether the inline link was parsed using markdown syntax or was automatic recognized. + /// + public bool IsAutoLink { get; set; } + /// /// Gets or sets the reference this link is attached to. May be null. /// diff --git a/src/Markdig/Syntax/Inlines/LiteralInline.cs b/src/Markdig/Syntax/Inlines/LiteralInline.cs index 476ac934..46c4be2a 100644 --- a/src/Markdig/Syntax/Inlines/LiteralInline.cs +++ b/src/Markdig/Syntax/Inlines/LiteralInline.cs @@ -48,6 +48,11 @@ namespace Markdig.Syntax.Inlines /// public StringSlice Content; + /// + /// A boolean indicating whether the first character of this literal is escaped by `\`. + /// + public bool IsFirstCharacterEscaped { get; set; } + public override string ToString() { return Content.ToString(); 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 diff --git a/src/markdig.sln b/src/markdig.sln index 8e4bf05f..81b5a8fd 100644 --- a/src/markdig.sln +++ b/src/markdig.sln @@ -1,11 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.16 +VisualStudioVersion = 15.0.27004.2005 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{061866E2-005C-4D13-A338-EA464BBEC60F}" ProjectSection(SolutionItems) = preProject ..\appveyor.yml = ..\appveyor.yml + ..\changelog.md = ..\changelog.md ..\license.txt = ..\license.txt ..\readme.md = ..\readme.md EndProjectSection