diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..142bc04f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +* text=auto +*.cs text=auto diff=csharp +*.sln text=auto eol=crlf \ No newline at end of file diff --git a/changelog.md b/changelog.md index b4b00175..d5e456c6 100644 --- a/changelog.md +++ b/changelog.md @@ -1,213 +1,213 @@ -# Changelog - -## 0.20.0 (18 Apr 2020) -- Markdig is now compatible only with `NETStandard 2.0`, `NETStandard 2.1`, `NETCoreApp 2.1` and `NETCoreApp 3.1`. -- Many performance improvements from [PR #416](https://github.com/lunet-io/markdig/pull/416) -[PR #417](https://github.com/lunet-io/markdig/pull/417) -[PR #418](https://github.com/lunet-io/markdig/pull/418) -[PR #421](https://github.com/lunet-io/markdig/pull/421) -[PR #422](https://github.com/lunet-io/markdig/pull/422) -[PR #410](https://github.com/lunet-io/markdig/pull/410) - -## 0.18.3 (8 Mar 2020) -- Publish NuGet Symbol packages - -## 0.18.2 (8 Mar 2020) -- Optimize LineReader.ReadLine in [PR #393](https://github.com/lunet-io/markdig/pull/393) -- Use HashSet instead of Dictionary in CharacterMap in [PR #394](https://github.com/lunet-io/markdig/pull/394) -- Use BitVector128 in CharacterMap in [PR #396](https://github.com/lunet-io/markdig/pull/396) -- Optimizations in StringLineGroup in [PR #399](https://github.com/lunet-io/markdig/pull/399) -- Fixed a bug in HeadingRenderer in [PR #402](https://github.com/lunet-io/markdig/pull/402) -- Fixes issue #303 in [PR #404](https://github.com/lunet-io/markdig/pull/404) -- Make output of HtmlTableRenderer XML wellformed in [PR #406](https://github.com/lunet-io/markdig/pull/406) - -## 0.18.1 (21 Jan 2020) -- Re-allow emojis and smileys customization, that was broken in [PR #308](https://github.com/lunet-io/markdig/pull/308) ([PR #386](https://github.com/lunet-io/markdig/pull/386)) -- Add `IHostProvider` for medialink customization (#337), support protocol-less url (#135) ([(PR #341)](https://github.com/lunet-io/markdig/pull/341)) -- Add missing Descendants overload ([(PR #387)](https://github.com/lunet-io/markdig/pull/387)) - -## 0.18.0 (24 Oct 2019) -- Ignore backslashes in GFM AutoLinks ([(PR #357)](https://github.com/lunet-io/markdig/pull/357)) -- Fix SmartyPants quote matching ([(PR #360)](https://github.com/lunet-io/markdig/pull/360)) -- Fix generic attributes with values of length 1 ([(PR #361)](https://github.com/lunet-io/markdig/pull/361)) -- Fix link text balanced bracket matching ([(PR #375)](https://github.com/lunet-io/markdig/pull/375)) -- Improve overall performance and substantially reduce allocations ([(PR #377)](https://github.com/lunet-io/markdig/pull/377)) - -## 0.17.1 (04 July 2019) -- Fix regression when escaping HTML characters ([(PR #340)](https://github.com/lunet-io/markdig/pull/340)) -- Update Emoji Dictionary ([(PR #346)](https://github.com/lunet-io/markdig/pull/346)) - -## 0.17.0 (10 May 2019) -- Update to latest CommonMark specs 0.29 ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) -- Add `AutoLinkOptions` with `OpenInNewWindow`, `UseHttpsForWWWLinks` ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) -- Add `DisableHeadings` extension method to `MarkdownPipelineBuilder` ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) -- Drop support for netstandard1.1 and Portable Class Libraries ([(PR #319)](https://github.com/lunet-io/markdig/pull/319)) -- Allow non-ASCII characters in url domain names ([(PR #319)](https://github.com/lunet-io/markdig/pull/319)) -- Add better support for youtu.be link ([(PR #336)](https://github.com/lunet-io/markdig/pull/336)) -- Fix backsticks in Markdown.Normalize ([(PR #334)](https://github.com/lunet-io/markdig/pull/334)) - -## 0.16.0 (25 Feb 2019) -- Improve performance of emoji-abbreviation parser ([(PR #305)](https://github.com/lunet-io/markdig/pull/305)) -- Change output for math extension to use a rendering more compatible with existing Math JS libraries ([(PR #311)](https://github.com/lunet-io/markdig/pull/311)) -- Improve emphasis parser to allow to match more than 2 characters ([(PR #301)](https://github.com/lunet-io/markdig/pull/301)) -- Output attached attributes to a `` from a table row ([(PR #300)](https://github.com/lunet-io/markdig/pull/300)) -- Improve MarkdownObject.Descendants() search ([(PR #288)](https://github.com/lunet-io/markdig/pull/288)) -- Allow to pass a `MarkdownParserContext` ([(PR #285)](https://github.com/lunet-io/markdig/pull/285)) - -## 0.15.7 (11 Jan 2019) -- Add configurable leading count for ATX headers ([(PR #282)](https://github.com/lunet-io/markdig/pull/282)) -- Render XML well-formed boolean attribute ([(PR #281)](https://github.com/lunet-io/markdig/pull/281)) - -## 0.15.6 (28 Dec 2018) -- Fix potential hang when parsing LinkReferenceDefinition #278 -- Fix parsing of an invalid html entity (#277) -- Fix IndexOutOfRangeException while parsing fenced code block with a single trailing space (#276) -- Add tests for checking that ArgumentOutOfRangeException doesn't occur on invalid input md string (#275) - -## 0.15.5 (11 Dec 2018) -- Empty image alt text for link reference definitions ([(PR #254)](https://github.com/lunet-io/markdig/pull/254)) -- Fix AutoLink Match links without slash after domain ([(PR #260)](https://github.com/lunet-io/markdig/pull/260)) -- Make AutoLink ValidPreviousCharacters configurable ([(PR #264)](https://github.com/lunet-io/markdig/pull/264)) -- Ensuring line breaks when renderer does not have html enabled ([(PR #270)](https://github.com/lunet-io/markdig/pull/270)) - -## 0.15.4 (07 Oct 2018) -- Add autolink domain GFM validation ([(PR #253)](https://github.com/lunet-io/markdig/pull/253)) - -## 0.15.3 (15 Sep 2018) -- Add support for RTL ([(PR #239)](https://github.com/lunet-io/markdig/pull/239)) -- Add MarkdownDocument.LineCount ([(PR #241)](https://github.com/lunet-io/markdig/pull/241)) -- Fix source positions for link definitions ([(PR #243)](https://github.com/lunet-io/markdig/pull/243)) -- Add ListItemBlock.Order ([(PR #244)](https://github.com/lunet-io/markdig/pull/244)) -- Add MarkdownDocument.LineStartIndexes ([(PR #247)](https://github.com/lunet-io/markdig/pull/247)) - -## 0.15.2 (21 Aug 2018) -- Fix footnotes parsing when they are defined after a container that has been closed in the meantime (#223) - -## 0.15.1 (10 July 2018) -- Add support for `netstandard2.0` -- Make AutoIdentifierExtension thread safe - -## 0.15.0 (4 Apr 2018) -- Add `ConfigureNewLine` extension method to `MarkdownPipelineBuilder` ([(PR #214)](https://github.com/lunet-io/markdig/pull/214)) -- Add alternative `Use` extension method to `MarkdownPipelineBuilder` that receives an object instance ([(PR #213)](https://github.com/lunet-io/markdig/pull/213)) -- Added class attribute to media link extension ([(PR #203)](https://github.com/lunet-io/markdig/pull/203)) -- Optional link rewriter func for HtmlRenderer #143 ([(PR #201)](https://github.com/lunet-io/markdig/pull/201)) -- Upgrade NUnit3TestAdapter from 3.2 to 3.9 to address Resharper test runner problems ([(PR #199)](https://github.com/lunet-io/markdig/pull/199)) -- HTML renderer supports converting relative URLs on links and images to absolute #143 ([(PR #197)](https://github.com/lunet-io/markdig/pull/197)) - -## 0.14.9 (15 Jan 2018) -- AutoLinkParser should to remove mailto: in outputted text ([(PR #195)](https://github.com/lunet-io/markdig/pull/195)) -- Add support for `music.yandex.ru` and `ok.ru` for MediaLinks extension ([(PR #193)](https://github.com/lunet-io/markdig/pull/193)) -## 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 Abbreviation 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 +# Changelog + +## 0.20.0 (18 Apr 2020) +- Markdig is now compatible only with `NETStandard 2.0`, `NETStandard 2.1`, `NETCoreApp 2.1` and `NETCoreApp 3.1`. +- Many performance improvements from [PR #416](https://github.com/lunet-io/markdig/pull/416) +[PR #417](https://github.com/lunet-io/markdig/pull/417) +[PR #418](https://github.com/lunet-io/markdig/pull/418) +[PR #421](https://github.com/lunet-io/markdig/pull/421) +[PR #422](https://github.com/lunet-io/markdig/pull/422) +[PR #410](https://github.com/lunet-io/markdig/pull/410) + +## 0.18.3 (8 Mar 2020) +- Publish NuGet Symbol packages + +## 0.18.2 (8 Mar 2020) +- Optimize LineReader.ReadLine in [PR #393](https://github.com/lunet-io/markdig/pull/393) +- Use HashSet instead of Dictionary in CharacterMap in [PR #394](https://github.com/lunet-io/markdig/pull/394) +- Use BitVector128 in CharacterMap in [PR #396](https://github.com/lunet-io/markdig/pull/396) +- Optimizations in StringLineGroup in [PR #399](https://github.com/lunet-io/markdig/pull/399) +- Fixed a bug in HeadingRenderer in [PR #402](https://github.com/lunet-io/markdig/pull/402) +- Fixes issue #303 in [PR #404](https://github.com/lunet-io/markdig/pull/404) +- Make output of HtmlTableRenderer XML wellformed in [PR #406](https://github.com/lunet-io/markdig/pull/406) + +## 0.18.1 (21 Jan 2020) +- Re-allow emojis and smileys customization, that was broken in [PR #308](https://github.com/lunet-io/markdig/pull/308) ([PR #386](https://github.com/lunet-io/markdig/pull/386)) +- Add `IHostProvider` for medialink customization (#337), support protocol-less url (#135) ([(PR #341)](https://github.com/lunet-io/markdig/pull/341)) +- Add missing Descendants overload ([(PR #387)](https://github.com/lunet-io/markdig/pull/387)) + +## 0.18.0 (24 Oct 2019) +- Ignore backslashes in GFM AutoLinks ([(PR #357)](https://github.com/lunet-io/markdig/pull/357)) +- Fix SmartyPants quote matching ([(PR #360)](https://github.com/lunet-io/markdig/pull/360)) +- Fix generic attributes with values of length 1 ([(PR #361)](https://github.com/lunet-io/markdig/pull/361)) +- Fix link text balanced bracket matching ([(PR #375)](https://github.com/lunet-io/markdig/pull/375)) +- Improve overall performance and substantially reduce allocations ([(PR #377)](https://github.com/lunet-io/markdig/pull/377)) + +## 0.17.1 (04 July 2019) +- Fix regression when escaping HTML characters ([(PR #340)](https://github.com/lunet-io/markdig/pull/340)) +- Update Emoji Dictionary ([(PR #346)](https://github.com/lunet-io/markdig/pull/346)) + +## 0.17.0 (10 May 2019) +- Update to latest CommonMark specs 0.29 ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) +- Add `AutoLinkOptions` with `OpenInNewWindow`, `UseHttpsForWWWLinks` ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) +- Add `DisableHeadings` extension method to `MarkdownPipelineBuilder` ([(PR #327)](https://github.com/lunet-io/markdig/pull/327)) +- Drop support for netstandard1.1 and Portable Class Libraries ([(PR #319)](https://github.com/lunet-io/markdig/pull/319)) +- Allow non-ASCII characters in url domain names ([(PR #319)](https://github.com/lunet-io/markdig/pull/319)) +- Add better support for youtu.be link ([(PR #336)](https://github.com/lunet-io/markdig/pull/336)) +- Fix backsticks in Markdown.Normalize ([(PR #334)](https://github.com/lunet-io/markdig/pull/334)) + +## 0.16.0 (25 Feb 2019) +- Improve performance of emoji-abbreviation parser ([(PR #305)](https://github.com/lunet-io/markdig/pull/305)) +- Change output for math extension to use a rendering more compatible with existing Math JS libraries ([(PR #311)](https://github.com/lunet-io/markdig/pull/311)) +- Improve emphasis parser to allow to match more than 2 characters ([(PR #301)](https://github.com/lunet-io/markdig/pull/301)) +- Output attached attributes to a `` from a table row ([(PR #300)](https://github.com/lunet-io/markdig/pull/300)) +- Improve MarkdownObject.Descendants() search ([(PR #288)](https://github.com/lunet-io/markdig/pull/288)) +- Allow to pass a `MarkdownParserContext` ([(PR #285)](https://github.com/lunet-io/markdig/pull/285)) + +## 0.15.7 (11 Jan 2019) +- Add configurable leading count for ATX headers ([(PR #282)](https://github.com/lunet-io/markdig/pull/282)) +- Render XML well-formed boolean attribute ([(PR #281)](https://github.com/lunet-io/markdig/pull/281)) + +## 0.15.6 (28 Dec 2018) +- Fix potential hang when parsing LinkReferenceDefinition #278 +- Fix parsing of an invalid html entity (#277) +- Fix IndexOutOfRangeException while parsing fenced code block with a single trailing space (#276) +- Add tests for checking that ArgumentOutOfRangeException doesn't occur on invalid input md string (#275) + +## 0.15.5 (11 Dec 2018) +- Empty image alt text for link reference definitions ([(PR #254)](https://github.com/lunet-io/markdig/pull/254)) +- Fix AutoLink Match links without slash after domain ([(PR #260)](https://github.com/lunet-io/markdig/pull/260)) +- Make AutoLink ValidPreviousCharacters configurable ([(PR #264)](https://github.com/lunet-io/markdig/pull/264)) +- Ensuring line breaks when renderer does not have html enabled ([(PR #270)](https://github.com/lunet-io/markdig/pull/270)) + +## 0.15.4 (07 Oct 2018) +- Add autolink domain GFM validation ([(PR #253)](https://github.com/lunet-io/markdig/pull/253)) + +## 0.15.3 (15 Sep 2018) +- Add support for RTL ([(PR #239)](https://github.com/lunet-io/markdig/pull/239)) +- Add MarkdownDocument.LineCount ([(PR #241)](https://github.com/lunet-io/markdig/pull/241)) +- Fix source positions for link definitions ([(PR #243)](https://github.com/lunet-io/markdig/pull/243)) +- Add ListItemBlock.Order ([(PR #244)](https://github.com/lunet-io/markdig/pull/244)) +- Add MarkdownDocument.LineStartIndexes ([(PR #247)](https://github.com/lunet-io/markdig/pull/247)) + +## 0.15.2 (21 Aug 2018) +- Fix footnotes parsing when they are defined after a container that has been closed in the meantime (#223) + +## 0.15.1 (10 July 2018) +- Add support for `netstandard2.0` +- Make AutoIdentifierExtension thread safe + +## 0.15.0 (4 Apr 2018) +- Add `ConfigureNewLine` extension method to `MarkdownPipelineBuilder` ([(PR #214)](https://github.com/lunet-io/markdig/pull/214)) +- Add alternative `Use` extension method to `MarkdownPipelineBuilder` that receives an object instance ([(PR #213)](https://github.com/lunet-io/markdig/pull/213)) +- Added class attribute to media link extension ([(PR #203)](https://github.com/lunet-io/markdig/pull/203)) +- Optional link rewriter func for HtmlRenderer #143 ([(PR #201)](https://github.com/lunet-io/markdig/pull/201)) +- Upgrade NUnit3TestAdapter from 3.2 to 3.9 to address Resharper test runner problems ([(PR #199)](https://github.com/lunet-io/markdig/pull/199)) +- HTML renderer supports converting relative URLs on links and images to absolute #143 ([(PR #197)](https://github.com/lunet-io/markdig/pull/197)) + +## 0.14.9 (15 Jan 2018) +- AutoLinkParser should to remove mailto: in outputted text ([(PR #195)](https://github.com/lunet-io/markdig/pull/195)) +- Add support for `music.yandex.ru` and `ok.ru` for MediaLinks extension ([(PR #193)](https://github.com/lunet-io/markdig/pull/193)) +## 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 Abbreviation 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 281fdf29..d5c885ce 100644 --- a/src/Markdig.Tests/Markdig.Tests.csproj +++ b/src/Markdig.Tests/Markdig.Tests.csproj @@ -1,23 +1,23 @@ - - - - netcoreapp2.1;netcoreapp3.1 - Library - false - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - + + + + netcoreapp2.1;netcoreapp3.1 + Library + false + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + \ No newline at end of file diff --git a/src/Markdig.Tests/MiscTests.cs b/src/Markdig.Tests/MiscTests.cs index 8117b47f..532cee8a 100644 --- a/src/Markdig.Tests/MiscTests.cs +++ b/src/Markdig.Tests/MiscTests.cs @@ -1,241 +1,241 @@ -using System; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using Markdig.Extensions.AutoLinks; -using NUnit.Framework; - -namespace Markdig.Tests -{ - public class MiscTests - { - [TestCase("link [foo [bar]]")] // https://spec.commonmark.org/0.29/#example-508 - [TestCase("link [foo][bar]")] - [TestCase("link [][foo][bar][]")] - [TestCase("link [][foo][bar][[]]")] - [TestCase("link [foo] [bar]")] - [TestCase("link [[foo] [] [bar] [[abc]def]]")] - [TestCase("[]")] - [TestCase("[ ]")] - [TestCase("[bar][]")] - [TestCase("[bar][ foo]")] - [TestCase("[bar][foo ][]")] - [TestCase("[bar][fo[ ]o ][][]")] - [TestCase("[a]b[c[d[e]f]g]h")] - [TestCase("a[b[c[d]e]f[g]h]i foo [j]k[l[m]n]o")] - [TestCase("a[b[c[d]e]f[g]h]i[] [][foo][bar][] foo [j]k[l[m]n]o")] - [TestCase("a[b[c[d]e]f[g]h]i foo [j]k[l[m]n]o[][]")] - public void LinkTextMayContainBalancedBrackets(string linkText) - { - string markdown = $"[{linkText}](/uri)"; - string expected = $@"

{linkText}

"; - - TestParser.TestSpec(markdown, expected); - - // Make the link text unbalanced - foreach (var bracketIndex in linkText - .Select((c, i) => new Tuple(c, i)) - .Where(t => t.Item1 == '[' || t.Item1 == ']') - .Select(t => t.Item2)) - { - string brokenLinkText = linkText.Remove(bracketIndex, 1); - - markdown = $"[{brokenLinkText}](/uri)"; - expected = $@"

{brokenLinkText}

"; - - string actual = Markdown.ToHtml(markdown); - Assert.AreNotEqual(expected, actual); - } - } - - [Test] - public void IsIssue356Corrected() - { - string input = @"https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97"; - string expected = @"

https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97

"; - - TestParser.TestSpec($"<{input}>", expected); - TestParser.TestSpec(input, expected, "autolinks|advanced"); - } - - [Test] - public void TestAltTextIsCorrectlyEscaped() - { - TestParser.TestSpec( - @"![This is image alt text with quotation ' and double quotation ""hello"" world](girl.png)", - @"

"); - } - - [Test] - public void TestChangelogPRLinksMatchDescription() - { - string solutionFolder = Path.GetFullPath(Path.Combine(TestParser.TestsDirectory, "../..")); - string changelogPath = Path.Combine(solutionFolder, "changelog.md"); - string changelog = File.ReadAllText(changelogPath); - var matches = Regex.Matches(changelog, @"\(\[\(PR #(\d+)\)\]\(.*?pull\/(\d+)\)\)"); - Assert.Greater(matches.Count, 0); - foreach (Match match in matches) - { - Assert.True(int.TryParse(match.Groups[1].Value, out int textNr)); - Assert.True(int.TryParse(match.Groups[2].Value, out int linkNr)); - Assert.AreEqual(textNr, linkNr); - } - } - - [Test] - public void TestFixHang() - { - var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "hang.md")); - _ = Markdown.ToHtml(input); - } - - [Test] - public void TestInvalidHtmlEntity() - { - var input = "9&ddr;&*&ddr;&de��__"; - TestParser.TestSpec(input, "

9&ddr;&*&ddr;&de��__

"); - } - - [Test] - public void TestInvalidCharacterHandling() - { - var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "ArgumentOutOfRangeException.md")); - _ = Markdown.ToHtml(input); - } - - [Test] - public void TestInvalidCodeEscape() - { - var input = "```**Header** "; - _ = Markdown.ToHtml(input); - } - - [Test] - public void TestEmphasisAndHtmlEntity() - { - var markdownText = "*Unlimited-Fun®*®"; - TestParser.TestSpec(markdownText, "

Unlimited-Fun®®

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

    -
    Id                                   DisplayName         Description
    ---                                   -----------         -----------
    -62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified       ...
    -
  2. -
"); - } - - [Test] - public void VisualizeMathExpressions() - { - string math = @"Math expressions - -$\frac{n!}{k!(n-k)!} = \binom{n}{k}$ - -$$\frac{n!}{k!(n-k)!} = \binom{n}{k}$$ - -$$ -\frac{n!}{k!(n-k)!} = \binom{n}{k} -$$ - -
-\begin{align} -\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\ - & = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\ - & = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\ - & = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\ - & \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right) -\end{align} -
-"; - Console.WriteLine("Math Expressions:\n"); - - var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() - - - var html = Markdown.ToHtml(math, pl); - Console.WriteLine(html); - } - - [Test] - public void InlineMathExpression() - { - string math = @"Math expressions - -$\frac{n!}{k!(n-k)!} = \binom{n}{k}$ -"; - var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() - - var html = Markdown.ToHtml(math, pl); - Console.WriteLine(html); - - Assert.IsTrue(html.Contains("

\\("), "Leading bracket missing"); - Assert.IsTrue(html.Contains("\\)

"), "Trailing bracket missing"); - } - - [Test] - public void BlockMathExpression() - { - string math = @"Math expressions - -$$ -\frac{n!}{k!(n-k)!} = \binom{n}{k} -$$ -"; - var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() - - var html = Markdown.ToHtml(math, pl); - Console.WriteLine(html); - - Assert.IsTrue(html.Contains("
\n\\["), "Leading bracket missing"); - Assert.IsTrue(html.Contains("\\]
"), "Trailing bracket missing"); - } - - [Test] - public void CanDisableParsingHeadings() - { - var noHeadingsPipeline = new MarkdownPipelineBuilder().DisableHeadings().Build(); - - TestParser.TestSpec("Foo\n===", "

Foo

"); - TestParser.TestSpec("Foo\n===", "

Foo\n===

", noHeadingsPipeline); - - TestParser.TestSpec("# Heading 1", "

Heading 1

"); - TestParser.TestSpec("# Heading 1", "

# Heading 1

", noHeadingsPipeline); - - // Does not also disable link reference definitions - TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "

Foo

"); - TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "

Foo

", noHeadingsPipeline); - } - - [Test] - public void CanOpenAutoLinksInNewWindow() - { - var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); - var newWindowPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { OpenInNewWindow = true }).Build(); - - TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", pipeline); - TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", newWindowPipeline); - } - - [Test] - public void CanUseHttpsPrefixForWWWAutoLinks() - { - var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); - var httpsPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { UseHttpsForWWWLinks = true }).Build(); - - TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", pipeline); - TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", httpsPipeline); - } - } -} +using System; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using Markdig.Extensions.AutoLinks; +using NUnit.Framework; + +namespace Markdig.Tests +{ + public class MiscTests + { + [TestCase("link [foo [bar]]")] // https://spec.commonmark.org/0.29/#example-508 + [TestCase("link [foo][bar]")] + [TestCase("link [][foo][bar][]")] + [TestCase("link [][foo][bar][[]]")] + [TestCase("link [foo] [bar]")] + [TestCase("link [[foo] [] [bar] [[abc]def]]")] + [TestCase("[]")] + [TestCase("[ ]")] + [TestCase("[bar][]")] + [TestCase("[bar][ foo]")] + [TestCase("[bar][foo ][]")] + [TestCase("[bar][fo[ ]o ][][]")] + [TestCase("[a]b[c[d[e]f]g]h")] + [TestCase("a[b[c[d]e]f[g]h]i foo [j]k[l[m]n]o")] + [TestCase("a[b[c[d]e]f[g]h]i[] [][foo][bar][] foo [j]k[l[m]n]o")] + [TestCase("a[b[c[d]e]f[g]h]i foo [j]k[l[m]n]o[][]")] + public void LinkTextMayContainBalancedBrackets(string linkText) + { + string markdown = $"[{linkText}](/uri)"; + string expected = $@"

{linkText}

"; + + TestParser.TestSpec(markdown, expected); + + // Make the link text unbalanced + foreach (var bracketIndex in linkText + .Select((c, i) => new Tuple(c, i)) + .Where(t => t.Item1 == '[' || t.Item1 == ']') + .Select(t => t.Item2)) + { + string brokenLinkText = linkText.Remove(bracketIndex, 1); + + markdown = $"[{brokenLinkText}](/uri)"; + expected = $@"

{brokenLinkText}

"; + + string actual = Markdown.ToHtml(markdown); + Assert.AreNotEqual(expected, actual); + } + } + + [Test] + public void IsIssue356Corrected() + { + string input = @"https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97"; + string expected = @"

https://foo.bar/path/\#m4mv5W0GYKZpGvfA.97

"; + + TestParser.TestSpec($"<{input}>", expected); + TestParser.TestSpec(input, expected, "autolinks|advanced"); + } + + [Test] + public void TestAltTextIsCorrectlyEscaped() + { + TestParser.TestSpec( + @"![This is image alt text with quotation ' and double quotation ""hello"" world](girl.png)", + @"

"); + } + + [Test] + public void TestChangelogPRLinksMatchDescription() + { + string solutionFolder = Path.GetFullPath(Path.Combine(TestParser.TestsDirectory, "../..")); + string changelogPath = Path.Combine(solutionFolder, "changelog.md"); + string changelog = File.ReadAllText(changelogPath); + var matches = Regex.Matches(changelog, @"\(\[\(PR #(\d+)\)\]\(.*?pull\/(\d+)\)\)"); + Assert.Greater(matches.Count, 0); + foreach (Match match in matches) + { + Assert.True(int.TryParse(match.Groups[1].Value, out int textNr)); + Assert.True(int.TryParse(match.Groups[2].Value, out int linkNr)); + Assert.AreEqual(textNr, linkNr); + } + } + + [Test] + public void TestFixHang() + { + var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "hang.md")); + _ = Markdown.ToHtml(input); + } + + [Test] + public void TestInvalidHtmlEntity() + { + var input = "9&ddr;&*&ddr;&de��__"; + TestParser.TestSpec(input, "

9&ddr;&*&ddr;&de��__

"); + } + + [Test] + public void TestInvalidCharacterHandling() + { + var input = File.ReadAllText(Path.Combine(TestParser.TestsDirectory, "ArgumentOutOfRangeException.md")); + _ = Markdown.ToHtml(input); + } + + [Test] + public void TestInvalidCodeEscape() + { + var input = "```**Header** "; + _ = Markdown.ToHtml(input); + } + + [Test] + public void TestEmphasisAndHtmlEntity() + { + var markdownText = "*Unlimited-Fun®*®"; + TestParser.TestSpec(markdownText, "

Unlimited-Fun®®

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

    +
    Id                                   DisplayName         Description
    +--                                   -----------         -----------
    +62375ab9-6b52-47ed-826b-58e47e0e304b Group.Unified       ...
    +
  2. +
"); + } + + [Test] + public void VisualizeMathExpressions() + { + string math = @"Math expressions + +$\frac{n!}{k!(n-k)!} = \binom{n}{k}$ + +$$\frac{n!}{k!(n-k)!} = \binom{n}{k}$$ + +$$ +\frac{n!}{k!(n-k)!} = \binom{n}{k} +$$ + +
+\begin{align} +\sqrt{37} & = \sqrt{\frac{73^2-1}{12^2}} \\ + & = \sqrt{\frac{73^2}{12^2}\cdot\frac{73^2-1}{73^2}} \\ + & = \sqrt{\frac{73^2}{12^2}}\sqrt{\frac{73^2-1}{73^2}} \\ + & = \frac{73}{12}\sqrt{1 - \frac{1}{73^2}} \\ + & \approx \frac{73}{12}\left(1 - \frac{1}{2\cdot73^2}\right) +\end{align} +
+"; + Console.WriteLine("Math Expressions:\n"); + + var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() + + + var html = Markdown.ToHtml(math, pl); + Console.WriteLine(html); + } + + [Test] + public void InlineMathExpression() + { + string math = @"Math expressions + +$\frac{n!}{k!(n-k)!} = \binom{n}{k}$ +"; + var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() + + var html = Markdown.ToHtml(math, pl); + Console.WriteLine(html); + + Assert.IsTrue(html.Contains("

\\("), "Leading bracket missing"); + Assert.IsTrue(html.Contains("\\)

"), "Trailing bracket missing"); + } + + [Test] + public void BlockMathExpression() + { + string math = @"Math expressions + +$$ +\frac{n!}{k!(n-k)!} = \binom{n}{k} +$$ +"; + var pl = new MarkdownPipelineBuilder().UseMathematics().Build(); // UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build() + + var html = Markdown.ToHtml(math, pl); + Console.WriteLine(html); + + Assert.IsTrue(html.Contains("
\n\\["), "Leading bracket missing"); + Assert.IsTrue(html.Contains("\\]
"), "Trailing bracket missing"); + } + + [Test] + public void CanDisableParsingHeadings() + { + var noHeadingsPipeline = new MarkdownPipelineBuilder().DisableHeadings().Build(); + + TestParser.TestSpec("Foo\n===", "

Foo

"); + TestParser.TestSpec("Foo\n===", "

Foo\n===

", noHeadingsPipeline); + + TestParser.TestSpec("# Heading 1", "

Heading 1

"); + TestParser.TestSpec("# Heading 1", "

# Heading 1

", noHeadingsPipeline); + + // Does not also disable link reference definitions + TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "

Foo

"); + TestParser.TestSpec("[Foo]\n\n[Foo]: bar", "

Foo

", noHeadingsPipeline); + } + + [Test] + public void CanOpenAutoLinksInNewWindow() + { + var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); + var newWindowPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { OpenInNewWindow = true }).Build(); + + TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", pipeline); + TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", newWindowPipeline); + } + + [Test] + public void CanUseHttpsPrefixForWWWAutoLinks() + { + var pipeline = new MarkdownPipelineBuilder().UseAutoLinks().Build(); + var httpsPipeline = new MarkdownPipelineBuilder().UseAutoLinks(new AutoLinkOptions() { UseHttpsForWWWLinks = true }).Build(); + + TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", pipeline); + TestParser.TestSpec("www.foo.bar", "

www.foo.bar

", httpsPipeline); + } + } +} diff --git a/src/Markdig.Tests/NormalizeSpecs/Headings.md b/src/Markdig.Tests/NormalizeSpecs/Headings.md index 551fac41..4f8344e9 100644 --- a/src/Markdig.Tests/NormalizeSpecs/Headings.md +++ b/src/Markdig.Tests/NormalizeSpecs/Headings.md @@ -1,5 +1,5 @@ -# Headings - +# Headings + ```````````````````````````````` example # Heading 1 @@ -24,8 +24,8 @@ ##### Heading 5 ###### Heading 6 -```````````````````````````````` - +```````````````````````````````` + ```````````````````````````````` example ###### Heading @@ -34,8 +34,8 @@ Text after two newlines ###### Heading Text after two newlines -```````````````````````````````` - +```````````````````````````````` + ```````````````````````````````` example Heading ======= diff --git a/src/Markdig.Tests/Specs/AbbreviationSpecs.md b/src/Markdig.Tests/Specs/AbbreviationSpecs.md index 4c88f69b..832b6b2a 100644 --- a/src/Markdig.Tests/Specs/AbbreviationSpecs.md +++ b/src/Markdig.Tests/Specs/AbbreviationSpecs.md @@ -112,9 +112,9 @@ A The longest matching abbreviation should be used ```````````````````````````````` example -*[Foo]: foo -*[Foo Bar]: foobar - +*[Foo]: foo +*[Foo Bar]: foobar + Foo B .

Foo B

diff --git a/src/Markdig.Tests/Specs/AutoLinks.md b/src/Markdig.Tests/Specs/AutoLinks.md index f8bb7bd9..b38f3dea 100644 --- a/src/Markdig.Tests/Specs/AutoLinks.md +++ b/src/Markdig.Tests/Specs/AutoLinks.md @@ -245,18 +245,18 @@ https://github.com:443 Links with unicode characters in the path / query / fragment are matched and url encoded -```````````````````````````````` example -http://abc.net/☃ - -http://abc.net?☃ - -http://abc.net#☃ - +```````````````````````````````` example +http://abc.net/☃ + +http://abc.net?☃ + +http://abc.net#☃ + http://abc.net/foo#☃ -. -

http://abc.net/☃

-

http://abc.net?☃

-

http://abc.net#☃

+. +

http://abc.net/☃

+

http://abc.net?☃

+

http://abc.net#☃

http://abc.net/foo#☃

```````````````````````````````` @@ -270,18 +270,18 @@ http://☃.net?☃ Same goes for regular autolinks -```````````````````````````````` example - - - - - - +```````````````````````````````` example + + + + + + -. -

http://abc.net/☃

-

http://abc.net?☃

-

http://abc.net#☃

+. +

http://abc.net/☃

+

http://abc.net?☃

+

http://abc.net#☃

http://abc.net/foo#☃

```````````````````````````````` diff --git a/src/Markdig.Tests/TestCustomEmojis.cs b/src/Markdig.Tests/TestCustomEmojis.cs index 984736f1..aee5ab0c 100644 --- a/src/Markdig.Tests/TestCustomEmojis.cs +++ b/src/Markdig.Tests/TestCustomEmojis.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using Markdig.Extensions.Emoji; +using System; +using System.Collections; +using System.Collections.Generic; +using Markdig.Extensions.Emoji; using NUnit.Framework; namespace Markdig.Tests { [TestFixture] public class TestCustomEmojis - { + { [Test] [TestCase(":smiley:", "

♥

\n")] [TestCase(":confused:", "

:confused:

\n")] // default emoji does not work @@ -28,13 +28,13 @@ namespace Markdig.Tests var actual = Markdown.ToHtml(input, pipeline); Assert.AreEqual(expected, actual); - } - + } + [Test] [TestCase(":testheart:", "

♥

\n")] [TestCase("hello", "

♥

\n")] [TestCase(":confused:", "

:confused:

\n")] // default emoji does not work - [TestCase(":/", "

:/

\n")] // default smiley does not work + [TestCase(":/", "

:/

\n")] // default smiley does not work public void TestCustomSmiley(string input, string expected) { var emojiToUnicode = new Dictionary(); @@ -55,8 +55,8 @@ namespace Markdig.Tests [Test] [TestCase(":smiley:", "

♥

\n")] - [TestCase(":)", "

♥

\n")] - [TestCase(":confused:", "

😕

\n")] // default emoji still works + [TestCase(":)", "

♥

\n")] + [TestCase(":confused:", "

😕

\n")] // default emoji still works [TestCase(":/", "

😕

\n")] // default smiley still works public void TestOverrideDefaultWithCustomEmoji(string input, string expected) { @@ -73,8 +73,8 @@ namespace Markdig.Tests var actual = Markdown.ToHtml(input, pipeline); Assert.AreEqual(expected, actual); - } - + } + [Test] [TestCase(":testheart:", "

♥

\n")] [TestCase("hello", "

♥

\n")] @@ -99,31 +99,31 @@ namespace Markdig.Tests } [Test] - public void TestCustomEmojiValidation() - { - var emojiToUnicode = new Dictionary(); - var smileyToEmoji = new Dictionary(); - - Assert.Throws(() => new EmojiMapping(null, smileyToEmoji)); - Assert.Throws(() => new EmojiMapping(emojiToUnicode, null)); - - emojiToUnicode.Add("null-value", null); - Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); - emojiToUnicode.Clear(); - - smileyToEmoji.Add("null-value", null); - Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); - smileyToEmoji.Clear(); - - smileyToEmoji.Add("foo", "something-that-does-not-exist-in-emojiToUnicode"); - Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); - smileyToEmoji.Clear(); - - emojiToUnicode.Add("a", "aaa"); - emojiToUnicode.Add("b", "bbb"); - emojiToUnicode.Add("c", "ccc"); - smileyToEmoji.Add("a", "c"); // "a" already exists in emojiToUnicode - Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); + public void TestCustomEmojiValidation() + { + var emojiToUnicode = new Dictionary(); + var smileyToEmoji = new Dictionary(); + + Assert.Throws(() => new EmojiMapping(null, smileyToEmoji)); + Assert.Throws(() => new EmojiMapping(emojiToUnicode, null)); + + emojiToUnicode.Add("null-value", null); + Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); + emojiToUnicode.Clear(); + + smileyToEmoji.Add("null-value", null); + Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); + smileyToEmoji.Clear(); + + smileyToEmoji.Add("foo", "something-that-does-not-exist-in-emojiToUnicode"); + Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); + smileyToEmoji.Clear(); + + emojiToUnicode.Add("a", "aaa"); + emojiToUnicode.Add("b", "bbb"); + emojiToUnicode.Add("c", "ccc"); + smileyToEmoji.Add("a", "c"); // "a" already exists in emojiToUnicode + Assert.Throws(() => new EmojiMapping(emojiToUnicode, smileyToEmoji)); } } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestDescendantsOrder.cs b/src/Markdig.Tests/TestDescendantsOrder.cs index 996e6b83..a56fd489 100644 --- a/src/Markdig.Tests/TestDescendantsOrder.cs +++ b/src/Markdig.Tests/TestDescendantsOrder.cs @@ -1,153 +1,153 @@ -using NUnit.Framework; -using Markdig.Syntax; -using Markdig.Syntax.Inlines; -using System; -using System.Linq; -using System.Collections.Generic; - -namespace Markdig.Tests -{ - [TestFixture] - public class TestDescendantsOrder - { - [Test] - public void TestSchemas() - { - foreach (var syntaxTree in TestParser.SpecsSyntaxTrees) - { - AssertIEnumerablesAreEqual( - Descendants_Legacy(syntaxTree), - syntaxTree.Descendants()); - - AssertIEnumerablesAreEqual( - syntaxTree.Descendants().OfType(), - syntaxTree.Descendants()); - - AssertIEnumerablesAreEqual( - syntaxTree.Descendants().OfType(), - (syntaxTree as ContainerBlock).Descendants()); - - AssertIEnumerablesAreEqual( - syntaxTree.Descendants().OfType(), - syntaxTree.Descendants()); - - foreach (LiteralInline literalInline in syntaxTree.Descendants()) - { - Assert.AreSame(Array.Empty(), literalInline.Descendants()); - Assert.AreSame(Array.Empty(), literalInline.Descendants()); - Assert.AreSame(Array.Empty(), literalInline.Descendants()); - } - - foreach (ContainerInline containerInline in syntaxTree.Descendants()) - { - AssertIEnumerablesAreEqual( - containerInline.FindDescendants(), - containerInline.Descendants()); - - AssertIEnumerablesAreEqual( - containerInline.FindDescendants(), - (containerInline as MarkdownObject).Descendants()); - - if (containerInline.FirstChild is null) - { - Assert.AreSame(Array.Empty(), containerInline.Descendants()); - Assert.AreSame(Array.Empty(), containerInline.FindDescendants()); - Assert.AreSame(Array.Empty(), (containerInline as MarkdownObject).Descendants()); - } - - Assert.AreSame(Array.Empty(), containerInline.Descendants()); - Assert.AreSame(Array.Empty(), containerInline.Descendants()); - } - - foreach (ParagraphBlock paragraphBlock in syntaxTree.Descendants()) - { - AssertIEnumerablesAreEqual( - (paragraphBlock as MarkdownObject).Descendants(), - paragraphBlock.Descendants()); - - Assert.AreSame(Array.Empty(), paragraphBlock.Descendants()); - } - - foreach (ContainerBlock containerBlock in syntaxTree.Descendants()) - { - AssertIEnumerablesAreEqual( - containerBlock.Descendants(), - (containerBlock as MarkdownObject).Descendants()); - - AssertIEnumerablesAreEqual( - containerBlock.Descendants(), - (containerBlock as MarkdownObject).Descendants()); - - if (containerBlock.Count == 0) - { - Assert.AreSame(Array.Empty(), containerBlock.Descendants()); - Assert.AreSame(Array.Empty(), (containerBlock as Block).Descendants()); - Assert.AreSame(Array.Empty(), (containerBlock as MarkdownObject).Descendants()); - } - } - } - } - - private static void AssertIEnumerablesAreEqual(IEnumerable first, IEnumerable second) - { - var firstList = new List(first); - var secondList = new List(second); - - Assert.AreEqual(firstList.Count, secondList.Count); - - for (int i = 0; i < firstList.Count; i++) - { - Assert.AreSame(firstList[i], secondList[i]); - } - } - - private static IEnumerable Descendants_Legacy(MarkdownObject markdownObject) - { - // TODO: implement a recursiveless method - - var block = markdownObject as ContainerBlock; - if (block != null) - { - foreach (var subBlock in block) - { - yield return subBlock; - - foreach (var sub in Descendants_Legacy(subBlock)) - { - yield return sub; - } - - // Visit leaf block that have inlines - var leafBlock = subBlock as LeafBlock; - if (leafBlock?.Inline != null) - { - foreach (var subInline in Descendants_Legacy(leafBlock.Inline)) - { - yield return subInline; - } - } - } - } - else - { - var inline = markdownObject as ContainerInline; - if (inline != null) - { - var child = inline.FirstChild; - while (child != null) - { - var next = child.NextSibling; - yield return child; - - foreach (var sub in Descendants_Legacy(child)) - { - yield return sub; - } - - child = next; - } - } - } - } - } -} +using NUnit.Framework; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestDescendantsOrder + { + [Test] + public void TestSchemas() + { + foreach (var syntaxTree in TestParser.SpecsSyntaxTrees) + { + AssertIEnumerablesAreEqual( + Descendants_Legacy(syntaxTree), + syntaxTree.Descendants()); + + AssertIEnumerablesAreEqual( + syntaxTree.Descendants().OfType(), + syntaxTree.Descendants()); + + AssertIEnumerablesAreEqual( + syntaxTree.Descendants().OfType(), + (syntaxTree as ContainerBlock).Descendants()); + + AssertIEnumerablesAreEqual( + syntaxTree.Descendants().OfType(), + syntaxTree.Descendants()); + + foreach (LiteralInline literalInline in syntaxTree.Descendants()) + { + Assert.AreSame(Array.Empty(), literalInline.Descendants()); + Assert.AreSame(Array.Empty(), literalInline.Descendants()); + Assert.AreSame(Array.Empty(), literalInline.Descendants()); + } + + foreach (ContainerInline containerInline in syntaxTree.Descendants()) + { + AssertIEnumerablesAreEqual( + containerInline.FindDescendants(), + containerInline.Descendants()); + + AssertIEnumerablesAreEqual( + containerInline.FindDescendants(), + (containerInline as MarkdownObject).Descendants()); + + if (containerInline.FirstChild is null) + { + Assert.AreSame(Array.Empty(), containerInline.Descendants()); + Assert.AreSame(Array.Empty(), containerInline.FindDescendants()); + Assert.AreSame(Array.Empty(), (containerInline as MarkdownObject).Descendants()); + } + + Assert.AreSame(Array.Empty(), containerInline.Descendants()); + Assert.AreSame(Array.Empty(), containerInline.Descendants()); + } + + foreach (ParagraphBlock paragraphBlock in syntaxTree.Descendants()) + { + AssertIEnumerablesAreEqual( + (paragraphBlock as MarkdownObject).Descendants(), + paragraphBlock.Descendants()); + + Assert.AreSame(Array.Empty(), paragraphBlock.Descendants()); + } + + foreach (ContainerBlock containerBlock in syntaxTree.Descendants()) + { + AssertIEnumerablesAreEqual( + containerBlock.Descendants(), + (containerBlock as MarkdownObject).Descendants()); + + AssertIEnumerablesAreEqual( + containerBlock.Descendants(), + (containerBlock as MarkdownObject).Descendants()); + + if (containerBlock.Count == 0) + { + Assert.AreSame(Array.Empty(), containerBlock.Descendants()); + Assert.AreSame(Array.Empty(), (containerBlock as Block).Descendants()); + Assert.AreSame(Array.Empty(), (containerBlock as MarkdownObject).Descendants()); + } + } + } + } + + private static void AssertIEnumerablesAreEqual(IEnumerable first, IEnumerable second) + { + var firstList = new List(first); + var secondList = new List(second); + + Assert.AreEqual(firstList.Count, secondList.Count); + + for (int i = 0; i < firstList.Count; i++) + { + Assert.AreSame(firstList[i], secondList[i]); + } + } + + private static IEnumerable Descendants_Legacy(MarkdownObject markdownObject) + { + // TODO: implement a recursiveless method + + var block = markdownObject as ContainerBlock; + if (block != null) + { + foreach (var subBlock in block) + { + yield return subBlock; + + foreach (var sub in Descendants_Legacy(subBlock)) + { + yield return sub; + } + + // Visit leaf block that have inlines + var leafBlock = subBlock as LeafBlock; + if (leafBlock?.Inline != null) + { + foreach (var subInline in Descendants_Legacy(leafBlock.Inline)) + { + yield return subInline; + } + } + } + } + else + { + var inline = markdownObject as ContainerInline; + if (inline != null) + { + var child = inline.FirstChild; + while (child != null) + { + var next = child.NextSibling; + yield return child; + + foreach (var sub in Descendants_Legacy(child)) + { + yield return sub; + } + + child = next; + } + } + } + } + } +} diff --git a/src/Markdig.Tests/TestEmphasisExtended.cs b/src/Markdig.Tests/TestEmphasisExtended.cs index dff35c26..3379258a 100644 --- a/src/Markdig.Tests/TestEmphasisExtended.cs +++ b/src/Markdig.Tests/TestEmphasisExtended.cs @@ -1,168 +1,168 @@ -using Markdig.Parsers.Inlines; -using Markdig.Renderers; -using Markdig.Renderers.Html; -using Markdig.Syntax.Inlines; -using NUnit.Framework; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; - -namespace Markdig.Tests -{ - [TestFixture] - public class TestEmphasisExtended - { - class EmphasisTestExtension : IMarkdownExtension - { - public void Setup(MarkdownPipelineBuilder pipeline) - { - var emphasisParser = pipeline.InlineParsers.Find(); - Debug.Assert(emphasisParser != null); - - foreach (var emphasis in EmphasisTestDescriptors) - { - emphasisParser.EmphasisDescriptors.Add( - new EmphasisDescriptor(emphasis.Character, emphasis.Minimum, emphasis.Maximum, true)); - } - emphasisParser.TryCreateEmphasisInlineList.Add((delimiterChar, delimiterCount) => - { - return delimiterChar == '*' || delimiterChar == '_' - ? null - : new CustomEmphasisInline() { DelimiterChar = delimiterChar, DelimiterCount = delimiterCount }; - }); - } - - public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) - { - renderer.ObjectRenderers.Insert(0, new EmphasisRenderer()); - } - - class EmphasisRenderer : HtmlObjectRenderer - { - protected override void Write(HtmlRenderer renderer, CustomEmphasisInline obj) - { - var tag = EmphasisTestDescriptors.First(test => test.Character == obj.DelimiterChar).Tags[obj.DelimiterCount]; - - renderer.Write(tag.OpeningTag); - renderer.WriteChildren(obj); - renderer.Write(tag.ClosingTag); - } - } - } - class Tag - { -#pragma warning disable CS0649 - public int Level; -#pragma warning restore CS0649 - public string RawTag; - public string OpeningTag; - public string ClosingTag; - - public Tag(string tag) - { - RawTag = tag; - OpeningTag = "<" + tag + ">"; - ClosingTag = ""; - } - - public static implicit operator Tag(string tag) - => new Tag(tag); - } - class EmphasisTestDescriptor - { - public char Character; - public int Minimum; - public int Maximum; - public Dictionary Tags = new Dictionary(); - - private EmphasisTestDescriptor(char character, int min, int max) - { - Character = character; - Minimum = min; - Maximum = max; - } - public EmphasisTestDescriptor(char character, int min, int max, params Tag[] tags) - : this(character, min, max) - { - Debug.Assert(tags.Length == max - min + 1); - foreach (var tag in tags) - { - Tags.Add(min++, tag); - } - } - public EmphasisTestDescriptor(char character, int min, int max, string tag) - : this(character, min, max, new Tag(tag)) { } - } - class CustomEmphasisInline : EmphasisInline { } - static readonly EmphasisTestDescriptor[] EmphasisTestDescriptors = new[] - { - // Min Max - new EmphasisTestDescriptor('"', 1, 1, "quotation"), - new EmphasisTestDescriptor(',', 1, 2, "comma", "extra-comma"), - new EmphasisTestDescriptor('!', 2, 3, "warning", "error"), - new EmphasisTestDescriptor('=', 1, 3, "equal", "really-equal", "congruent"), - new EmphasisTestDescriptor('1', 1, 1, "one-only"), - new EmphasisTestDescriptor('2', 2, 2, "two-only"), - new EmphasisTestDescriptor('3', 3, 3, "three-only"), - }; - - static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder().Use().Build(); - - [Test] - [TestCase("*foo**", "foo*")] - [TestCase("**foo*", "*foo")] - [TestCase("***foo***", "foo")] - [TestCase("**_foo_**", "foo")] - [TestCase("_**foo**_", "foo")] - [TestCase("\"foo\"", "foo")] - [TestCase("\"\"foo\"\"", "foo")] - [TestCase("\"foo\"\"", "foo"")] - [TestCase("\"\"foo\"", ""foo")] - [TestCase(", foo", ", foo")] - [TestCase(", foo,", ", foo,")] - [TestCase(",some, foo,", "some foo,")] - [TestCase(",,foo,,", "foo")] - [TestCase(",foo,,", "foo,")] - [TestCase(",,,foo,,,", "foo")] - [TestCase("!1!", "!1!")] - [TestCase("!!2!!", "2")] - [TestCase("!!!3!!!", "3")] - [TestCase("!!!34!!!!", "34!")] - [TestCase("!!!!43!!!", "!43")] - [TestCase("!!!!44!!!!", "!44!")] // This is a new case - should the second ! be before or after ? - [TestCase("!!!!!5!!!!!", "5")] - [TestCase("!!!!!!6!!!!!!", "6")] - [TestCase("!! !mixed!!!", "!! !mixed!!!")] // can't open the delimiter because of the whitespace - [TestCase("=", "=")] - [TestCase("==", "==")] - [TestCase("====", "====")] - [TestCase("=a", "=a")] - [TestCase("=a=", "a")] - [TestCase("==a=", "=a")] - [TestCase("==a==", "a")] - [TestCase("==a===", "a=")] - [TestCase("===a===", "a")] - [TestCase("====a====", "a")] - [TestCase("=====a=====", "a")] - [TestCase("1", "1")] - [TestCase("1 1", "1 1")] - [TestCase("1Foo1", "Foo")] - [TestCase("1121", "12")] - [TestCase("22322", "3")] - [TestCase("2232", "2232")] - [TestCase("333", "333")] - [TestCase("3334333", "4")] - [TestCase("33334333", "34")] - [TestCase("33343333", "43")] - [TestCase("122122", "2222")] - [TestCase("221221", "11")] - [TestCase("122foo221", "foo")] - [TestCase("122foo122", "22foo22")] - [TestCase("!!!!!Attention:!! \"==1+1== 2\",but ===333 and 222===, mod 111!!!", - "Attention: + 2but 333 and 222 mod 111")] - public void TestEmphasis(string markdown, string expectedHtml) - { - TestParser.TestSpec(markdown, "

" + expectedHtml + "

", Pipeline); - } - } -} +using Markdig.Parsers.Inlines; +using Markdig.Renderers; +using Markdig.Renderers.Html; +using Markdig.Syntax.Inlines; +using NUnit.Framework; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestEmphasisExtended + { + class EmphasisTestExtension : IMarkdownExtension + { + public void Setup(MarkdownPipelineBuilder pipeline) + { + var emphasisParser = pipeline.InlineParsers.Find(); + Debug.Assert(emphasisParser != null); + + foreach (var emphasis in EmphasisTestDescriptors) + { + emphasisParser.EmphasisDescriptors.Add( + new EmphasisDescriptor(emphasis.Character, emphasis.Minimum, emphasis.Maximum, true)); + } + emphasisParser.TryCreateEmphasisInlineList.Add((delimiterChar, delimiterCount) => + { + return delimiterChar == '*' || delimiterChar == '_' + ? null + : new CustomEmphasisInline() { DelimiterChar = delimiterChar, DelimiterCount = delimiterCount }; + }); + } + + public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) + { + renderer.ObjectRenderers.Insert(0, new EmphasisRenderer()); + } + + class EmphasisRenderer : HtmlObjectRenderer + { + protected override void Write(HtmlRenderer renderer, CustomEmphasisInline obj) + { + var tag = EmphasisTestDescriptors.First(test => test.Character == obj.DelimiterChar).Tags[obj.DelimiterCount]; + + renderer.Write(tag.OpeningTag); + renderer.WriteChildren(obj); + renderer.Write(tag.ClosingTag); + } + } + } + class Tag + { +#pragma warning disable CS0649 + public int Level; +#pragma warning restore CS0649 + public string RawTag; + public string OpeningTag; + public string ClosingTag; + + public Tag(string tag) + { + RawTag = tag; + OpeningTag = "<" + tag + ">"; + ClosingTag = ""; + } + + public static implicit operator Tag(string tag) + => new Tag(tag); + } + class EmphasisTestDescriptor + { + public char Character; + public int Minimum; + public int Maximum; + public Dictionary Tags = new Dictionary(); + + private EmphasisTestDescriptor(char character, int min, int max) + { + Character = character; + Minimum = min; + Maximum = max; + } + public EmphasisTestDescriptor(char character, int min, int max, params Tag[] tags) + : this(character, min, max) + { + Debug.Assert(tags.Length == max - min + 1); + foreach (var tag in tags) + { + Tags.Add(min++, tag); + } + } + public EmphasisTestDescriptor(char character, int min, int max, string tag) + : this(character, min, max, new Tag(tag)) { } + } + class CustomEmphasisInline : EmphasisInline { } + static readonly EmphasisTestDescriptor[] EmphasisTestDescriptors = new[] + { + // Min Max + new EmphasisTestDescriptor('"', 1, 1, "quotation"), + new EmphasisTestDescriptor(',', 1, 2, "comma", "extra-comma"), + new EmphasisTestDescriptor('!', 2, 3, "warning", "error"), + new EmphasisTestDescriptor('=', 1, 3, "equal", "really-equal", "congruent"), + new EmphasisTestDescriptor('1', 1, 1, "one-only"), + new EmphasisTestDescriptor('2', 2, 2, "two-only"), + new EmphasisTestDescriptor('3', 3, 3, "three-only"), + }; + + static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder().Use().Build(); + + [Test] + [TestCase("*foo**", "foo*")] + [TestCase("**foo*", "*foo")] + [TestCase("***foo***", "foo")] + [TestCase("**_foo_**", "foo")] + [TestCase("_**foo**_", "foo")] + [TestCase("\"foo\"", "foo")] + [TestCase("\"\"foo\"\"", "foo")] + [TestCase("\"foo\"\"", "foo"")] + [TestCase("\"\"foo\"", ""foo")] + [TestCase(", foo", ", foo")] + [TestCase(", foo,", ", foo,")] + [TestCase(",some, foo,", "some foo,")] + [TestCase(",,foo,,", "foo")] + [TestCase(",foo,,", "foo,")] + [TestCase(",,,foo,,,", "foo")] + [TestCase("!1!", "!1!")] + [TestCase("!!2!!", "2")] + [TestCase("!!!3!!!", "3")] + [TestCase("!!!34!!!!", "34!")] + [TestCase("!!!!43!!!", "!43")] + [TestCase("!!!!44!!!!", "!44!")] // This is a new case - should the second ! be before or after ? + [TestCase("!!!!!5!!!!!", "5")] + [TestCase("!!!!!!6!!!!!!", "6")] + [TestCase("!! !mixed!!!", "!! !mixed!!!")] // can't open the delimiter because of the whitespace + [TestCase("=", "=")] + [TestCase("==", "==")] + [TestCase("====", "====")] + [TestCase("=a", "=a")] + [TestCase("=a=", "a")] + [TestCase("==a=", "=a")] + [TestCase("==a==", "a")] + [TestCase("==a===", "a=")] + [TestCase("===a===", "a")] + [TestCase("====a====", "a")] + [TestCase("=====a=====", "a")] + [TestCase("1", "1")] + [TestCase("1 1", "1 1")] + [TestCase("1Foo1", "Foo")] + [TestCase("1121", "12")] + [TestCase("22322", "3")] + [TestCase("2232", "2232")] + [TestCase("333", "333")] + [TestCase("3334333", "4")] + [TestCase("33334333", "34")] + [TestCase("33343333", "43")] + [TestCase("122122", "2222")] + [TestCase("221221", "11")] + [TestCase("122foo221", "foo")] + [TestCase("122foo122", "22foo22")] + [TestCase("!!!!!Attention:!! \"==1+1== 2\",but ===333 and 222===, mod 111!!!", + "Attention: + 2but 333 and 222 mod 111")] + public void TestEmphasis(string markdown, string expectedHtml) + { + TestParser.TestSpec(markdown, "

" + expectedHtml + "

", Pipeline); + } + } +} diff --git a/src/Markdig.Tests/TestEmphasisExtraOptions.cs b/src/Markdig.Tests/TestEmphasisExtraOptions.cs index 7920402a..7da3fec7 100644 --- a/src/Markdig.Tests/TestEmphasisExtraOptions.cs +++ b/src/Markdig.Tests/TestEmphasisExtraOptions.cs @@ -1,45 +1,45 @@ -using NUnit.Framework; -using Markdig.Extensions.EmphasisExtras; - -namespace Markdig.Tests -{ - [TestFixture] - public class TestEmphasisExtraOptions - { - [Test] - public void OnlyStrikethrough_Single() - { - TestParser.TestSpec("~foo~", "

~foo~

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); - } - - [Test] - public void OnlyStrikethrough_Double() - { - TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); - } - - [Test] - public void OnlySubscript_Single() - { - TestParser.TestSpec("~foo~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); - } - - [Test] - public void OnlySubscript_Double() - { - TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); - } - - [Test] - public void SubscriptAndStrikethrough_Single() - { - TestParser.TestSpec("~foo~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); - } - - [Test] - public void SubscriptAndStrikethrough_Double() - { - TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); - } - } +using NUnit.Framework; +using Markdig.Extensions.EmphasisExtras; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestEmphasisExtraOptions + { + [Test] + public void OnlyStrikethrough_Single() + { + TestParser.TestSpec("~foo~", "

~foo~

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); + } + + [Test] + public void OnlyStrikethrough_Double() + { + TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build()); + } + + [Test] + public void OnlySubscript_Single() + { + TestParser.TestSpec("~foo~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); + } + + [Test] + public void OnlySubscript_Double() + { + TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Subscript).Build()); + } + + [Test] + public void SubscriptAndStrikethrough_Single() + { + TestParser.TestSpec("~foo~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); + } + + [Test] + public void SubscriptAndStrikethrough_Double() + { + TestParser.TestSpec("~~foo~~", "

foo

", new MarkdownPipelineBuilder().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough | EmphasisExtraOptions.Subscript).Build()); + } + } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestImageAltText.cs b/src/Markdig.Tests/TestImageAltText.cs index db662603..f8a0ad2e 100644 --- a/src/Markdig.Tests/TestImageAltText.cs +++ b/src/Markdig.Tests/TestImageAltText.cs @@ -1,25 +1,25 @@ -using NUnit.Framework; -using System.Text.RegularExpressions; - -namespace Markdig.Tests -{ - [TestFixture] - public class TestImageAltText - { - [Test] - [TestCase("![](image.jpg)", "")] - [TestCase("![foo](image.jpg)", "foo")] - [TestCase("![][1]\n\n[1]: image.jpg", "")] - [TestCase("![bar][1]\n\n[1]: image.jpg", "bar")] - [TestCase("![](image.jpg 'title')", "")] - [TestCase("![foo](image.jpg 'title')", "foo")] - [TestCase("![][1]\n\n[1]: image.jpg 'title'", "")] - [TestCase("![bar][1]\n\n[1]: image.jpg 'title'", "bar")] - public void TestImageHtmlAltText(string markdown, string expectedAltText) - { - string html = Markdown.ToHtml(markdown); - string actualAltText = Regex.Match(html, "alt=\"(.*?)\"").Groups[1].Value; - Assert.AreEqual(expectedAltText, actualAltText); - } - } -} +using NUnit.Framework; +using System.Text.RegularExpressions; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestImageAltText + { + [Test] + [TestCase("![](image.jpg)", "")] + [TestCase("![foo](image.jpg)", "foo")] + [TestCase("![][1]\n\n[1]: image.jpg", "")] + [TestCase("![bar][1]\n\n[1]: image.jpg", "bar")] + [TestCase("![](image.jpg 'title')", "")] + [TestCase("![foo](image.jpg 'title')", "foo")] + [TestCase("![][1]\n\n[1]: image.jpg 'title'", "")] + [TestCase("![bar][1]\n\n[1]: image.jpg 'title'", "bar")] + public void TestImageHtmlAltText(string markdown, string expectedAltText) + { + string html = Markdown.ToHtml(markdown); + string actualAltText = Regex.Match(html, "alt=\"(.*?)\"").Groups[1].Value; + Assert.AreEqual(expectedAltText, actualAltText); + } + } +} diff --git a/src/Markdig.Tests/TestLinkHelper.cs b/src/Markdig.Tests/TestLinkHelper.cs index 9d67dd1f..c6fab951 100644 --- a/src/Markdig.Tests/TestLinkHelper.cs +++ b/src/Markdig.Tests/TestLinkHelper.cs @@ -327,9 +327,9 @@ namespace Markdig.Tests [TestCase("[HTML], [S5], or [RTF]?", "html-s5-or-rtf")] [TestCase("3. Applications", "3-applications")] [TestCase("33", "33")] - public void TestUrilizeGfm(string input, string expectedResult) - { - Assert.AreEqual(expectedResult, LinkHelper.UrilizeAsGfm(input)); + public void TestUrilizeGfm(string input, string expectedResult) + { + Assert.AreEqual(expectedResult, LinkHelper.UrilizeAsGfm(input)); } [TestCase("abc", "abc")] @@ -421,9 +421,9 @@ namespace Markdig.Tests } [Test] - public void TestUnicodeInDomainNameOfLinkReferenceDefinition() - { - TestParser.TestSpec("[Foo]\n\n[Foo]: http://ünicode.com", "

Foo

"); + public void TestUnicodeInDomainNameOfLinkReferenceDefinition() + { + TestParser.TestSpec("[Foo]\n\n[Foo]: http://ünicode.com", "

Foo

"); } } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestParser.cs b/src/Markdig.Tests/TestParser.cs index b99e8ec0..d04b17c3 100644 --- a/src/Markdig.Tests/TestParser.cs +++ b/src/Markdig.Tests/TestParser.cs @@ -1,190 +1,190 @@ -// 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.IO; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using Markdig.Extensions.JiraLinks; -using Markdig.Syntax; -using NUnit.Framework; - -namespace Markdig.Tests -{ - public class TestParser - { - [Test] - public void EnsureSpecsAreUpToDate() - { - // In CI, SpecFileGen is guaranteed to run - if (IsContinuousIntegration) - return; - - foreach (var specFilePath in SpecsFilePaths) - { - string testFilePath = Path.ChangeExtension(specFilePath, ".generated.cs"); - - Assert.True(File.Exists(testFilePath), - "A new specification file has been added. Add the spec to the list in SpecFileGen and regenerate the tests."); - - DateTime specTime = File.GetLastWriteTimeUtc(specFilePath); - DateTime testTime = File.GetLastWriteTimeUtc(testFilePath); - - // If file creation times aren't preserved by git, add some leeway - // If specs have come from git, assume that they were regenerated since CI would fail otherwise - testTime = testTime.AddMinutes(3); - - // This might not catch a changed spec every time, but should at least sometimes. Otherwise CI will catch it - - // This could also trigger, if a user has modified the spec file but reverted the change - can't think of a good workaround - Assert.Less(specTime, testTime, - $"{Path.GetFileName(specFilePath)} has been modified. Run SpecFileGen to regenerate the tests. " + - "If you have modified a specification file, but reverted all changes, ignore this error or revert the 'changed' timestamp metadata on the file."); - } - } - - public static void TestSpec(string inputText, string expectedOutputText, string extensions = null, bool plainText = false) - { - foreach (var pipeline in GetPipeline(extensions)) - { - Console.WriteLine($"Pipeline configured with extensions: {pipeline.Key}"); - TestSpec(inputText, expectedOutputText, pipeline.Value, plainText); - } - } - - public static void TestSpec(string inputText, string expectedOutputText, MarkdownPipeline pipeline, bool plainText = false) - { - // Uncomment this line to get more debug information for process inlines. - //pipeline.DebugLog = Console.Out; - var result = plainText ? Markdown.ToPlainText(inputText, pipeline) : Markdown.ToHtml(inputText, pipeline); - - result = Compact(result); - expectedOutputText = Compact(expectedOutputText); - - PrintAssertExpected(inputText, result, expectedOutputText); - } - - public static void PrintAssertExpected(string source, string result, string expected) - { - Console.WriteLine("```````````````````Source"); - Console.WriteLine(DisplaySpaceAndTabs(source)); - Console.WriteLine("```````````````````Result"); - Console.WriteLine(DisplaySpaceAndTabs(result)); - Console.WriteLine("```````````````````Expected"); - Console.WriteLine(DisplaySpaceAndTabs(expected)); - Console.WriteLine("```````````````````"); - Console.WriteLine(); - TextAssert.AreEqual(expected, result); - } - - public static IEnumerable> GetPipeline(string extensionsGroupText) - { - // For the standard case, we make sure that both the CommmonMark core and Extra/Advanced are CommonMark compliant! - if (string.IsNullOrEmpty(extensionsGroupText)) - { - yield return new KeyValuePair("default", new MarkdownPipelineBuilder().Build()); - - yield return new KeyValuePair("advanced", new MarkdownPipelineBuilder() // Use similar to advanced extension without auto-identifier - .UseAbbreviations() - //.UseAutoIdentifiers() - .UseCitations() - .UseCustomContainers() - .UseDefinitionLists() - .UseEmphasisExtras() - .UseFigures() - .UseFooters() - .UseFootnotes() - .UseGridTables() - .UseMathematics() - .UseMediaLinks() - .UsePipeTables() - .UseListExtras() - .UseGenericAttributes().Build()); - - yield break; - } - - var extensionGroups = extensionsGroupText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); - foreach (var extensionsText in extensionGroups) - { - var builder = new MarkdownPipelineBuilder(); - builder.DebugLog = Console.Out; - if (extensionsText == "jiralinks") - { - builder.UseJiraLinks(new JiraLinkOptions("http://your.company.abc")); - } - else - { - builder = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText); - } - yield return new KeyValuePair(extensionsText, builder.Build()); - } - } - - public static string DisplaySpaceAndTabs(string text) - { - // Output special characters to check correctly the results - return text.Replace('\t', '→').Replace(' ', '·'); - } - - private static string Compact(string html) - { - // Normalize the output to make it compatible with CommonMark specs - html = html.Replace("\r\n", "\n").Replace(@"\r", @"\n").Trim(); - html = Regex.Replace(html, @"\s+", ""); - html = Regex.Replace(html, @"
  • \s+", "
  • "); - html = html.Normalize(NormalizationForm.FormKD); - return html; - } - - public static readonly bool IsContinuousIntegration = Environment.GetEnvironmentVariable("CI") != null; - - public static readonly string TestsDirectory = - Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(TestParser).Assembly.Location), "../../..")); - - /// - /// Contains absolute paths to specification markdown files (order is the same as in ) - /// - public static readonly string[] SpecsFilePaths; - /// - /// Contains the markdown source for specification files (order is the same as in ) - /// - public static readonly string[] SpecsMarkdown; - /// - /// Contains the markdown syntax tree for specification files (order is the same as in ) - /// - public static readonly MarkdownDocument[] SpecsSyntaxTrees; - - static TestParser() - { - const string RunningInsideVisualStudioPath = "\\src\\.vs\\markdig\\"; - int index = TestsDirectory.IndexOf(RunningInsideVisualStudioPath); - if (index != -1) - { - TestsDirectory = TestsDirectory.Substring(0, index) + "\\src\\Markdig.Tests"; - } - - SpecsFilePaths = Directory.GetDirectories(TestsDirectory) - .Where(dir => dir.EndsWith("Specs")) - .SelectMany(dir => Directory.GetFiles(dir) - .Where(file => file.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) - .Where(file => file.IndexOf("readme", StringComparison.OrdinalIgnoreCase) == -1)) - .ToArray(); - - SpecsMarkdown = new string[SpecsFilePaths.Length]; - SpecsSyntaxTrees = new MarkdownDocument[SpecsFilePaths.Length]; - - var pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .Build(); - - for (int i = 0; i < SpecsFilePaths.Length; i++) - { - string markdown = SpecsMarkdown[i] = File.ReadAllText(SpecsFilePaths[i]); - SpecsSyntaxTrees[i] = Markdown.Parse(markdown, pipeline); - } - } - } +// 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.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using Markdig.Extensions.JiraLinks; +using Markdig.Syntax; +using NUnit.Framework; + +namespace Markdig.Tests +{ + public class TestParser + { + [Test] + public void EnsureSpecsAreUpToDate() + { + // In CI, SpecFileGen is guaranteed to run + if (IsContinuousIntegration) + return; + + foreach (var specFilePath in SpecsFilePaths) + { + string testFilePath = Path.ChangeExtension(specFilePath, ".generated.cs"); + + Assert.True(File.Exists(testFilePath), + "A new specification file has been added. Add the spec to the list in SpecFileGen and regenerate the tests."); + + DateTime specTime = File.GetLastWriteTimeUtc(specFilePath); + DateTime testTime = File.GetLastWriteTimeUtc(testFilePath); + + // If file creation times aren't preserved by git, add some leeway + // If specs have come from git, assume that they were regenerated since CI would fail otherwise + testTime = testTime.AddMinutes(3); + + // This might not catch a changed spec every time, but should at least sometimes. Otherwise CI will catch it + + // This could also trigger, if a user has modified the spec file but reverted the change - can't think of a good workaround + Assert.Less(specTime, testTime, + $"{Path.GetFileName(specFilePath)} has been modified. Run SpecFileGen to regenerate the tests. " + + "If you have modified a specification file, but reverted all changes, ignore this error or revert the 'changed' timestamp metadata on the file."); + } + } + + public static void TestSpec(string inputText, string expectedOutputText, string extensions = null, bool plainText = false) + { + foreach (var pipeline in GetPipeline(extensions)) + { + Console.WriteLine($"Pipeline configured with extensions: {pipeline.Key}"); + TestSpec(inputText, expectedOutputText, pipeline.Value, plainText); + } + } + + public static void TestSpec(string inputText, string expectedOutputText, MarkdownPipeline pipeline, bool plainText = false) + { + // Uncomment this line to get more debug information for process inlines. + //pipeline.DebugLog = Console.Out; + var result = plainText ? Markdown.ToPlainText(inputText, pipeline) : Markdown.ToHtml(inputText, pipeline); + + result = Compact(result); + expectedOutputText = Compact(expectedOutputText); + + PrintAssertExpected(inputText, result, expectedOutputText); + } + + public static void PrintAssertExpected(string source, string result, string expected) + { + Console.WriteLine("```````````````````Source"); + Console.WriteLine(DisplaySpaceAndTabs(source)); + Console.WriteLine("```````````````````Result"); + Console.WriteLine(DisplaySpaceAndTabs(result)); + Console.WriteLine("```````````````````Expected"); + Console.WriteLine(DisplaySpaceAndTabs(expected)); + Console.WriteLine("```````````````````"); + Console.WriteLine(); + TextAssert.AreEqual(expected, result); + } + + public static IEnumerable> GetPipeline(string extensionsGroupText) + { + // For the standard case, we make sure that both the CommmonMark core and Extra/Advanced are CommonMark compliant! + if (string.IsNullOrEmpty(extensionsGroupText)) + { + yield return new KeyValuePair("default", new MarkdownPipelineBuilder().Build()); + + yield return new KeyValuePair("advanced", new MarkdownPipelineBuilder() // Use similar to advanced extension without auto-identifier + .UseAbbreviations() + //.UseAutoIdentifiers() + .UseCitations() + .UseCustomContainers() + .UseDefinitionLists() + .UseEmphasisExtras() + .UseFigures() + .UseFooters() + .UseFootnotes() + .UseGridTables() + .UseMathematics() + .UseMediaLinks() + .UsePipeTables() + .UseListExtras() + .UseGenericAttributes().Build()); + + yield break; + } + + var extensionGroups = extensionsGroupText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); + foreach (var extensionsText in extensionGroups) + { + var builder = new MarkdownPipelineBuilder(); + builder.DebugLog = Console.Out; + if (extensionsText == "jiralinks") + { + builder.UseJiraLinks(new JiraLinkOptions("http://your.company.abc")); + } + else + { + builder = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText); + } + yield return new KeyValuePair(extensionsText, builder.Build()); + } + } + + public static string DisplaySpaceAndTabs(string text) + { + // Output special characters to check correctly the results + return text.Replace('\t', '→').Replace(' ', '·'); + } + + private static string Compact(string html) + { + // Normalize the output to make it compatible with CommonMark specs + html = html.Replace("\r\n", "\n").Replace(@"\r", @"\n").Trim(); + html = Regex.Replace(html, @"\s+
  • ", ""); + html = Regex.Replace(html, @"
  • \s+", "
  • "); + html = html.Normalize(NormalizationForm.FormKD); + return html; + } + + public static readonly bool IsContinuousIntegration = Environment.GetEnvironmentVariable("CI") != null; + + public static readonly string TestsDirectory = + Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(TestParser).Assembly.Location), "../../..")); + + /// + /// Contains absolute paths to specification markdown files (order is the same as in ) + /// + public static readonly string[] SpecsFilePaths; + /// + /// Contains the markdown source for specification files (order is the same as in ) + /// + public static readonly string[] SpecsMarkdown; + /// + /// Contains the markdown syntax tree for specification files (order is the same as in ) + /// + public static readonly MarkdownDocument[] SpecsSyntaxTrees; + + static TestParser() + { + const string RunningInsideVisualStudioPath = "\\src\\.vs\\markdig\\"; + int index = TestsDirectory.IndexOf(RunningInsideVisualStudioPath); + if (index != -1) + { + TestsDirectory = TestsDirectory.Substring(0, index) + "\\src\\Markdig.Tests"; + } + + SpecsFilePaths = Directory.GetDirectories(TestsDirectory) + .Where(dir => dir.EndsWith("Specs")) + .SelectMany(dir => Directory.GetFiles(dir) + .Where(file => file.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) + .Where(file => file.IndexOf("readme", StringComparison.OrdinalIgnoreCase) == -1)) + .ToArray(); + + SpecsMarkdown = new string[SpecsFilePaths.Length]; + SpecsSyntaxTrees = new MarkdownDocument[SpecsFilePaths.Length]; + + var pipeline = new MarkdownPipelineBuilder() + .UseAdvancedExtensions() + .Build(); + + for (int i = 0; i < SpecsFilePaths.Length; i++) + { + string markdown = SpecsMarkdown[i] = File.ReadAllText(SpecsFilePaths[i]); + SpecsSyntaxTrees[i] = Markdown.Parse(markdown, pipeline); + } + } + } } \ No newline at end of file diff --git a/src/Markdig.Tests/TestPlainText.cs b/src/Markdig.Tests/TestPlainText.cs index a2982185..57be76de 100644 --- a/src/Markdig.Tests/TestPlainText.cs +++ b/src/Markdig.Tests/TestPlainText.cs @@ -35,9 +35,9 @@ namespace Markdig.Tests TestParser.TestSpec(markdownText, expected, extensions, plainText: true); } - public static void TestSpec(string markdownText, string expected, string extensions) - { - TestParser.TestSpec(markdownText, expected, extensions, plainText: true); + public static void TestSpec(string markdownText, string expected, string extensions) + { + TestParser.TestSpec(markdownText, expected, extensions, plainText: true); } } } diff --git a/src/Markdig.Tests/TestSourcePosition.cs b/src/Markdig.Tests/TestSourcePosition.cs index de3d938e..55bd8199 100644 --- a/src/Markdig.Tests/TestSourcePosition.cs +++ b/src/Markdig.Tests/TestSourcePosition.cs @@ -140,48 +140,48 @@ literal ( 0, 2) 2-3 emphasis ( 0, 4) 4-9 literal ( 0, 6) 6-7 "); - } - + } + [Test] - public void TestFootnoteLinkReferenceDefinition() + public void TestFootnoteLinkReferenceDefinition() { // 01 2 345678 var footnote = Markdown.Parse("0\n\n [^1]:", new MarkdownPipelineBuilder().UsePreciseSourceLocation().UseFootnotes().Build()).Descendants().FirstOrDefault(); - Assert.NotNull(footnote); - - Assert.AreEqual(2, footnote.Line); + Assert.NotNull(footnote); + + Assert.AreEqual(2, footnote.Line); Assert.AreEqual(new SourceSpan(4, 7), footnote.Span); - Assert.AreEqual(new SourceSpan(5, 6), footnote.LabelSpan); - } - + Assert.AreEqual(new SourceSpan(5, 6), footnote.LabelSpan); + } + [Test] - public void TestLinkReferenceDefinition1() + public void TestLinkReferenceDefinition1() { // 0 1 // 0123456789012345 var link = Markdown.Parse("[234]: /56 'yo' ", new MarkdownPipelineBuilder().UsePreciseSourceLocation().Build()).Descendants().FirstOrDefault(); - Assert.NotNull(link); - - Assert.AreEqual(0, link.Line); + Assert.NotNull(link); + + Assert.AreEqual(0, link.Line); Assert.AreEqual(new SourceSpan(0, 14), link.Span); Assert.AreEqual(new SourceSpan(1, 3), link.LabelSpan); Assert.AreEqual(new SourceSpan(7, 9), link.UrlSpan); - Assert.AreEqual(new SourceSpan(11, 14), link.TitleSpan); + Assert.AreEqual(new SourceSpan(11, 14), link.TitleSpan); } [Test] - public void TestLinkReferenceDefinition2() + public void TestLinkReferenceDefinition2() { // 0 1 // 01 2 34567890123456789 var link = Markdown.Parse("0\n\n [234]: /56 'yo' ", new MarkdownPipelineBuilder().UsePreciseSourceLocation().Build()).Descendants().FirstOrDefault(); - Assert.NotNull(link); - - Assert.AreEqual(2, link.Line); + Assert.NotNull(link); + + Assert.AreEqual(2, link.Line); Assert.AreEqual(new SourceSpan(4, 18), link.Span); Assert.AreEqual(new SourceSpan(5, 7), link.LabelSpan); Assert.AreEqual(new SourceSpan(11, 13), link.UrlSpan); - Assert.AreEqual(new SourceSpan(15, 18), link.TitleSpan); + Assert.AreEqual(new SourceSpan(15, 18), link.TitleSpan); } [Test] @@ -395,42 +395,42 @@ listitem ( 1, 0) 4-6 paragraph ( 1, 2) 6-6 literal ( 1, 2) 6-6 "); - } - + } + [Test] - public void TestListBlock2() + public void TestListBlock2() { string test = @" -1. Foo -9. Bar -5. Foo -6. Bar +1. Foo +9. Bar +5. Foo +6. Bar 987123. FooBar"; test = test.Replace("\r\n", "\n"); var list = Markdown.Parse(test, new MarkdownPipelineBuilder().UsePreciseSourceLocation().Build()).Descendants().FirstOrDefault(); - Assert.NotNull(list); - - Assert.AreEqual(1, list.Line); - Assert.True(list.IsOrdered); - List items = list.Cast().ToList(); - Assert.AreEqual(5, items.Count); - - // Test orders - Assert.AreEqual(1, items[0].Order); + Assert.NotNull(list); + + Assert.AreEqual(1, list.Line); + Assert.True(list.IsOrdered); + List items = list.Cast().ToList(); + Assert.AreEqual(5, items.Count); + + // Test orders + Assert.AreEqual(1, items[0].Order); Assert.AreEqual(9, items[1].Order); Assert.AreEqual(5, items[2].Order); Assert.AreEqual(6, items[3].Order); - Assert.AreEqual(987123, items[4].Order); - - // Test positions - for (int i = 0; i < 4; i++) - { - Assert.AreEqual(i + 1, items[i].Line); - Assert.AreEqual(1 + (i * 7), items[i].Span.Start); - Assert.AreEqual(6, items[i].Span.Length); - } - Assert.AreEqual(5, items[4].Line); - Assert.AreEqual(new SourceSpan(29, 42), items[4].Span); + Assert.AreEqual(987123, items[4].Order); + + // Test positions + for (int i = 0; i < 4; i++) + { + Assert.AreEqual(i + 1, items[i].Line); + Assert.AreEqual(1 + (i * 7), items[i].Span.Start); + Assert.AreEqual(6, items[i].Span.Length); + } + Assert.AreEqual(5, items[4].Line); + Assert.AreEqual(new SourceSpan(29, 42), items[4].Span); } [Test] diff --git a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs index 213d1c9c..9cfecc94 100644 --- a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs +++ b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs @@ -1,7 +1,7 @@ // 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; using System.Collections.Generic; using Markdig.Helpers; using Markdig.Parsers; @@ -99,29 +99,29 @@ namespace Markdig.Extensions.Abbreviations var text = content.Text; for (int i = content.Start; i <= content.End; i++) - { - // Abbreviation must be a whole word == start at the start of a line or after a whitespace - if (i != 0) - { - for (i = i - 1; i <= content.End; i++) - { - if (text[i].IsWhitespace()) - { - i++; - goto ValidAbbreviationStart; - } - } - break; - } - + { + // Abbreviation must be a whole word == start at the start of a line or after a whitespace + if (i != 0) + { + for (i = i - 1; i <= content.End; i++) + { + if (text[i].IsWhitespace()) + { + i++; + goto ValidAbbreviationStart; + } + } + break; + } + ValidAbbreviationStart:; if (prefixTree.TryMatchLongest(text.AsSpan(i, content.End - i + 1), out KeyValuePair abbreviationMatch)) { var match = abbreviationMatch.Key; - if (!IsValidAbbreviationEnding(match, content, i)) + if (!IsValidAbbreviationEnding(match, content, i)) { - continue; + continue; } var indexAfterMatch = i + match.Length; @@ -151,7 +151,7 @@ namespace Markdig.Extensions.Abbreviations // Append the previous literal if (i > content.Start && literal.Parent == null) - { + { container.AppendChild(literal); } diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs index 961a8517..46782bf3 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs @@ -1,214 +1,214 @@ -// 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.IO; -using Markdig.Helpers; -using Markdig.Parsers; -using Markdig.Renderers; -using Markdig.Renderers.Html; -using Markdig.Syntax; -using Markdig.Syntax.Inlines; - -namespace Markdig.Extensions.AutoIdentifiers -{ - /// - /// The auto-identifier extension - /// - /// - public class AutoIdentifierExtension : IMarkdownExtension - { - private const string AutoIdentifierKey = "AutoIdentifier"; - private readonly AutoIdentifierOptions options; - private readonly StripRendererCache rendererCache = new StripRendererCache(); - - /// - /// Initializes a new instance of the class. - /// - /// The options. - public AutoIdentifierExtension(AutoIdentifierOptions options) - { - this.options = options; - } - - public void Setup(MarkdownPipelineBuilder pipeline) - { - var headingBlockParser = pipeline.BlockParsers.Find(); - if (headingBlockParser != null) - { - // Install a hook on the HeadingBlockParser when a HeadingBlock is actually processed - headingBlockParser.Closed -= HeadingBlockParser_Closed; - headingBlockParser.Closed += HeadingBlockParser_Closed; - } - var paragraphBlockParser = pipeline.BlockParsers.FindExact(); - if (paragraphBlockParser != null) - { - // Install a hook on the ParagraphBlockParser when a HeadingBlock is actually processed as a Setex heading - paragraphBlockParser.Closed -= HeadingBlockParser_Closed; - paragraphBlockParser.Closed += HeadingBlockParser_Closed; - } - } - - public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) - { - } - - /// - /// Process on a new - /// - /// The processor. - /// The heading block. - private void HeadingBlockParser_Closed(BlockProcessor processor, Block block) - { - // We may have a ParagraphBlock here as we have a hook on the ParagraphBlockParser - var headingBlock = block as HeadingBlock; - if (headingBlock == null) - { - return; - } - - // If the AutoLink options is set, we register a LinkReferenceDefinition at the document level - if ((options & AutoIdentifierOptions.AutoLink) != 0) - { - var headingLine = headingBlock.Lines.Lines[0]; - - var text = headingLine.ToString(); - - var linkRef = new HeadingLinkReferenceDefinition() - { - Heading = headingBlock, - CreateLinkInline = CreateLinkInlineForHeading - }; - - 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 around - 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. - /// - private Inline CreateLinkInlineForHeading(InlineProcessor inlineState, LinkReferenceDefinition linkRef, Inline child) - { - var headingRef = (HeadingLinkReferenceDefinition) linkRef; - return new LinkInline() - { - // Use GetDynamicUrl to allow late binding of the Url (as a link may occur before the heading is declared and - // the inlines of the heading are actually processed by HeadingBlock_ProcessInlinesEnd) - GetDynamicUrl = () => HtmlHelper.Unescape("#" + headingRef.Heading.GetAttributes().Id), - Title = HtmlHelper.Unescape(linkRef.Title), - }; - } - - /// - /// Process the inlines of the heading to create a unique identifier - /// - /// The processor. - /// The inline. - private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline inline) - { - var identifiers = processor.Document.GetData(AutoIdentifierKey) as HashSet; - if (identifiers == null) - { - identifiers = new HashSet(); - processor.Document.SetData(AutoIdentifierKey, identifiers); - } - - var headingBlock = (HeadingBlock) processor.Block; - if (headingBlock.Inline == null) - { - return; - } - - // If id is already set, don't try to modify it - var attributes = processor.Block.GetAttributes(); - if (attributes.Id != null) - { - return; - } - - // Use internally a HtmlRenderer to strip links from a heading - var stripRenderer = rendererCache.Get(); - - stripRenderer.Render(headingBlock.Inline); - var headingText = stripRenderer.Writer.ToString(); - rendererCache.Release(stripRenderer); - - // 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(); - while (!identifiers.Add(headingId)) - { - index++; - headingBuffer.Append(baseHeadingId); - headingBuffer.Append('-'); - headingBuffer.Append(index); - headingId = headingBuffer.ToString(); - headingBuffer.Length = 0; - } - - attributes.Id = headingId; - } - - private sealed class StripRendererCache : ObjectCache - { - protected override HtmlRenderer NewInstance() - { - var headingWriter = new StringWriter(); - var stripRenderer = new HtmlRenderer(headingWriter) - { - // Set to false both to avoid having any HTML tags in the output - EnableHtmlForInline = false, - EnableHtmlEscape = false - }; - return stripRenderer; - } - - protected override void Reset(HtmlRenderer instance) - { - instance.Reset(); - } - } - } +// 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.IO; +using Markdig.Helpers; +using Markdig.Parsers; +using Markdig.Renderers; +using Markdig.Renderers.Html; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; + +namespace Markdig.Extensions.AutoIdentifiers +{ + /// + /// The auto-identifier extension + /// + /// + public class AutoIdentifierExtension : IMarkdownExtension + { + private const string AutoIdentifierKey = "AutoIdentifier"; + private readonly AutoIdentifierOptions options; + private readonly StripRendererCache rendererCache = new StripRendererCache(); + + /// + /// Initializes a new instance of the class. + /// + /// The options. + public AutoIdentifierExtension(AutoIdentifierOptions options) + { + this.options = options; + } + + public void Setup(MarkdownPipelineBuilder pipeline) + { + var headingBlockParser = pipeline.BlockParsers.Find(); + if (headingBlockParser != null) + { + // Install a hook on the HeadingBlockParser when a HeadingBlock is actually processed + headingBlockParser.Closed -= HeadingBlockParser_Closed; + headingBlockParser.Closed += HeadingBlockParser_Closed; + } + var paragraphBlockParser = pipeline.BlockParsers.FindExact(); + if (paragraphBlockParser != null) + { + // Install a hook on the ParagraphBlockParser when a HeadingBlock is actually processed as a Setex heading + paragraphBlockParser.Closed -= HeadingBlockParser_Closed; + paragraphBlockParser.Closed += HeadingBlockParser_Closed; + } + } + + public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) + { + } + + /// + /// Process on a new + /// + /// The processor. + /// The heading block. + private void HeadingBlockParser_Closed(BlockProcessor processor, Block block) + { + // We may have a ParagraphBlock here as we have a hook on the ParagraphBlockParser + var headingBlock = block as HeadingBlock; + if (headingBlock == null) + { + return; + } + + // If the AutoLink options is set, we register a LinkReferenceDefinition at the document level + if ((options & AutoIdentifierOptions.AutoLink) != 0) + { + var headingLine = headingBlock.Lines.Lines[0]; + + var text = headingLine.ToString(); + + var linkRef = new HeadingLinkReferenceDefinition() + { + Heading = headingBlock, + CreateLinkInline = CreateLinkInlineForHeading + }; + + 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 around + 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. + /// + private Inline CreateLinkInlineForHeading(InlineProcessor inlineState, LinkReferenceDefinition linkRef, Inline child) + { + var headingRef = (HeadingLinkReferenceDefinition) linkRef; + return new LinkInline() + { + // Use GetDynamicUrl to allow late binding of the Url (as a link may occur before the heading is declared and + // the inlines of the heading are actually processed by HeadingBlock_ProcessInlinesEnd) + GetDynamicUrl = () => HtmlHelper.Unescape("#" + headingRef.Heading.GetAttributes().Id), + Title = HtmlHelper.Unescape(linkRef.Title), + }; + } + + /// + /// Process the inlines of the heading to create a unique identifier + /// + /// The processor. + /// The inline. + private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline inline) + { + var identifiers = processor.Document.GetData(AutoIdentifierKey) as HashSet; + if (identifiers == null) + { + identifiers = new HashSet(); + processor.Document.SetData(AutoIdentifierKey, identifiers); + } + + var headingBlock = (HeadingBlock) processor.Block; + if (headingBlock.Inline == null) + { + return; + } + + // If id is already set, don't try to modify it + var attributes = processor.Block.GetAttributes(); + if (attributes.Id != null) + { + return; + } + + // Use internally a HtmlRenderer to strip links from a heading + var stripRenderer = rendererCache.Get(); + + stripRenderer.Render(headingBlock.Inline); + var headingText = stripRenderer.Writer.ToString(); + rendererCache.Release(stripRenderer); + + // 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(); + while (!identifiers.Add(headingId)) + { + index++; + headingBuffer.Append(baseHeadingId); + headingBuffer.Append('-'); + headingBuffer.Append(index); + headingId = headingBuffer.ToString(); + headingBuffer.Length = 0; + } + + attributes.Id = headingId; + } + + private sealed class StripRendererCache : ObjectCache + { + protected override HtmlRenderer NewInstance() + { + var headingWriter = new StringWriter(); + var stripRenderer = new HtmlRenderer(headingWriter) + { + // Set to false both to avoid having any HTML tags in the output + EnableHtmlForInline = false, + EnableHtmlEscape = false + }; + return stripRenderer; + } + + protected override void Reset(HtmlRenderer instance) + { + instance.Reset(); + } + } + } } \ No newline at end of file diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs index d6897457..81c6bfe7 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs @@ -16,9 +16,9 @@ namespace Markdig.Extensions.AutoLinks { public readonly AutoLinkOptions Options; - public AutoLinkExtension(AutoLinkOptions options) - { - Options = options ?? new AutoLinkOptions(); + public AutoLinkExtension(AutoLinkOptions options) + { + Options = options ?? new AutoLinkOptions(); } public void Setup(MarkdownPipelineBuilder pipeline) diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs index e059f991..aaa69ea8 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs @@ -1,22 +1,22 @@ -namespace Markdig.Extensions.AutoLinks -{ - public class AutoLinkOptions - { - public AutoLinkOptions() - { - ValidPreviousCharacters = "*_~("; - } - - public string ValidPreviousCharacters { get; set; } - - /// - /// Should the link open in a new window when clicked (false by default) - /// - public bool OpenInNewWindow { get; set; } - - /// - /// Should a www link be prefixed with https:// instead of http:// (false by default) - /// - public bool UseHttpsForWWWLinks { get; set; } - } -} +namespace Markdig.Extensions.AutoLinks +{ + public class AutoLinkOptions + { + public AutoLinkOptions() + { + ValidPreviousCharacters = "*_~("; + } + + public string ValidPreviousCharacters { get; set; } + + /// + /// Should the link open in a new window when clicked (false by default) + /// + public bool OpenInNewWindow { get; set; } + + /// + /// Should a www link be prefixed with https:// instead of http:// (false by default) + /// + public bool UseHttpsForWWWLinks { get; set; } + } +} diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs index 5b92b2a7..d289f5e5 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs @@ -3,10 +3,10 @@ // See the license.txt file in the project root for more information. using System; -using System.Collections.Generic; +using System.Collections.Generic; using Markdig.Helpers; using Markdig.Parsers; -using Markdig.Renderers.Html; +using Markdig.Renderers.Html; using Markdig.Syntax.Inlines; namespace Markdig.Extensions.AutoLinks @@ -30,13 +30,13 @@ namespace Markdig.Extensions.AutoLinks 'f', // for ftp:// 'm', // for mailto: 'w', // for www. - }; - - _listOfCharCache = new ListOfCharCache(); - } - - public readonly AutoLinkOptions Options; - + }; + + _listOfCharCache = new ListOfCharCache(); + } + + public readonly AutoLinkOptions Options; + private readonly ListOfCharCache _listOfCharCache; public override bool Match(InlineProcessor processor, ref StringSlice slice) @@ -49,151 +49,151 @@ namespace Markdig.Extensions.AutoLinks } List pendingEmphasis = _listOfCharCache.Get(); - try - { - // Check that an autolink is possible in the current context - if (!IsAutoLinkValidInCurrentContext(processor, pendingEmphasis)) - { - return false; - } - - var startPosition = slice.Start; - int domainOffset = 0; - - var c = slice.CurrentChar; - // Precheck URL - switch (c) - { - case 'h': - if (slice.MatchLowercase("ttp://", 1)) - { - domainOffset = 7; // http:// - } - else if (slice.MatchLowercase("ttps://", 1)) - { - domainOffset = 8; // https:// - } - else return false; - break; - case 'f': - if (!slice.MatchLowercase("tp://", 1)) - { - return false; - } - domainOffset = 6; // ftp:// - break; - case 'm': - if (!slice.MatchLowercase("ailto:", 1)) - { - return false; - } - break; - - case 'w': - if (!slice.MatchLowercase("ww.", 1)) // We won't match http:/www. or /www.xxx - { - return false; - } - domainOffset = 4; // www. - break; - } - - // Parse URL - if (!LinkHelper.TryParseUrl(ref slice, out string link, true)) - { - return false; - } - - - // If we have any pending emphasis, remove any pending emphasis characters from the end of the link - if (pendingEmphasis.Count > 0) - { - for (int i = link.Length - 1; i >= 0; i--) - { - if (pendingEmphasis.Contains(link[i])) - { - slice.Start--; - } - else - { - if (i < link.Length - 1) - { - link = link.Substring(0, i + 1); - } - break; - } - } - } - - // Post-check URL - switch (c) - { - case 'h': - if (string.Equals(link, "http://", StringComparison.OrdinalIgnoreCase) || - string.Equals(link, "https://", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - break; - case 'f': - if (string.Equals(link, "ftp://", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - break; - case 'm': - int atIndex = link.IndexOf('@'); - if (atIndex == -1 || - atIndex == 7) // mailto:@ - no email part - { - return false; - } - domainOffset = atIndex + 1; - break; - } - - if (!LinkHelper.IsValidDomain(link, domainOffset)) - { - return false; - } - - var inline = new LinkInline() - { + try + { + // Check that an autolink is possible in the current context + if (!IsAutoLinkValidInCurrentContext(processor, pendingEmphasis)) + { + return false; + } + + var startPosition = slice.Start; + int domainOffset = 0; + + var c = slice.CurrentChar; + // Precheck URL + switch (c) + { + case 'h': + if (slice.MatchLowercase("ttp://", 1)) + { + domainOffset = 7; // http:// + } + else if (slice.MatchLowercase("ttps://", 1)) + { + domainOffset = 8; // https:// + } + else return false; + break; + case 'f': + if (!slice.MatchLowercase("tp://", 1)) + { + return false; + } + domainOffset = 6; // ftp:// + break; + case 'm': + if (!slice.MatchLowercase("ailto:", 1)) + { + return false; + } + break; + + case 'w': + if (!slice.MatchLowercase("ww.", 1)) // We won't match http:/www. or /www.xxx + { + return false; + } + domainOffset = 4; // www. + break; + } + + // Parse URL + if (!LinkHelper.TryParseUrl(ref slice, out string link, true)) + { + return false; + } + + + // If we have any pending emphasis, remove any pending emphasis characters from the end of the link + if (pendingEmphasis.Count > 0) + { + for (int i = link.Length - 1; i >= 0; i--) + { + if (pendingEmphasis.Contains(link[i])) + { + slice.Start--; + } + else + { + if (i < link.Length - 1) + { + link = link.Substring(0, i + 1); + } + break; + } + } + } + + // Post-check URL + switch (c) + { + case 'h': + if (string.Equals(link, "http://", StringComparison.OrdinalIgnoreCase) || + string.Equals(link, "https://", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + break; + case 'f': + if (string.Equals(link, "ftp://", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + break; + case 'm': + int atIndex = link.IndexOf('@'); + if (atIndex == -1 || + atIndex == 7) // mailto:@ - no email part + { + return false; + } + domainOffset = atIndex + 1; + break; + } + + if (!LinkHelper.IsValidDomain(link, domainOffset)) + { + return false; + } + + var inline = new LinkInline() + { Span = { Start = processor.GetSourcePosition(startPosition, out int line, out int column), - }, - Line = line, - Column = column, - Url = c == 'w' ? ((Options.UseHttpsForWWWLinks ? "https://" : "http://") + link) : link, - IsClosed = true, - IsAutoLink = true, - }; - - var skipFromBeginning = c == 'm' ? 7 : 0; // For mailto: skip "mailto:" for content - - inline.Span.End = inline.Span.Start + link.Length - 1; - inline.UrlSpan = inline.Span; - inline.AppendChild(new LiteralInline() - { - Span = inline.Span, - Line = line, - Column = column, - Content = new StringSlice(slice.Text, startPosition + skipFromBeginning, startPosition + link.Length - 1), - IsClosed = true - }); - processor.Inline = inline; - - if (Options.OpenInNewWindow) - { - inline.GetAttributes().AddPropertyIfNotExist("target", "blank"); - } - - return true; + }, + Line = line, + Column = column, + Url = c == 'w' ? ((Options.UseHttpsForWWWLinks ? "https://" : "http://") + link) : link, + IsClosed = true, + IsAutoLink = true, + }; + + var skipFromBeginning = c == 'm' ? 7 : 0; // For mailto: skip "mailto:" for content + + inline.Span.End = inline.Span.Start + link.Length - 1; + inline.UrlSpan = inline.Span; + inline.AppendChild(new LiteralInline() + { + Span = inline.Span, + Line = line, + Column = column, + Content = new StringSlice(slice.Text, startPosition + skipFromBeginning, startPosition + link.Length - 1), + IsClosed = true + }); + processor.Inline = inline; + + if (Options.OpenInNewWindow) + { + inline.GetAttributes().AddPropertyIfNotExist("target", "blank"); + } + + return true; } finally - { - _listOfCharCache.Release(pendingEmphasis); + { + _listOfCharCache.Release(pendingEmphasis); } } @@ -254,14 +254,14 @@ namespace Markdig.Extensions.AutoLinks } return countBrackets <= 0; - } - - private sealed class ListOfCharCache : DefaultObjectCache> - { - protected override void Reset(List instance) - { - instance.Clear(); - } + } + + private sealed class ListOfCharCache : DefaultObjectCache> + { + protected override void Reset(List instance) + { + instance.Clear(); + } } } } \ No newline at end of file diff --git a/src/Markdig/Extensions/Citations/CitationExtension.cs b/src/Markdig/Extensions/Citations/CitationExtension.cs index 7d04b153..05377443 100644 --- a/src/Markdig/Extensions/Citations/CitationExtension.cs +++ b/src/Markdig/Extensions/Citations/CitationExtension.cs @@ -6,8 +6,8 @@ using Markdig.Parsers.Inlines; using Markdig.Renderers; using Markdig.Renderers.Html.Inlines; using Markdig.Syntax.Inlines; -using System.Diagnostics; - +using System.Diagnostics; + namespace Markdig.Extensions.Citations { /// diff --git a/src/Markdig/Extensions/CustomContainers/CustomContainerExtension.cs b/src/Markdig/Extensions/CustomContainers/CustomContainerExtension.cs index bd85868a..3f30fd68 100644 --- a/src/Markdig/Extensions/CustomContainers/CustomContainerExtension.cs +++ b/src/Markdig/Extensions/CustomContainers/CustomContainerExtension.cs @@ -3,8 +3,8 @@ // See the license.txt file in the project root for more information. using Markdig.Parsers.Inlines; -using Markdig.Renderers; - +using Markdig.Renderers; + namespace Markdig.Extensions.CustomContainers { /// @@ -27,12 +27,12 @@ namespace Markdig.Extensions.CustomContainers { inlineParser.EmphasisDescriptors.Add(new EmphasisDescriptor(':', 2, 2, true)); inlineParser.TryCreateEmphasisInlineList.Add((emphasisChar, delimiterCount) => - { - if (delimiterCount == 2 && emphasisChar == ':') - { - return new CustomContainerInline(); - } - return null; + { + if (delimiterCount == 2 && emphasisChar == ':') + { + return new CustomContainerInline(); + } + return null; }); } } diff --git a/src/Markdig/Extensions/Emoji/EmojiExtension.cs b/src/Markdig/Extensions/Emoji/EmojiExtension.cs index cfa04f40..08b16bca 100644 --- a/src/Markdig/Extensions/Emoji/EmojiExtension.cs +++ b/src/Markdig/Extensions/Emoji/EmojiExtension.cs @@ -13,12 +13,12 @@ namespace Markdig.Extensions.Emoji public class EmojiExtension : IMarkdownExtension { public EmojiExtension(EmojiMapping emojiMapping) - { - EmojiMapping = emojiMapping; + { + EmojiMapping = emojiMapping; } - - public EmojiMapping EmojiMapping { get; } - + + public EmojiMapping EmojiMapping { get; } + public void Setup(MarkdownPipelineBuilder pipeline) { if (!pipeline.InlineParsers.Contains()) diff --git a/src/Markdig/Extensions/Emoji/EmojiMapping.cs b/src/Markdig/Extensions/Emoji/EmojiMapping.cs index 11232a94..22389029 100644 --- a/src/Markdig/Extensions/Emoji/EmojiMapping.cs +++ b/src/Markdig/Extensions/Emoji/EmojiMapping.cs @@ -7,34 +7,34 @@ using System.Collections.Generic; using Markdig.Helpers; namespace Markdig.Extensions.Emoji -{ - /// - /// An emoji shortcodes and smileys mapping, to be used by . - /// - public class EmojiMapping +{ + /// + /// An emoji shortcodes and smileys mapping, to be used by . + /// + public class EmojiMapping { - /// - /// The default emoji shortcodes and smileys mapping. + /// + /// The default emoji shortcodes and smileys mapping. /// - public static readonly EmojiMapping DefaultEmojisAndSmileysMapping = new EmojiMapping(); - - /// - /// The default emoji shortcodes mapping, without smileys. + public static readonly EmojiMapping DefaultEmojisAndSmileysMapping = new EmojiMapping(); + + /// + /// The default emoji shortcodes mapping, without smileys. /// - public static readonly EmojiMapping DefaultEmojisOnlyMapping = new EmojiMapping(enableSmileys: false); + public static readonly EmojiMapping DefaultEmojisOnlyMapping = new EmojiMapping(enableSmileys: false); internal CompactPrefixTree PrefixTree { get; } - internal char[] OpeningCharacters { get; } - - #region Emojis and Smileys - - /// - /// Returns a new instance of the default emoji shortcode to emoji unicode dictionary. - /// It can be used to create a customized . - /// - public static IDictionary GetDefaultEmojiShortcodeToUnicode() - { + internal char[] OpeningCharacters { get; } + + #region Emojis and Smileys + + /// + /// Returns a new instance of the default emoji shortcode to emoji unicode dictionary. + /// It can be used to create a customized . + /// + public static IDictionary GetDefaultEmojiShortcodeToUnicode() + { return new Dictionary(1603) { {":100:", "💯"}, @@ -1643,15 +1643,15 @@ namespace Markdig.Extensions.Emoji {":south_africa:","🇿🇦"}, {":zambia:","🇿🇲"}, {":zimbabwe:","🇿🇼"} - }; - } - - /// - /// Gets a new instance of the default smiley to emoji shortcode dictionary. - /// It can be used to create a customized . - /// - public static IDictionary GetDefaultSmileyToEmojiShortcode() - { + }; + } + + /// + /// Gets a new instance of the default smiley to emoji shortcode dictionary. + /// It can be used to create a customized . + /// + public static IDictionary GetDefaultSmileyToEmojiShortcode() + { return new Dictionary(71) { {">:(", ":angry:"}, @@ -1728,67 +1728,67 @@ namespace Markdig.Extensions.Emoji {"<=", ":custom_arrow_left_strong:" }, {"=>", ":custom_arrow_right_strong:" }, {"<=>", ":custom_arrow_left_right_strong:" }, - }; - } - - #endregion - - /// - /// Constructs a mapping for the default emoji shortcodes and smileys. - /// - public EmojiMapping(bool enableSmileys = true) - : this(GetDefaultEmojiShortcodeToUnicode(), - enableSmileys ? GetDefaultSmileyToEmojiShortcode() : new Dictionary()) - { } - - /// - /// Constructs a mapping from a dictionary of emoji shortcodes to unicode, and a dictionary of smileys to emoji shortcodes. - /// - public EmojiMapping(IDictionary shortcodeToUnicode, IDictionary smileyToShortcode) - { + }; + } + + #endregion + + /// + /// Constructs a mapping for the default emoji shortcodes and smileys. + /// + public EmojiMapping(bool enableSmileys = true) + : this(GetDefaultEmojiShortcodeToUnicode(), + enableSmileys ? GetDefaultSmileyToEmojiShortcode() : new Dictionary()) + { } + + /// + /// Constructs a mapping from a dictionary of emoji shortcodes to unicode, and a dictionary of smileys to emoji shortcodes. + /// + public EmojiMapping(IDictionary shortcodeToUnicode, IDictionary smileyToShortcode) + { if (shortcodeToUnicode == null) ThrowHelper.ArgumentNullException(nameof(shortcodeToUnicode)); - if (smileyToShortcode == null) + if (smileyToShortcode == null) ThrowHelper.ArgumentNullException(nameof(smileyToShortcode)); - // Build emojis and smileys CompactPrefixTree - - int jointCount = shortcodeToUnicode.Count + smileyToShortcode.Count; - - // Count * 2 seems to be a good fit for the data set - PrefixTree = new CompactPrefixTree(jointCount, jointCount * 2, jointCount * 2); - - // This is not the best data set for the prefix tree as it will have to check the first character linearly - // A work-around would require a bunch of substrings / removing the leading ':' from emojis, neither one is pretty - // This way we sacrifice a few microseconds for not introducing breaking changes, emojis aren't all that common anyhow - + // Build emojis and smileys CompactPrefixTree + + int jointCount = shortcodeToUnicode.Count + smileyToShortcode.Count; + + // Count * 2 seems to be a good fit for the data set + PrefixTree = new CompactPrefixTree(jointCount, jointCount * 2, jointCount * 2); + + // This is not the best data set for the prefix tree as it will have to check the first character linearly + // A work-around would require a bunch of substrings / removing the leading ':' from emojis, neither one is pretty + // This way we sacrifice a few microseconds for not introducing breaking changes, emojis aren't all that common anyhow + var firstChars = new HashSet(); - foreach (var shortcode in shortcodeToUnicode) - { - if (string.IsNullOrEmpty(shortcode.Key) || string.IsNullOrEmpty(shortcode.Value)) - ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(shortcodeToUnicode)); - - firstChars.Add(shortcode.Key[0]); - PrefixTree.Add(shortcode); + foreach (var shortcode in shortcodeToUnicode) + { + if (string.IsNullOrEmpty(shortcode.Key) || string.IsNullOrEmpty(shortcode.Value)) + ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(shortcodeToUnicode)); + + firstChars.Add(shortcode.Key[0]); + PrefixTree.Add(shortcode); } foreach (var smiley in smileyToShortcode) - { - if (string.IsNullOrEmpty(smiley.Key) || string.IsNullOrEmpty(smiley.Value)) + { + if (string.IsNullOrEmpty(smiley.Key) || string.IsNullOrEmpty(smiley.Value)) ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(smileyToShortcode)); if (!shortcodeToUnicode.TryGetValue(smiley.Value, out string unicode)) - ThrowHelper.ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value)); - + ThrowHelper.ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value)); + firstChars.Add(smiley.Key[0]); if (!PrefixTree.TryAdd(smiley.Key, unicode)) ThrowHelper.ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key)); } - OpeningCharacters = new List(firstChars).ToArray(); - } + OpeningCharacters = new List(firstChars).ToArray(); + } } } \ No newline at end of file diff --git a/src/Markdig/Extensions/Emoji/EmojiParser.cs b/src/Markdig/Extensions/Emoji/EmojiParser.cs index 72cb1021..b325791b 100644 --- a/src/Markdig/Extensions/Emoji/EmojiParser.cs +++ b/src/Markdig/Extensions/Emoji/EmojiParser.cs @@ -2,7 +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; using System.Collections.Generic; using Markdig.Helpers; using Markdig.Parsers; @@ -15,14 +15,14 @@ namespace Markdig.Extensions.Emoji /// public class EmojiParser : InlineParser { - private readonly EmojiMapping _emojiMapping; - + private readonly EmojiMapping _emojiMapping; + /// /// Initializes a new instance of the class. /// public EmojiParser(EmojiMapping emojiMapping) - { - _emojiMapping = emojiMapping; + { + _emojiMapping = emojiMapping; OpeningCharacters = _emojiMapping.OpeningCharacters; } diff --git a/src/Markdig/Extensions/EmphasisExtras/EmphasisExtraExtension.cs b/src/Markdig/Extensions/EmphasisExtras/EmphasisExtraExtension.cs index d3d06a6c..ac12ca78 100644 --- a/src/Markdig/Extensions/EmphasisExtras/EmphasisExtraExtension.cs +++ b/src/Markdig/Extensions/EmphasisExtras/EmphasisExtraExtension.cs @@ -6,8 +6,8 @@ using Markdig.Parsers.Inlines; using Markdig.Renderers; using Markdig.Renderers.Html.Inlines; using Markdig.Syntax.Inlines; -using System.Diagnostics; - +using System.Diagnostics; + namespace Markdig.Extensions.EmphasisExtras { /// @@ -68,9 +68,9 @@ namespace Markdig.Extensions.EmphasisExtras } if (requireTilde && !hasTilde) - { - int minimumCount = (Options & EmphasisExtraOptions.Subscript) != 0 ? 1 : 2; - int maximumCount = (Options & EmphasisExtraOptions.Strikethrough) != 0 ? 2 : 1; + { + int minimumCount = (Options & EmphasisExtraOptions.Subscript) != 0 ? 1 : 2; + int maximumCount = (Options & EmphasisExtraOptions.Strikethrough) != 0 ? 2 : 1; parser.EmphasisDescriptors.Add(new EmphasisDescriptor('~', minimumCount, maximumCount, true)); } if (requireSup && !hasSup) @@ -107,7 +107,7 @@ namespace Markdig.Extensions.EmphasisExtras var c = emphasisInline.DelimiterChar; switch (c) { - case '~': + case '~': Debug.Assert(emphasisInline.DelimiterCount <= 2); return emphasisInline.DelimiterCount == 2 ? "del" : "sub"; case '^': diff --git a/src/Markdig/Extensions/Footnotes/FootnoteParser.cs b/src/Markdig/Extensions/Footnotes/FootnoteParser.cs index 79a0343c..ca95a2fc 100644 --- a/src/Markdig/Extensions/Footnotes/FootnoteParser.cs +++ b/src/Markdig/Extensions/Footnotes/FootnoteParser.cs @@ -5,8 +5,8 @@ using Markdig.Helpers; using Markdig.Parsers; using Markdig.Syntax; -using Markdig.Syntax.Inlines; - +using Markdig.Syntax.Inlines; + namespace Markdig.Extensions.Footnotes { /// diff --git a/src/Markdig/Helpers/CharHelper.cs b/src/Markdig/Helpers/CharHelper.cs index 31f5cd10..789c3a52 100644 --- a/src/Markdig/Helpers/CharHelper.cs +++ b/src/Markdig/Helpers/CharHelper.cs @@ -3,13 +3,13 @@ // See the license.txt file in the project root for more information. using System; -using System.Globalization; +using System.Globalization; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Diagnostics; - +using System.Diagnostics; + namespace Markdig.Helpers -{ +{ /// /// Helper class for handling characters. /// @@ -196,30 +196,30 @@ namespace Markdig.Helpers || category == UnicodeCategory.FinalQuotePunctuation || category == UnicodeCategory.OtherPunctuation); } - } - - // Same as CheckUnicodeCategory - internal static bool IsSpaceOrPunctuation(this char c) - { + } + + // Same as CheckUnicodeCategory + internal static bool IsSpaceOrPunctuation(this char c) + { if (c <= 'ÿ') { return c == '\0' || c == ' ' || (c >= '\t' && c <= '\r') || c == '\u00a0' || c == '\u0085' || (c >= 33 && c <= 47 && c != 38) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 126); - } - else - { - var category = CharUnicodeInfo.GetUnicodeCategory(c); - return category == UnicodeCategory.SpaceSeparator - || category == UnicodeCategory.LineSeparator - || category == UnicodeCategory.ParagraphSeparator - || category == UnicodeCategory.ConnectorPunctuation - || category == UnicodeCategory.DashPunctuation - || category == UnicodeCategory.OpenPunctuation - || category == UnicodeCategory.ClosePunctuation - || category == UnicodeCategory.InitialQuotePunctuation - || category == UnicodeCategory.FinalQuotePunctuation - || category == UnicodeCategory.OtherPunctuation; - } + } + else + { + var category = CharUnicodeInfo.GetUnicodeCategory(c); + return category == UnicodeCategory.SpaceSeparator + || category == UnicodeCategory.LineSeparator + || category == UnicodeCategory.ParagraphSeparator + || category == UnicodeCategory.ConnectorPunctuation + || category == UnicodeCategory.DashPunctuation + || category == UnicodeCategory.OpenPunctuation + || category == UnicodeCategory.ClosePunctuation + || category == UnicodeCategory.InitialQuotePunctuation + || category == UnicodeCategory.FinalQuotePunctuation + || category == UnicodeCategory.OtherPunctuation; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -266,13 +266,13 @@ namespace Markdig.Helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsAlphaUpper(this char c) - { + { return (uint)(c - 'A') <= ('Z' - 'A'); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsAlpha(this char c) - { + { return (uint)((c - 'A') & ~0x20) <= ('Z' - 'A'); } @@ -339,7 +339,7 @@ namespace Markdig.Helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsHighSurrogate(char c) - { + { return IsInInclusiveRange(c, HighSurrogateStart, HighSurrogateEnd); } @@ -349,12 +349,12 @@ namespace Markdig.Helpers return IsInInclusiveRange(c, LowSurrogateStart, LowSurrogateEnd); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool IsInInclusiveRange(char c, char min, char max) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool IsInInclusiveRange(char c, char min, char max) => (uint)(c - min) <= (uint)(max - min); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static bool IsInInclusiveRange(int value, uint min, uint max) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static bool IsInInclusiveRange(int value, uint min, uint max) => ((uint)value - min) <= (max - min); public static IEnumerable ToUtf32(StringSlice text) @@ -362,7 +362,7 @@ namespace Markdig.Helpers for (int i = text.Start; i <= text.End; i++) { if (IsHighSurrogate(text[i]) && i < text.End && IsLowSurrogate(text[i + 1])) - { + { Debug.Assert(char.IsSurrogatePair(text[i], text[i + 1])); yield return char.ConvertToUtf32(text[i], text[i + 1]); } @@ -745,24 +745,24 @@ namespace Markdig.Helpers c == 0x002128 || c == 0x002395 || c == 0x01D4A2 || c == 0x01D4BB || c == 0x01D546; - } - - // Used by ListExtraItemParser to format numbers from 1 - 26 - private static readonly string[] smallNumberStringCache = { - "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", - "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", - "20", "21", "22", "23", "24", "25", "26", - }; + } - internal static string SmallNumberToString(int number) - { - string[] cache = smallNumberStringCache; - if ((uint)number < (uint)cache.Length) - { - return cache[number]; - } - - return number.ToString(); + // Used by ListExtraItemParser to format numbers from 1 - 26 + private static readonly string[] smallNumberStringCache = { + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", + "20", "21", "22", "23", "24", "25", "26", + }; + + internal static string SmallNumberToString(int number) + { + string[] cache = smallNumberStringCache; + if ((uint)number < (uint)cache.Length) + { + return cache[number]; + } + + return number.ToString(); } } } \ No newline at end of file diff --git a/src/Markdig/Helpers/CompactPrefixTree.cs b/src/Markdig/Helpers/CompactPrefixTree.cs index 207a1b86..2aa11ec9 100644 --- a/src/Markdig/Helpers/CompactPrefixTree.cs +++ b/src/Markdig/Helpers/CompactPrefixTree.cs @@ -1,999 +1,999 @@ -// Copyright (c) Miha Zupan. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; - -/* - * Ported to Markdig from https://github.com/MihaZupan/SharpCollections - * - * If you encounter any problems related to this data structure, please cc @MihaZupan - * - * This data structure aims to use less memory than reference-based implementations. - * Please note the lack of a Remove method on this data structure - */ - -// Relies on NETCORE being set for selective use of Spans - -//namespace SharpCollections.Generic -namespace Markdig.Helpers -{ - /// - /// A compact insert-only key/value collection for fast prefix lookups - /// Something between a Trie and a full Radix tree, but stored linearly in memory - /// - /// The value associated with the key - [ExcludeFromCodeCoverage] - internal sealed class CompactPrefixTree : IReadOnlyDictionary, IReadOnlyList> - { - /// - /// Used internally to control behavior of insertion - /// Copied from internals - /// - internal enum InsertionBehavior : byte - { - /// - /// The default insertion behavior. Does not overwrite or throw. - /// - None = 0, - - /// - /// Specifies that an existing entry with the same key should be overwritten if encountered. - /// - OverwriteExisting = 1, - - /// - /// Specifies that if an existing entry with the same key is encountered, an exception should be thrown. - /// - ThrowOnExisting = 2 - } - - [DebuggerDisplay("{Char}, Child: {ChildChar} at {ChildIndex}, Match: {MatchIndex}, Children: {Children?.Count ?? 0}")] - private struct Node - { - /// - /// The character this node represents, should never be 0 - /// - public char Char; - /// - /// Will be 0 if this is a leaf node - /// - public char ChildChar; - public int ChildIndex; - /// - /// Set to -1 if it does not point to a match - /// - public int MatchIndex; - /// - /// -1 if not present - /// - public int Children; - } - - private Node[] _tree; - private static readonly Node[] _emptyTree = new Node[0]; - - private KeyValuePair[] _matches; - private static readonly KeyValuePair[] _emptyMatches = new KeyValuePair[0]; - - private int _childrenIndex = 0; - private int[] _children = _emptyChildren; - private static readonly int[] _emptyChildren = new int[0]; - - #region Size and Capacity - - /// - /// Gets the number of nodes in the internal tree structure - /// You might be looking for - /// Exposing this might help in deducing more efficient initial parameters - /// - public int TreeSize { get; private set; } - /// - /// Gets or sets the capacity of the internal tree structure buffer - /// You might be looking for - /// - public int TreeCapacity - { - get - { - return _tree.Length; - } - set - { - if (value < TreeSize) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); - - if (value != TreeSize) - { - Node[] newTree = new Node[value]; - if (TreeSize > 0) - { - Array.Copy(_tree, 0, newTree, 0, TreeSize); - } - _tree = newTree; - } - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnsureTreeCapacity(int min) - { - if (_tree.Length < min) - { - EnsureTreeCapacityRare(min); - } - Debug.Assert(_tree.Length >= min); - } - [MethodImpl(MethodImplOptions.NoInlining)] - private void EnsureTreeCapacityRare(int min) - { - // Expansion logic as in System.Collections.Generic.List - Debug.Assert(min > _tree.Length); - int newCapacity = _tree.Length * 2; - if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; - if (newCapacity < min) newCapacity = min; - TreeCapacity = newCapacity; - } - - /// - /// Gets the number of key/value pairs contained in the - /// - public int Count { get; private set; } - /// - /// Gets or sets the capacity of the internal key/value pair buffer - /// - public int Capacity - { - get - { - return _matches.Length; - } - set - { - if (value < Count) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); - - if (value != Count) - { - KeyValuePair[] newMatches = new KeyValuePair[value]; - if (Count > 0) - { - Array.Copy(_matches, 0, newMatches, 0, Count); - } - _matches = newMatches; - } - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnsureCapacity(int min) - { - // Expansion logic as in System.Collections.Generic.List - if (_matches.Length < min) - { - EnsureCapacityRare(min); - } - Debug.Assert(_matches.Length >= min); - } - [MethodImpl(MethodImplOptions.NoInlining)] - private void EnsureCapacityRare(int min) - { - // Expansion logic as in System.Collections.Generic.List - Debug.Assert(min > _matches.Length); - int newCapacity = _matches.Length * 2; - if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; - if (newCapacity < min) newCapacity = min; - Capacity = newCapacity; - } - - /// - /// Gets the size of the children buffer in the internal tree structure - /// You might be looking for - /// Exposing this might help in deducing more efficient initial parameters - /// - public int ChildrenCount => _childrenIndex; - /// - /// Gets or sets the capacity of the internal children buffer - /// You might be looking for - /// - public int ChildrenCapacity - { - get - { - return _children.Length; - } - set - { - if (value < _childrenIndex) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); - - if (value != _childrenIndex) - { - int[] newChildren = new int[value]; - if (_childrenIndex > 0) - { - Array.Copy(_children, 0, newChildren, 0, _childrenIndex); - } - - // Set new odd indexes to -1 - for (int i = _childrenIndex + 1; i < newChildren.Length; i += 2) - newChildren[i] = -1; - - _children = newChildren; - } - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnsureChildrenCapacity(int min) - { - if (_children.Length < min) - { - EnsureChildrenCapacityRare(min); - } - Debug.Assert(_children.Length >= min); - } - [MethodImpl(MethodImplOptions.NoInlining)] - private void EnsureChildrenCapacityRare(int min) - { - // Expansion logic as in System.Collections.Generic.List - Debug.Assert(min > _children.Length); - Debug.Assert(_childrenIndex % 2 == 0); - int newCapacity = _children.Length * 2; - if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; - if (newCapacity < min) newCapacity = min; - ChildrenCapacity = newCapacity; - } - - #endregion Size and Capacity - - #region RootChar - - // Inspired by Markdig's CharacterMap - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryGetRoot(char rootChar, out int rootNodeIndex) - { - if (rootChar < 128) - { - rootNodeIndex = _asciiRootMap[rootChar]; - return rootNodeIndex != -1; - } - - if (_unicodeRootMap != null) - { - return _unicodeRootMap.TryGetValue(rootChar, out rootNodeIndex); - } - - rootNodeIndex = -1; - return false; - } - private void SetRootChar(char rootChar) - { - if (rootChar < 128) - { - Debug.Assert(_asciiRootMap[rootChar] == -1); - _asciiRootMap[rootChar] = TreeSize; - } - else - { - if (_unicodeRootMap == null) - { - _unicodeRootMap = new Dictionary(); - } - _unicodeRootMap.Add(rootChar, TreeSize); - } - } - private readonly int[] _asciiRootMap = new int[128]; - private Dictionary _unicodeRootMap; - - #endregion RootChar - - private void Init(int matchCapacity, int treeCapacity, int childrenCapacity) - { - for (int i = 0; i < _asciiRootMap.Length; i++) - _asciiRootMap[i] = -1; - - _matches = matchCapacity == 0 ? _emptyMatches : new KeyValuePair[matchCapacity]; - _tree = treeCapacity == 0 ? _emptyTree : new Node[treeCapacity]; - EnsureChildrenCapacity(childrenCapacity); - } - - /// - /// Constructs a new with no initial prefixes - /// - public CompactPrefixTree(int matchCapacity = 0, int treeCapacity = 0, int childrenCapacity = 0) - { - Init(matchCapacity, treeCapacity, childrenCapacity); - } - /// - /// Constructs a new with the supplied matches - /// - /// Matches to initialize the with. For best lookup performance, this collection should be sorted. - public CompactPrefixTree(ICollection> input) - { - if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input); - - Init(input.Count, input.Count * 2, input.Count * 2); - - using (var e = input.GetEnumerator()) - { - for (int i = 0; i < input.Count; i++) - { - e.MoveNext(); - TryInsert(e.Current, InsertionBehavior.ThrowOnExisting); - } - } - } - - #region this[] accessors - - /// - /// Retrieves the key/value pair at the specified index (must be lower than ) - /// - /// Index of pair to get, must be lower than (the order is the same as the order in which the elements were added) - /// The key/value pair of the element at the specified index - public KeyValuePair this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - if ((uint)index >= (uint)Count) ThrowHelper.ThrowIndexOutOfRangeException(); - return _matches[index]; - } - } - - /// - /// Gets or sets the value associated with the specified key - /// - /// The key of the value to get or set - /// The value of the element with the specified key - public TValue this[string key] - { - get - { - if (TryMatchExact(key.AsSpan(), out KeyValuePair match)) - return match.Value; - throw new KeyNotFoundException(key); - } - set - { - var pair = new KeyValuePair(key, value); - bool modified = TryInsert(in pair, InsertionBehavior.OverwriteExisting); - Debug.Assert(modified); - } - } // Get, Set - - /// - /// Gets the value associated with the specified key - /// - /// The key of the value to get - /// The key/value pair of the element with the specified key - public KeyValuePair this[ReadOnlySpan key] - { - get - { - if (TryMatchExact(key, out KeyValuePair match)) - return match; - throw new KeyNotFoundException(key.ToString()); - } - } // Get only - - #endregion this[] accessors - - #region Add, TryAdd - - /// - /// Adds the specified key/value pair to the - /// - /// The key of the element to add - /// The value of the element to add - public void Add(string key, TValue value) - { - var pair = new KeyValuePair(key, value); - TryInsert(in pair, InsertionBehavior.ThrowOnExisting); - } - /// - /// Adds the specified key/value pair to the - /// - /// The key/value pair to add - public void Add(KeyValuePair pair) - => TryInsert(in pair, InsertionBehavior.ThrowOnExisting); - - /// - /// Tries to add the key/value pair to the if the key is not yet present - /// - /// The key of the element to add - /// The value of the element to add - /// True if the element was added, false otherwise - public bool TryAdd(string key, TValue value) - { - var pair = new KeyValuePair(key, value); - return TryInsert(in pair, InsertionBehavior.None); - } - /// - /// Tries to add the key/value pair to the if the key is not yet present - /// - /// The pair to add - /// True if the element was added, false otherwise - public bool TryAdd(KeyValuePair pair) - => TryInsert(in pair, InsertionBehavior.None); - - #endregion Add, TryAdd - - #region Insert internal - - private bool TryInsert(in KeyValuePair pair, InsertionBehavior behavior) - { - string key = pair.Key; - if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); - if (key.Length == 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.key, ExceptionReason.String_Empty); - Debug.Assert(!string.IsNullOrEmpty(key)); - - char rootChar = key[0]; - if (TryGetRoot(rootChar, out int rootNodeIndex)) - { - var tree = _tree; - ref Node node = ref tree[rootNodeIndex]; - for (int i = 1; i < key.Length; i++) - { - char c = key[i]; - - if (node.ChildChar == c) - { - node = ref tree[node.ChildIndex]; - continue; - } - - if (node.Children == -1) - { - // This could be a leaf node - if (node.ChildChar == 0) - { - // This is a leaf node - int previousMatchIndex = node.MatchIndex; - ref KeyValuePair previousMatch = ref _matches[previousMatchIndex]; - string previousKey = previousMatch.Key; - - // Find out for how long the two keys continue to share a prefix - int previousIndex = i; - int minLength = Math.Min(key.Length, previousKey.Length); - for (; i < minLength; i++) - { - // We haven't checked the i-th character of key so far - if (key[i] != previousKey[i]) break; - } - - if (i == minLength && key.Length == previousKey.Length) - { - // The two keys are of the same length and i has reached the length of the key => duplicate - Debug.Assert(i == key.Length && i == previousKey.Length); - Debug.Assert(key == previousKey); - goto HandleDuplicateKey; - } - - // Clear the match index on the current node as it's no longer a leaf node - node.MatchIndex = -1; - - // insert (i - previousIndex) intermediary nodes (could be 0) - int intermediaryNodesToInsert = i - previousIndex; - if (intermediaryNodesToInsert > 0) - { - node.ChildIndex = TreeSize; - node.ChildChar = key[previousIndex]; - EnsureTreeCapacity(TreeSize + intermediaryNodesToInsert); - tree = _tree; - for (int j = 0; j < intermediaryNodesToInsert - 1; j++) - { - tree[TreeSize + j] = new Node() - { - Char = previousKey[previousIndex + j], - ChildChar = previousKey[previousIndex + j + 1], - ChildIndex = TreeSize + j + 1, - MatchIndex = -1, - Children = -1 - }; - } - TreeSize += intermediaryNodesToInsert; - tree[TreeSize - 1] = new Node() - { - Char = previousKey[previousIndex + intermediaryNodesToInsert - 1], - MatchIndex = -1, - Children = -1 - }; - node = ref tree[TreeSize - 1]; - } - - node.ChildIndex = TreeSize; - - // Insert the pair - EnsureCapacity(Count + 1); - _matches[Count] = pair; - - // One of the strings could be a prefix of the other, in that case we're only inserting one leaf node - if (i == minLength) - { - Debug.Assert(key.Length != previousKey.Length); - if (previousKey.Length < key.Length) // If the input was sorted, this should be hit - { - Debug.Assert(key.StartsWith(previousKey)); - node.ChildChar = key[i]; - node.MatchIndex = previousMatchIndex; - EnsureTreeCapacity(TreeSize + 1); - _tree[TreeSize] = new Node() - { - Char = key[i], - MatchIndex = Count, - Children = -1 - }; - } - else // if key.Length < previousKey.Length - { - Debug.Assert(key.Length < previousKey.Length); - Debug.Assert(previousKey.StartsWith(key)); - node.ChildChar = previousKey[i]; - node.MatchIndex = Count; - EnsureTreeCapacity(TreeSize + 1); - _tree[TreeSize] = new Node() - { - Char = previousKey[i], - MatchIndex = previousMatchIndex, - Children = -1 - }; - } - Count++; - TreeSize++; - return true; - } - - // Insert two leaf nodes - Debug.Assert(node.Char != 0 && node.Char == previousKey[i - 1]); - Debug.Assert(node.MatchIndex == -1); - Debug.Assert(node.Children == -1); - - node.ChildChar = previousKey[i]; - node.Children = _childrenIndex; - - EnsureChildrenCapacity(_childrenIndex + 2); - _children[_childrenIndex] = TreeSize + 1; - _childrenIndex += 2; - - // Insert the two leaf nodes - EnsureTreeCapacity(TreeSize + 2); - _tree[TreeSize] = new Node() - { - Char = previousKey[i], - MatchIndex = previousMatchIndex, - Children = -1 - }; - _tree[TreeSize + 1] = new Node() - { - Char = key[i], - MatchIndex = Count, - Children = -1 - }; - - Count++; - TreeSize += 2; - return true; - } - else - { - // This node has a child char, therefore we either don't have a match attached or that match is simply a prefix of the current key - Debug.Assert(node.MatchIndex == -1 || key.StartsWith(_matches[node.MatchIndex].Key)); - - // Set this pair as the current node's first element in the Children list - node.Children = _childrenIndex; - EnsureChildrenCapacity(_childrenIndex + 2); - _children[_childrenIndex] = TreeSize; - _childrenIndex += 2; - - InsertLeafNode(in pair, c); - return true; - } - } - else - { - // Look for a child node with a matching Char in all of children - var children = _children; - int childrenIndex = node.Children; - int lastChildrenIndex = childrenIndex; - do - { - if ((uint)childrenIndex >= (uint)children.Length) - break; - node = ref _tree[children[childrenIndex]]; - if (node.Char == c) goto NextChar; - lastChildrenIndex = childrenIndex; - childrenIndex = children[childrenIndex + 1]; - } - while (true); - - // A child node was not found, add a new one to children - EnsureChildrenCapacity(_childrenIndex + 2); - _children[lastChildrenIndex + 1] = _childrenIndex; - _children[_childrenIndex] = TreeSize; - _childrenIndex += 2; - - InsertLeafNode(in pair, c); - return true; - } - - NextChar:; - } - - // We have found our final node, check if a match already claimed this node - if (node.MatchIndex != -1) - { - ref KeyValuePair previousMatch = ref _matches[node.MatchIndex]; - - // Either some other key is the leaf here, or the key is duplicated - if (previousMatch.Key.Length == key.Length) - { - Debug.Assert(previousMatch.Key == key); - goto HandleDuplicateKey; - } - else - { - // It's not a duplicate but shares key.Length characters, therefore it's longer - // This will never occur if the input was sorted - Debug.Assert(previousMatch.Key.Length > key.Length); - Debug.Assert(previousMatch.Key.StartsWith(key)); - Debug.Assert(node.ChildChar == 0 && node.Children == -1); - - // It is a leaf node - // Move the prevMatch one node inward - int previousMatchIndex = node.MatchIndex; - node.MatchIndex = Count; - node.ChildChar = previousMatch.Key[key.Length]; - node.ChildIndex = TreeSize; - EnsureTreeCapacity(TreeSize + 1); - _tree[TreeSize] = new Node() - { - Char = previousMatch.Key[key.Length], - MatchIndex = previousMatchIndex, - Children = -1 - }; - TreeSize++; - - // Set the pair as a match on this node - } - } - - // Set the pair as a match on this node - node.MatchIndex = Count; // This might be modifying a forgotten node reference, but in that case it was already set - EnsureCapacity(Count + 1); - _matches[Count] = pair; - Count++; - return true; - - HandleDuplicateKey:; - Debug.Assert(key == _matches[node.MatchIndex].Key); - if (behavior == InsertionBehavior.None) return false; - if (behavior == InsertionBehavior.OverwriteExisting) - { - _matches[node.MatchIndex] = pair; - return true; - } - Debug.Assert(behavior == InsertionBehavior.ThrowOnExisting); - ThrowHelper.ThrowArgumentException(ExceptionArgument.key, ExceptionReason.DuplicateKey); - Debug.Assert(false, "Should throw by now"); - return false; - } - else // if the root character is not yet in the collection - { - SetRootChar(rootChar); - InsertLeafNode(in pair, rootChar); - return true; - } - } - private void InsertLeafNode(in KeyValuePair pair, char nodeChar) - { - EnsureCapacity(Count + 1); - _matches[Count] = pair; - - EnsureTreeCapacity(TreeSize + 1); - _tree[TreeSize] = new Node() - { - Char = nodeChar, - MatchIndex = Count, - Children = -1 - }; - - Count++; - TreeSize++; - } - - #endregion Insert internal - - #region TryMatch longest - - /// - /// Tries to find the longest prefix of text, that is contained in this - /// - /// The text in which to search for the prefix - /// The found prefix and the corresponding value - /// True if a match was found, false otherwise - public bool TryMatchLongest(ReadOnlySpan text, out KeyValuePair match) - { - match = default; - if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) - return false; - - int matchIndex = -1; - int depth = 1; - - ref Node node = ref _tree[nodeIndex]; - if (node.ChildChar == 0) goto LeafNodeFound; - if (node.MatchIndex != -1) matchIndex = node.MatchIndex; - - for (int i = 1; i < text.Length; i++) - { - char c = text[i]; - - if (node.ChildChar == c) - { - node = ref _tree[node.ChildIndex]; - goto NextChar; - } - - var children = _children; - int childrenIndex = node.Children; - do - { - if ((uint)childrenIndex >= (uint)children.Length) - goto Return; - node = ref _tree[children[childrenIndex]]; - if (node.Char == c) goto NextChar; - childrenIndex = children[childrenIndex + 1]; - } - while (true); - - NextChar:; - depth++; - if (node.ChildChar == 0) goto LeafNodeFound; - if (node.MatchIndex != -1) matchIndex = node.MatchIndex; - } - // We have ran out of our string, return the longest match we've found - goto Return; - - LeafNodeFound:; - ref KeyValuePair possibleMatch = ref _matches[node.MatchIndex]; - if (possibleMatch.Key.Length <= text.Length) - { - // Check that the rest of the strings match - if (text.Slice(depth).StartsWith(possibleMatch.Key.AsSpan(depth), StringComparison.Ordinal)) - { - matchIndex = node.MatchIndex; - } - } - - Return:; - if (matchIndex != -1) - { - match = _matches[matchIndex]; - return true; - } - return false; - } - - #endregion TryMatch longest - - #region TryMatch exact - - /// - /// Tries to find a prefix of text, that is contained in this and is exactly text.Length characters long - /// - /// The text in which to search for the prefix - /// The found prefix and the corresponding value - /// True if a match was found, false otherwise - public bool TryMatchExact(ReadOnlySpan text, out KeyValuePair match) - { - match = default; - if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) - return false; - - int depth = 1; - - ref Node node = ref _tree[nodeIndex]; - if (node.ChildChar == 0) goto LeafNodeFound; - if (node.MatchIndex != -1 && text.Length == 1) - { - match = _matches[node.MatchIndex]; - return true; - } - - for (int i = 1; i < text.Length; i++) - { - char c = text[i]; - - if (node.ChildChar == c) - { - node = ref _tree[node.ChildIndex]; - goto NextChar; - } - - var children = _children; - int childrenIndex = node.Children; - do - { - if ((uint)childrenIndex >= (uint)children.Length) - return false; - node = ref _tree[children[childrenIndex]]; - if (node.Char == c) goto NextChar; - childrenIndex = children[childrenIndex + 1]; - } - while (true); - - NextChar:; - depth++; - if (node.ChildChar == 0) goto LeafNodeFound; - } - - if (node.MatchIndex == -1) return false; - match = _matches[node.MatchIndex]; - Debug.Assert(match.Key.Length == text.Length); - return true; - - LeafNodeFound:; - match = _matches[node.MatchIndex]; - - return match.Key.Length == text.Length && - text.Slice(depth).Equals(match.Key.AsSpan(depth), StringComparison.Ordinal); - } - - #endregion TryMatch exact - - #region TryMatch shortest - - /// - /// Tries to find the shortest prefix of text, that is contained in this - /// - /// The text in which to search for the prefix - /// The found prefix and the corresponding value - /// True if a match was found, false otherwise - public bool TryMatchShortest(ReadOnlySpan text, out KeyValuePair match) - { - match = default; - if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) - return false; - - ref Node node = ref _tree[nodeIndex]; - if (node.MatchIndex != -1) - { - match = _matches[node.MatchIndex]; - return true; - } - - for (int i = 1; i < text.Length; i++) - { - char c = text[i]; - - if (node.ChildChar == c) - { - node = ref _tree[node.ChildIndex]; - goto NextChar; - } - - var children = _children; - int childrenIndex = node.Children; - do - { - if ((uint)childrenIndex >= (uint)children.Length) - return false; - node = ref _tree[children[childrenIndex]]; - if (node.Char == c) goto NextChar; - childrenIndex = children[childrenIndex + 1]; - } - while (true); - - NextChar:; - if (node.MatchIndex != -1) - { - match = _matches[node.MatchIndex]; - return true; - } - } - Debug.Assert(node.MatchIndex == -1); - return false; - } - - #endregion TryMatch shortest - - #region Interface implementations - - /// - /// Determines whether the contains the specified key - /// - /// The key to locate in this - /// True if the key is contained in this PrefixTree, false otherwise. - public bool ContainsKey(string key) - => TryMatchExact(key.AsSpan(), out _); - - /// - /// Gets the value associated with the specified key - /// - /// The key of the value to get - /// The value associated with the specified key - /// True if the key is contained in this PrefixTree, false otherwise. - public bool TryGetValue(string key, out TValue value) - { - bool ret = TryMatchExact(key.AsSpan(), out KeyValuePair match); - value = match.Value; - return ret; - } - - /// - /// Gets a collection containing the keys in this - /// - public IEnumerable Keys - { - get - { - for (int i = 0; i < Count; i++) - yield return _matches[i].Key; - } - } - /// - /// Gets a collection containing the values in this - /// - public IEnumerable Values - { - get - { - for (int i = 0; i < Count; i++) - yield return _matches[i].Value; - } - } - - /// - /// Returns an Enumerator that iterates through the . - /// Use the index accessor instead () - /// - /// - public IEnumerator> GetEnumerator() => new Enumerator(_matches); - - IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_matches); - - /// - /// Enumerates the elements of a - /// - public struct Enumerator : IEnumerator>, IEnumerator - { - private readonly KeyValuePair[] _matches; - private int _index; - - internal Enumerator(KeyValuePair[] matches) - { - _matches = matches; - _index = -1; - } - - /// - /// Increments the internal index - /// - /// True if the index is less than the length of the internal array - public bool MoveNext() => ++_index < _matches.Length; - /// - /// Gets the at the current position - /// - public KeyValuePair Current => _matches[_index]; - object IEnumerator.Current => _matches[_index]; - - /// - /// Does nothing - /// - public void Dispose() { } - - /// - /// Resets the internal index to the beginning of the array - /// - public void Reset() - { - _index = -1; - } - } - - #endregion Interface implementations - } -} +// Copyright (c) Miha Zupan. All rights reserved. +// This file is licensed under the BSD-Clause 2 license. +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +/* + * Ported to Markdig from https://github.com/MihaZupan/SharpCollections + * + * If you encounter any problems related to this data structure, please cc @MihaZupan + * + * This data structure aims to use less memory than reference-based implementations. + * Please note the lack of a Remove method on this data structure + */ + +// Relies on NETCORE being set for selective use of Spans + +//namespace SharpCollections.Generic +namespace Markdig.Helpers +{ + /// + /// A compact insert-only key/value collection for fast prefix lookups + /// Something between a Trie and a full Radix tree, but stored linearly in memory + /// + /// The value associated with the key + [ExcludeFromCodeCoverage] + internal sealed class CompactPrefixTree : IReadOnlyDictionary, IReadOnlyList> + { + /// + /// Used internally to control behavior of insertion + /// Copied from internals + /// + internal enum InsertionBehavior : byte + { + /// + /// The default insertion behavior. Does not overwrite or throw. + /// + None = 0, + + /// + /// Specifies that an existing entry with the same key should be overwritten if encountered. + /// + OverwriteExisting = 1, + + /// + /// Specifies that if an existing entry with the same key is encountered, an exception should be thrown. + /// + ThrowOnExisting = 2 + } + + [DebuggerDisplay("{Char}, Child: {ChildChar} at {ChildIndex}, Match: {MatchIndex}, Children: {Children?.Count ?? 0}")] + private struct Node + { + /// + /// The character this node represents, should never be 0 + /// + public char Char; + /// + /// Will be 0 if this is a leaf node + /// + public char ChildChar; + public int ChildIndex; + /// + /// Set to -1 if it does not point to a match + /// + public int MatchIndex; + /// + /// -1 if not present + /// + public int Children; + } + + private Node[] _tree; + private static readonly Node[] _emptyTree = new Node[0]; + + private KeyValuePair[] _matches; + private static readonly KeyValuePair[] _emptyMatches = new KeyValuePair[0]; + + private int _childrenIndex = 0; + private int[] _children = _emptyChildren; + private static readonly int[] _emptyChildren = new int[0]; + + #region Size and Capacity + + /// + /// Gets the number of nodes in the internal tree structure + /// You might be looking for + /// Exposing this might help in deducing more efficient initial parameters + /// + public int TreeSize { get; private set; } + /// + /// Gets or sets the capacity of the internal tree structure buffer + /// You might be looking for + /// + public int TreeCapacity + { + get + { + return _tree.Length; + } + set + { + if (value < TreeSize) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); + + if (value != TreeSize) + { + Node[] newTree = new Node[value]; + if (TreeSize > 0) + { + Array.Copy(_tree, 0, newTree, 0, TreeSize); + } + _tree = newTree; + } + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void EnsureTreeCapacity(int min) + { + if (_tree.Length < min) + { + EnsureTreeCapacityRare(min); + } + Debug.Assert(_tree.Length >= min); + } + [MethodImpl(MethodImplOptions.NoInlining)] + private void EnsureTreeCapacityRare(int min) + { + // Expansion logic as in System.Collections.Generic.List + Debug.Assert(min > _tree.Length); + int newCapacity = _tree.Length * 2; + if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; + if (newCapacity < min) newCapacity = min; + TreeCapacity = newCapacity; + } + + /// + /// Gets the number of key/value pairs contained in the + /// + public int Count { get; private set; } + /// + /// Gets or sets the capacity of the internal key/value pair buffer + /// + public int Capacity + { + get + { + return _matches.Length; + } + set + { + if (value < Count) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); + + if (value != Count) + { + KeyValuePair[] newMatches = new KeyValuePair[value]; + if (Count > 0) + { + Array.Copy(_matches, 0, newMatches, 0, Count); + } + _matches = newMatches; + } + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void EnsureCapacity(int min) + { + // Expansion logic as in System.Collections.Generic.List + if (_matches.Length < min) + { + EnsureCapacityRare(min); + } + Debug.Assert(_matches.Length >= min); + } + [MethodImpl(MethodImplOptions.NoInlining)] + private void EnsureCapacityRare(int min) + { + // Expansion logic as in System.Collections.Generic.List + Debug.Assert(min > _matches.Length); + int newCapacity = _matches.Length * 2; + if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; + if (newCapacity < min) newCapacity = min; + Capacity = newCapacity; + } + + /// + /// Gets the size of the children buffer in the internal tree structure + /// You might be looking for + /// Exposing this might help in deducing more efficient initial parameters + /// + public int ChildrenCount => _childrenIndex; + /// + /// Gets or sets the capacity of the internal children buffer + /// You might be looking for + /// + public int ChildrenCapacity + { + get + { + return _children.Length; + } + set + { + if (value < _childrenIndex) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionReason.SmallCapacity); + + if (value != _childrenIndex) + { + int[] newChildren = new int[value]; + if (_childrenIndex > 0) + { + Array.Copy(_children, 0, newChildren, 0, _childrenIndex); + } + + // Set new odd indexes to -1 + for (int i = _childrenIndex + 1; i < newChildren.Length; i += 2) + newChildren[i] = -1; + + _children = newChildren; + } + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void EnsureChildrenCapacity(int min) + { + if (_children.Length < min) + { + EnsureChildrenCapacityRare(min); + } + Debug.Assert(_children.Length >= min); + } + [MethodImpl(MethodImplOptions.NoInlining)] + private void EnsureChildrenCapacityRare(int min) + { + // Expansion logic as in System.Collections.Generic.List + Debug.Assert(min > _children.Length); + Debug.Assert(_childrenIndex % 2 == 0); + int newCapacity = _children.Length * 2; + if ((uint)min > int.MaxValue) newCapacity = int.MaxValue; + if (newCapacity < min) newCapacity = min; + ChildrenCapacity = newCapacity; + } + + #endregion Size and Capacity + + #region RootChar + + // Inspired by Markdig's CharacterMap + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool TryGetRoot(char rootChar, out int rootNodeIndex) + { + if (rootChar < 128) + { + rootNodeIndex = _asciiRootMap[rootChar]; + return rootNodeIndex != -1; + } + + if (_unicodeRootMap != null) + { + return _unicodeRootMap.TryGetValue(rootChar, out rootNodeIndex); + } + + rootNodeIndex = -1; + return false; + } + private void SetRootChar(char rootChar) + { + if (rootChar < 128) + { + Debug.Assert(_asciiRootMap[rootChar] == -1); + _asciiRootMap[rootChar] = TreeSize; + } + else + { + if (_unicodeRootMap == null) + { + _unicodeRootMap = new Dictionary(); + } + _unicodeRootMap.Add(rootChar, TreeSize); + } + } + private readonly int[] _asciiRootMap = new int[128]; + private Dictionary _unicodeRootMap; + + #endregion RootChar + + private void Init(int matchCapacity, int treeCapacity, int childrenCapacity) + { + for (int i = 0; i < _asciiRootMap.Length; i++) + _asciiRootMap[i] = -1; + + _matches = matchCapacity == 0 ? _emptyMatches : new KeyValuePair[matchCapacity]; + _tree = treeCapacity == 0 ? _emptyTree : new Node[treeCapacity]; + EnsureChildrenCapacity(childrenCapacity); + } + + /// + /// Constructs a new with no initial prefixes + /// + public CompactPrefixTree(int matchCapacity = 0, int treeCapacity = 0, int childrenCapacity = 0) + { + Init(matchCapacity, treeCapacity, childrenCapacity); + } + /// + /// Constructs a new with the supplied matches + /// + /// Matches to initialize the with. For best lookup performance, this collection should be sorted. + public CompactPrefixTree(ICollection> input) + { + if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input); + + Init(input.Count, input.Count * 2, input.Count * 2); + + using (var e = input.GetEnumerator()) + { + for (int i = 0; i < input.Count; i++) + { + e.MoveNext(); + TryInsert(e.Current, InsertionBehavior.ThrowOnExisting); + } + } + } + + #region this[] accessors + + /// + /// Retrieves the key/value pair at the specified index (must be lower than ) + /// + /// Index of pair to get, must be lower than (the order is the same as the order in which the elements were added) + /// The key/value pair of the element at the specified index + public KeyValuePair this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if ((uint)index >= (uint)Count) ThrowHelper.ThrowIndexOutOfRangeException(); + return _matches[index]; + } + } + + /// + /// Gets or sets the value associated with the specified key + /// + /// The key of the value to get or set + /// The value of the element with the specified key + public TValue this[string key] + { + get + { + if (TryMatchExact(key.AsSpan(), out KeyValuePair match)) + return match.Value; + throw new KeyNotFoundException(key); + } + set + { + var pair = new KeyValuePair(key, value); + bool modified = TryInsert(in pair, InsertionBehavior.OverwriteExisting); + Debug.Assert(modified); + } + } // Get, Set + + /// + /// Gets the value associated with the specified key + /// + /// The key of the value to get + /// The key/value pair of the element with the specified key + public KeyValuePair this[ReadOnlySpan key] + { + get + { + if (TryMatchExact(key, out KeyValuePair match)) + return match; + throw new KeyNotFoundException(key.ToString()); + } + } // Get only + + #endregion this[] accessors + + #region Add, TryAdd + + /// + /// Adds the specified key/value pair to the + /// + /// The key of the element to add + /// The value of the element to add + public void Add(string key, TValue value) + { + var pair = new KeyValuePair(key, value); + TryInsert(in pair, InsertionBehavior.ThrowOnExisting); + } + /// + /// Adds the specified key/value pair to the + /// + /// The key/value pair to add + public void Add(KeyValuePair pair) + => TryInsert(in pair, InsertionBehavior.ThrowOnExisting); + + /// + /// Tries to add the key/value pair to the if the key is not yet present + /// + /// The key of the element to add + /// The value of the element to add + /// True if the element was added, false otherwise + public bool TryAdd(string key, TValue value) + { + var pair = new KeyValuePair(key, value); + return TryInsert(in pair, InsertionBehavior.None); + } + /// + /// Tries to add the key/value pair to the if the key is not yet present + /// + /// The pair to add + /// True if the element was added, false otherwise + public bool TryAdd(KeyValuePair pair) + => TryInsert(in pair, InsertionBehavior.None); + + #endregion Add, TryAdd + + #region Insert internal + + private bool TryInsert(in KeyValuePair pair, InsertionBehavior behavior) + { + string key = pair.Key; + if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); + if (key.Length == 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.key, ExceptionReason.String_Empty); + Debug.Assert(!string.IsNullOrEmpty(key)); + + char rootChar = key[0]; + if (TryGetRoot(rootChar, out int rootNodeIndex)) + { + var tree = _tree; + ref Node node = ref tree[rootNodeIndex]; + for (int i = 1; i < key.Length; i++) + { + char c = key[i]; + + if (node.ChildChar == c) + { + node = ref tree[node.ChildIndex]; + continue; + } + + if (node.Children == -1) + { + // This could be a leaf node + if (node.ChildChar == 0) + { + // This is a leaf node + int previousMatchIndex = node.MatchIndex; + ref KeyValuePair previousMatch = ref _matches[previousMatchIndex]; + string previousKey = previousMatch.Key; + + // Find out for how long the two keys continue to share a prefix + int previousIndex = i; + int minLength = Math.Min(key.Length, previousKey.Length); + for (; i < minLength; i++) + { + // We haven't checked the i-th character of key so far + if (key[i] != previousKey[i]) break; + } + + if (i == minLength && key.Length == previousKey.Length) + { + // The two keys are of the same length and i has reached the length of the key => duplicate + Debug.Assert(i == key.Length && i == previousKey.Length); + Debug.Assert(key == previousKey); + goto HandleDuplicateKey; + } + + // Clear the match index on the current node as it's no longer a leaf node + node.MatchIndex = -1; + + // insert (i - previousIndex) intermediary nodes (could be 0) + int intermediaryNodesToInsert = i - previousIndex; + if (intermediaryNodesToInsert > 0) + { + node.ChildIndex = TreeSize; + node.ChildChar = key[previousIndex]; + EnsureTreeCapacity(TreeSize + intermediaryNodesToInsert); + tree = _tree; + for (int j = 0; j < intermediaryNodesToInsert - 1; j++) + { + tree[TreeSize + j] = new Node() + { + Char = previousKey[previousIndex + j], + ChildChar = previousKey[previousIndex + j + 1], + ChildIndex = TreeSize + j + 1, + MatchIndex = -1, + Children = -1 + }; + } + TreeSize += intermediaryNodesToInsert; + tree[TreeSize - 1] = new Node() + { + Char = previousKey[previousIndex + intermediaryNodesToInsert - 1], + MatchIndex = -1, + Children = -1 + }; + node = ref tree[TreeSize - 1]; + } + + node.ChildIndex = TreeSize; + + // Insert the pair + EnsureCapacity(Count + 1); + _matches[Count] = pair; + + // One of the strings could be a prefix of the other, in that case we're only inserting one leaf node + if (i == minLength) + { + Debug.Assert(key.Length != previousKey.Length); + if (previousKey.Length < key.Length) // If the input was sorted, this should be hit + { + Debug.Assert(key.StartsWith(previousKey)); + node.ChildChar = key[i]; + node.MatchIndex = previousMatchIndex; + EnsureTreeCapacity(TreeSize + 1); + _tree[TreeSize] = new Node() + { + Char = key[i], + MatchIndex = Count, + Children = -1 + }; + } + else // if key.Length < previousKey.Length + { + Debug.Assert(key.Length < previousKey.Length); + Debug.Assert(previousKey.StartsWith(key)); + node.ChildChar = previousKey[i]; + node.MatchIndex = Count; + EnsureTreeCapacity(TreeSize + 1); + _tree[TreeSize] = new Node() + { + Char = previousKey[i], + MatchIndex = previousMatchIndex, + Children = -1 + }; + } + Count++; + TreeSize++; + return true; + } + + // Insert two leaf nodes + Debug.Assert(node.Char != 0 && node.Char == previousKey[i - 1]); + Debug.Assert(node.MatchIndex == -1); + Debug.Assert(node.Children == -1); + + node.ChildChar = previousKey[i]; + node.Children = _childrenIndex; + + EnsureChildrenCapacity(_childrenIndex + 2); + _children[_childrenIndex] = TreeSize + 1; + _childrenIndex += 2; + + // Insert the two leaf nodes + EnsureTreeCapacity(TreeSize + 2); + _tree[TreeSize] = new Node() + { + Char = previousKey[i], + MatchIndex = previousMatchIndex, + Children = -1 + }; + _tree[TreeSize + 1] = new Node() + { + Char = key[i], + MatchIndex = Count, + Children = -1 + }; + + Count++; + TreeSize += 2; + return true; + } + else + { + // This node has a child char, therefore we either don't have a match attached or that match is simply a prefix of the current key + Debug.Assert(node.MatchIndex == -1 || key.StartsWith(_matches[node.MatchIndex].Key)); + + // Set this pair as the current node's first element in the Children list + node.Children = _childrenIndex; + EnsureChildrenCapacity(_childrenIndex + 2); + _children[_childrenIndex] = TreeSize; + _childrenIndex += 2; + + InsertLeafNode(in pair, c); + return true; + } + } + else + { + // Look for a child node with a matching Char in all of children + var children = _children; + int childrenIndex = node.Children; + int lastChildrenIndex = childrenIndex; + do + { + if ((uint)childrenIndex >= (uint)children.Length) + break; + node = ref _tree[children[childrenIndex]]; + if (node.Char == c) goto NextChar; + lastChildrenIndex = childrenIndex; + childrenIndex = children[childrenIndex + 1]; + } + while (true); + + // A child node was not found, add a new one to children + EnsureChildrenCapacity(_childrenIndex + 2); + _children[lastChildrenIndex + 1] = _childrenIndex; + _children[_childrenIndex] = TreeSize; + _childrenIndex += 2; + + InsertLeafNode(in pair, c); + return true; + } + + NextChar:; + } + + // We have found our final node, check if a match already claimed this node + if (node.MatchIndex != -1) + { + ref KeyValuePair previousMatch = ref _matches[node.MatchIndex]; + + // Either some other key is the leaf here, or the key is duplicated + if (previousMatch.Key.Length == key.Length) + { + Debug.Assert(previousMatch.Key == key); + goto HandleDuplicateKey; + } + else + { + // It's not a duplicate but shares key.Length characters, therefore it's longer + // This will never occur if the input was sorted + Debug.Assert(previousMatch.Key.Length > key.Length); + Debug.Assert(previousMatch.Key.StartsWith(key)); + Debug.Assert(node.ChildChar == 0 && node.Children == -1); + + // It is a leaf node + // Move the prevMatch one node inward + int previousMatchIndex = node.MatchIndex; + node.MatchIndex = Count; + node.ChildChar = previousMatch.Key[key.Length]; + node.ChildIndex = TreeSize; + EnsureTreeCapacity(TreeSize + 1); + _tree[TreeSize] = new Node() + { + Char = previousMatch.Key[key.Length], + MatchIndex = previousMatchIndex, + Children = -1 + }; + TreeSize++; + + // Set the pair as a match on this node + } + } + + // Set the pair as a match on this node + node.MatchIndex = Count; // This might be modifying a forgotten node reference, but in that case it was already set + EnsureCapacity(Count + 1); + _matches[Count] = pair; + Count++; + return true; + + HandleDuplicateKey:; + Debug.Assert(key == _matches[node.MatchIndex].Key); + if (behavior == InsertionBehavior.None) return false; + if (behavior == InsertionBehavior.OverwriteExisting) + { + _matches[node.MatchIndex] = pair; + return true; + } + Debug.Assert(behavior == InsertionBehavior.ThrowOnExisting); + ThrowHelper.ThrowArgumentException(ExceptionArgument.key, ExceptionReason.DuplicateKey); + Debug.Assert(false, "Should throw by now"); + return false; + } + else // if the root character is not yet in the collection + { + SetRootChar(rootChar); + InsertLeafNode(in pair, rootChar); + return true; + } + } + private void InsertLeafNode(in KeyValuePair pair, char nodeChar) + { + EnsureCapacity(Count + 1); + _matches[Count] = pair; + + EnsureTreeCapacity(TreeSize + 1); + _tree[TreeSize] = new Node() + { + Char = nodeChar, + MatchIndex = Count, + Children = -1 + }; + + Count++; + TreeSize++; + } + + #endregion Insert internal + + #region TryMatch longest + + /// + /// Tries to find the longest prefix of text, that is contained in this + /// + /// The text in which to search for the prefix + /// The found prefix and the corresponding value + /// True if a match was found, false otherwise + public bool TryMatchLongest(ReadOnlySpan text, out KeyValuePair match) + { + match = default; + if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) + return false; + + int matchIndex = -1; + int depth = 1; + + ref Node node = ref _tree[nodeIndex]; + if (node.ChildChar == 0) goto LeafNodeFound; + if (node.MatchIndex != -1) matchIndex = node.MatchIndex; + + for (int i = 1; i < text.Length; i++) + { + char c = text[i]; + + if (node.ChildChar == c) + { + node = ref _tree[node.ChildIndex]; + goto NextChar; + } + + var children = _children; + int childrenIndex = node.Children; + do + { + if ((uint)childrenIndex >= (uint)children.Length) + goto Return; + node = ref _tree[children[childrenIndex]]; + if (node.Char == c) goto NextChar; + childrenIndex = children[childrenIndex + 1]; + } + while (true); + + NextChar:; + depth++; + if (node.ChildChar == 0) goto LeafNodeFound; + if (node.MatchIndex != -1) matchIndex = node.MatchIndex; + } + // We have ran out of our string, return the longest match we've found + goto Return; + + LeafNodeFound:; + ref KeyValuePair possibleMatch = ref _matches[node.MatchIndex]; + if (possibleMatch.Key.Length <= text.Length) + { + // Check that the rest of the strings match + if (text.Slice(depth).StartsWith(possibleMatch.Key.AsSpan(depth), StringComparison.Ordinal)) + { + matchIndex = node.MatchIndex; + } + } + + Return:; + if (matchIndex != -1) + { + match = _matches[matchIndex]; + return true; + } + return false; + } + + #endregion TryMatch longest + + #region TryMatch exact + + /// + /// Tries to find a prefix of text, that is contained in this and is exactly text.Length characters long + /// + /// The text in which to search for the prefix + /// The found prefix and the corresponding value + /// True if a match was found, false otherwise + public bool TryMatchExact(ReadOnlySpan text, out KeyValuePair match) + { + match = default; + if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) + return false; + + int depth = 1; + + ref Node node = ref _tree[nodeIndex]; + if (node.ChildChar == 0) goto LeafNodeFound; + if (node.MatchIndex != -1 && text.Length == 1) + { + match = _matches[node.MatchIndex]; + return true; + } + + for (int i = 1; i < text.Length; i++) + { + char c = text[i]; + + if (node.ChildChar == c) + { + node = ref _tree[node.ChildIndex]; + goto NextChar; + } + + var children = _children; + int childrenIndex = node.Children; + do + { + if ((uint)childrenIndex >= (uint)children.Length) + return false; + node = ref _tree[children[childrenIndex]]; + if (node.Char == c) goto NextChar; + childrenIndex = children[childrenIndex + 1]; + } + while (true); + + NextChar:; + depth++; + if (node.ChildChar == 0) goto LeafNodeFound; + } + + if (node.MatchIndex == -1) return false; + match = _matches[node.MatchIndex]; + Debug.Assert(match.Key.Length == text.Length); + return true; + + LeafNodeFound:; + match = _matches[node.MatchIndex]; + + return match.Key.Length == text.Length && + text.Slice(depth).Equals(match.Key.AsSpan(depth), StringComparison.Ordinal); + } + + #endregion TryMatch exact + + #region TryMatch shortest + + /// + /// Tries to find the shortest prefix of text, that is contained in this + /// + /// The text in which to search for the prefix + /// The found prefix and the corresponding value + /// True if a match was found, false otherwise + public bool TryMatchShortest(ReadOnlySpan text, out KeyValuePair match) + { + match = default; + if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex)) + return false; + + ref Node node = ref _tree[nodeIndex]; + if (node.MatchIndex != -1) + { + match = _matches[node.MatchIndex]; + return true; + } + + for (int i = 1; i < text.Length; i++) + { + char c = text[i]; + + if (node.ChildChar == c) + { + node = ref _tree[node.ChildIndex]; + goto NextChar; + } + + var children = _children; + int childrenIndex = node.Children; + do + { + if ((uint)childrenIndex >= (uint)children.Length) + return false; + node = ref _tree[children[childrenIndex]]; + if (node.Char == c) goto NextChar; + childrenIndex = children[childrenIndex + 1]; + } + while (true); + + NextChar:; + if (node.MatchIndex != -1) + { + match = _matches[node.MatchIndex]; + return true; + } + } + Debug.Assert(node.MatchIndex == -1); + return false; + } + + #endregion TryMatch shortest + + #region Interface implementations + + /// + /// Determines whether the contains the specified key + /// + /// The key to locate in this + /// True if the key is contained in this PrefixTree, false otherwise. + public bool ContainsKey(string key) + => TryMatchExact(key.AsSpan(), out _); + + /// + /// Gets the value associated with the specified key + /// + /// The key of the value to get + /// The value associated with the specified key + /// True if the key is contained in this PrefixTree, false otherwise. + public bool TryGetValue(string key, out TValue value) + { + bool ret = TryMatchExact(key.AsSpan(), out KeyValuePair match); + value = match.Value; + return ret; + } + + /// + /// Gets a collection containing the keys in this + /// + public IEnumerable Keys + { + get + { + for (int i = 0; i < Count; i++) + yield return _matches[i].Key; + } + } + /// + /// Gets a collection containing the values in this + /// + public IEnumerable Values + { + get + { + for (int i = 0; i < Count; i++) + yield return _matches[i].Value; + } + } + + /// + /// Returns an Enumerator that iterates through the . + /// Use the index accessor instead () + /// + /// + public IEnumerator> GetEnumerator() => new Enumerator(_matches); + + IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_matches); + + /// + /// Enumerates the elements of a + /// + public struct Enumerator : IEnumerator>, IEnumerator + { + private readonly KeyValuePair[] _matches; + private int _index; + + internal Enumerator(KeyValuePair[] matches) + { + _matches = matches; + _index = -1; + } + + /// + /// Increments the internal index + /// + /// True if the index is less than the length of the internal array + public bool MoveNext() => ++_index < _matches.Length; + /// + /// Gets the at the current position + /// + public KeyValuePair Current => _matches[_index]; + object IEnumerator.Current => _matches[_index]; + + /// + /// Does nothing + /// + public void Dispose() { } + + /// + /// Resets the internal index to the beginning of the array + /// + public void Reset() + { + _index = -1; + } + } + + #endregion Interface implementations + } +} diff --git a/src/Markdig/Helpers/LinkHelper.cs b/src/Markdig/Helpers/LinkHelper.cs index ab09db7b..67278a5e 100644 --- a/src/Markdig/Helpers/LinkHelper.cs +++ b/src/Markdig/Helpers/LinkHelper.cs @@ -1,7 +1,7 @@ // 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; using System.Runtime.CompilerServices; using Markdig.Syntax; @@ -300,17 +300,17 @@ namespace Markdig.Helpers } // Chars valid for both scheme and email - if (c <= 127) - { - if (c > ' ' && c != '>') - { - builder.Append(c); - } - else break; + if (c <= 127) + { + if (c > ' ' && c != '>') + { + builder.Append(c); + } + else break; } - else if (!c.IsSpaceOrPunctuation()) - { - builder.Append(c); + else if (!c.IsSpaceOrPunctuation()) + { + builder.Append(c); } else break; } @@ -550,9 +550,9 @@ namespace Markdig.Helpers continue; } - if (c.IsNewLine()) - { - break; + if (c.IsNewLine()) + { + break; } hasEscape = false; @@ -663,55 +663,55 @@ namespace Markdig.Helpers return c == '\0' || c.IsSpaceOrTab() || c.IsControl() || (isAutoLink && c == '<'); // TODO: specs unclear. space is strict or relaxed? (includes tabs?) } - public static bool IsValidDomain(string link, int prefixLength) - { - // https://github.github.com/gfm/#extended-www-autolink - // A valid domain consists of alphanumeric characters, underscores (_), hyphens (-) and periods (.). - // There must be at least one period, and no underscores may be present in the last two segments of the domain. - - // Extended as of https://github.com/lunet-io/markdig/issues/316 to accept non-ascii characters, - // as long as they are not in the space or punctuation categories - - int segmentCount = 1; - bool segmentHasCharacters = false; - int lastUnderscoreSegment = -1; - - for (int i = prefixLength; i < link.Length; i++) - { - char c = link[i]; - - if (c == '.') // New segment - { - if (!segmentHasCharacters) - return false; - - segmentCount++; - segmentHasCharacters = false; - continue; - } - - if (!c.IsAlphaNumeric()) - { - if (c == '/' || c == '?' || c == '#' || c == ':') // End of domain name - break; - - if (c == '_') - { - lastUnderscoreSegment = segmentCount; - } - else if (c != '-' && c.IsSpaceOrPunctuation()) - { - // An invalid character has been found - return false; - } - } - - segmentHasCharacters = true; - } - - return segmentCount != 1 && // At least one dot was present - segmentHasCharacters && // Last segment has valid characters - segmentCount - lastUnderscoreSegment >= 2; // No underscores are present in the last two segments of the domain + public static bool IsValidDomain(string link, int prefixLength) + { + // https://github.github.com/gfm/#extended-www-autolink + // A valid domain consists of alphanumeric characters, underscores (_), hyphens (-) and periods (.). + // There must be at least one period, and no underscores may be present in the last two segments of the domain. + + // Extended as of https://github.com/lunet-io/markdig/issues/316 to accept non-ascii characters, + // as long as they are not in the space or punctuation categories + + int segmentCount = 1; + bool segmentHasCharacters = false; + int lastUnderscoreSegment = -1; + + for (int i = prefixLength; i < link.Length; i++) + { + char c = link[i]; + + if (c == '.') // New segment + { + if (!segmentHasCharacters) + return false; + + segmentCount++; + segmentHasCharacters = false; + continue; + } + + if (!c.IsAlphaNumeric()) + { + if (c == '/' || c == '?' || c == '#' || c == ':') // End of domain name + break; + + if (c == '_') + { + lastUnderscoreSegment = segmentCount; + } + else if (c != '-' && c.IsSpaceOrPunctuation()) + { + // An invalid character has been found + return false; + } + } + + segmentHasCharacters = true; + } + + return segmentCount != 1 && // At least one dot was present + segmentHasCharacters && // Last segment has valid characters + segmentCount - lastUnderscoreSegment >= 2; // No underscores are present in the last two segments of the domain } public static bool TryParseLinkReferenceDefinition(T text, out string label, out string url, diff --git a/src/Markdig/Helpers/ThrowHelper.cs b/src/Markdig/Helpers/ThrowHelper.cs index 84e509c8..a0b8d1ce 100644 --- a/src/Markdig/Helpers/ThrowHelper.cs +++ b/src/Markdig/Helpers/ThrowHelper.cs @@ -1,93 +1,93 @@ -using System; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; - -namespace Markdig.Helpers -{ - /// - /// Inspired by CoreLib, taken from https://github.com/MihaZupan/SharpCollections, cc @MihaZupan - /// - [ExcludeFromCodeCoverage] - internal static class ThrowHelper - { - public static void ArgumentNullException(string paramName) => throw new ArgumentNullException(paramName); - public static void ArgumentNullException_item() => throw new ArgumentNullException("item"); - public static void ArgumentNullException_text() => throw new ArgumentNullException("text"); - public static void ArgumentNullException_label() => throw new ArgumentNullException("label"); - public static void ArgumentNullException_key() => throw new ArgumentNullException("key"); - public static void ArgumentNullException_name() => throw new ArgumentNullException("name"); - public static void ArgumentNullException_markdown() => throw new ArgumentNullException("markdown"); - public static void ArgumentNullException_writer() => throw new ArgumentNullException("writer"); - public static void ArgumentNullException_leafBlock() => throw new ArgumentNullException("leafBlock"); - public static void ArgumentNullException_markdownObject() => throw new ArgumentNullException("markdownObject"); - - public static void ArgumentException(string message) => throw new ArgumentException(message); - public static void ArgumentException(string message, string paramName) => throw new ArgumentException(message, paramName); - - public static void ArgumentOutOfRangeException(string paramName) => throw new ArgumentOutOfRangeException(paramName); - public static void ArgumentOutOfRangeException(string message, string paramName) => throw new ArgumentOutOfRangeException(message, paramName); - public static void ArgumentOutOfRangeException_index() => throw new ArgumentOutOfRangeException("index"); - - public static void InvalidOperationException(string message) => throw new InvalidOperationException(message); - - public static void ThrowArgumentNullException(ExceptionArgument argument) - { - throw new ArgumentNullException(argument.ToString()); - } - - public static void ThrowArgumentException(ExceptionArgument argument, ExceptionReason reason) - { - throw new ArgumentException(argument.ToString(), GetExceptionReason(reason)); - } - - public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionReason reason) - { - throw new ArgumentOutOfRangeException(argument.ToString(), GetExceptionReason(reason)); - } - - public static void ThrowIndexOutOfRangeException() - { - throw new IndexOutOfRangeException(); - } - - private static string GetExceptionReason(ExceptionReason reason) - { - switch (reason) - { - case ExceptionReason.String_Empty: - return "String must not be empty."; - - case ExceptionReason.SmallCapacity: - return "Capacity was less than the current size."; - - case ExceptionReason.InvalidOffsetLength: - return "Offset and length must refer to a position in the string."; - - case ExceptionReason.DuplicateKey: - return "The given key is already present in the dictionary."; - - default: - Debug.Assert(false, "The enum value is not defined, please check the ExceptionReason Enum."); - return ""; - } - } - } - - internal enum ExceptionArgument - { - key, - input, - value, - length, - offsetLength, - text - } - - internal enum ExceptionReason - { - String_Empty, - SmallCapacity, - InvalidOffsetLength, - DuplicateKey, - } -} +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace Markdig.Helpers +{ + /// + /// Inspired by CoreLib, taken from https://github.com/MihaZupan/SharpCollections, cc @MihaZupan + /// + [ExcludeFromCodeCoverage] + internal static class ThrowHelper + { + public static void ArgumentNullException(string paramName) => throw new ArgumentNullException(paramName); + public static void ArgumentNullException_item() => throw new ArgumentNullException("item"); + public static void ArgumentNullException_text() => throw new ArgumentNullException("text"); + public static void ArgumentNullException_label() => throw new ArgumentNullException("label"); + public static void ArgumentNullException_key() => throw new ArgumentNullException("key"); + public static void ArgumentNullException_name() => throw new ArgumentNullException("name"); + public static void ArgumentNullException_markdown() => throw new ArgumentNullException("markdown"); + public static void ArgumentNullException_writer() => throw new ArgumentNullException("writer"); + public static void ArgumentNullException_leafBlock() => throw new ArgumentNullException("leafBlock"); + public static void ArgumentNullException_markdownObject() => throw new ArgumentNullException("markdownObject"); + + public static void ArgumentException(string message) => throw new ArgumentException(message); + public static void ArgumentException(string message, string paramName) => throw new ArgumentException(message, paramName); + + public static void ArgumentOutOfRangeException(string paramName) => throw new ArgumentOutOfRangeException(paramName); + public static void ArgumentOutOfRangeException(string message, string paramName) => throw new ArgumentOutOfRangeException(message, paramName); + public static void ArgumentOutOfRangeException_index() => throw new ArgumentOutOfRangeException("index"); + + public static void InvalidOperationException(string message) => throw new InvalidOperationException(message); + + public static void ThrowArgumentNullException(ExceptionArgument argument) + { + throw new ArgumentNullException(argument.ToString()); + } + + public static void ThrowArgumentException(ExceptionArgument argument, ExceptionReason reason) + { + throw new ArgumentException(argument.ToString(), GetExceptionReason(reason)); + } + + public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionReason reason) + { + throw new ArgumentOutOfRangeException(argument.ToString(), GetExceptionReason(reason)); + } + + public static void ThrowIndexOutOfRangeException() + { + throw new IndexOutOfRangeException(); + } + + private static string GetExceptionReason(ExceptionReason reason) + { + switch (reason) + { + case ExceptionReason.String_Empty: + return "String must not be empty."; + + case ExceptionReason.SmallCapacity: + return "Capacity was less than the current size."; + + case ExceptionReason.InvalidOffsetLength: + return "Offset and length must refer to a position in the string."; + + case ExceptionReason.DuplicateKey: + return "The given key is already present in the dictionary."; + + default: + Debug.Assert(false, "The enum value is not defined, please check the ExceptionReason Enum."); + return ""; + } + } + } + + internal enum ExceptionArgument + { + key, + input, + value, + length, + offsetLength, + text + } + + internal enum ExceptionReason + { + String_Empty, + SmallCapacity, + InvalidOffsetLength, + DuplicateKey, + } +} diff --git a/src/Markdig/Markdig.csproj b/src/Markdig/Markdig.csproj index 87e8a68b..d7894be2 100644 --- a/src/Markdig/Markdig.csproj +++ b/src/Markdig/Markdig.csproj @@ -1,6 +1,6 @@ - - - Markdig - - - + + + Markdig + + + diff --git a/src/Markdig/MarkdownExtensions.cs b/src/Markdig/MarkdownExtensions.cs index 831008fe..74a8e0aa 100644 --- a/src/Markdig/MarkdownExtensions.cs +++ b/src/Markdig/MarkdownExtensions.cs @@ -34,8 +34,8 @@ using Markdig.Extensions.Yaml; using Markdig.Parsers; using Markdig.Parsers.Inlines; using Markdig.Extensions.Globalization; -using Markdig.Helpers; - +using Markdig.Helpers; + namespace Markdig { /// @@ -93,8 +93,8 @@ namespace Markdig .UseDiagrams() .UseAutoLinks() .UseGenericAttributes(); // Must be last as it is one parser that is modifying other parsers - } - + } + /// /// Uses this extension to enable autolinks from text `http://`, `https://`, `ftp://`, `mailto:`, `www.xxx.yyy` /// @@ -633,19 +633,19 @@ namespace Markdig return pipeline; } - /// - /// Disables parsing of ATX and Setex headings - /// - /// The pipeline. + /// + /// Disables parsing of ATX and Setex headings + /// + /// The pipeline. /// The modified pipeline - public static MarkdownPipelineBuilder DisableHeadings(this MarkdownPipelineBuilder pipeline) - { - pipeline.BlockParsers.TryRemove(); - if (pipeline.BlockParsers.TryFind(out var parser)) - { - parser.ParseSetexHeadings = false; - } - return pipeline; + public static MarkdownPipelineBuilder DisableHeadings(this MarkdownPipelineBuilder pipeline) + { + pipeline.BlockParsers.TryRemove(); + if (pipeline.BlockParsers.TryFind(out var parser)) + { + parser.ParseSetexHeadings = false; + } + return pipeline; } } } diff --git a/src/Markdig/MarkdownPipelineBuilder.cs b/src/Markdig/MarkdownPipelineBuilder.cs index e835b831..c9d4d295 100644 --- a/src/Markdig/MarkdownPipelineBuilder.cs +++ b/src/Markdig/MarkdownPipelineBuilder.cs @@ -114,9 +114,9 @@ namespace Markdig new BlockParserList(BlockParsers), new InlineParserList(InlineParsers), DebugLog, - GetDocumentProcessed) - { - PreciseSourceLocation = PreciseSourceLocation + GetDocumentProcessed) + { + PreciseSourceLocation = PreciseSourceLocation }; return pipeline; } diff --git a/src/Markdig/Parsers/FencedBlockParserBase.cs b/src/Markdig/Parsers/FencedBlockParserBase.cs index b78fb23c..f25e7eda 100644 --- a/src/Markdig/Parsers/FencedBlockParserBase.cs +++ b/src/Markdig/Parsers/FencedBlockParserBase.cs @@ -1,243 +1,243 @@ -// 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 Markdig.Helpers; -using Markdig.Renderers.Html; -using Markdig.Syntax; - -namespace Markdig.Parsers -{ - public abstract class FencedBlockParserBase : BlockParser, IAttributesParseable - { - /// - /// Delegate used to parse the string on the first line after the fenced code block special characters (usually ` or ~) - /// - /// The parser processor. - /// The being processed line. - /// The fenced code block. - /// The opening character for the fenced code block (usually ` or ~) - /// true if parsing of the line is successfull; false otherwise - public delegate bool InfoParserDelegate(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter); - - - /// - /// Gets or sets the information parser. - /// - public InfoParserDelegate InfoParser { get; set; } - - /// - /// A delegates that allows to process attached attributes - /// - public TryParseAttributesDelegate TryParseAttributes { get; set; } - } - - /// - /// Base parser for fenced blocks (opened by 3 or more character delimiters on a first line, and closed by at least the same number of delimiters) - /// - /// - public abstract class FencedBlockParserBase : FencedBlockParserBase where T : Block, IFencedBlock - { - - /// - /// Initializes a new instance of the class. - /// - protected FencedBlockParserBase() - { - InfoParser = DefaultInfoParser; - MinimumMatchCount = 3; - MaximumMatchCount = Int32.MaxValue; - } - - /// - /// Gets or sets the language prefix (default is "language-") - /// - public string InfoPrefix { get; set; } - - public int MinimumMatchCount { get; set; } - - public int MaximumMatchCount { get; set; } - - /// - /// The default parser for the information after the fenced code block special characters (usually ` or ~) - /// - /// The parser processor. - /// The line. - /// The fenced code block. - /// true if parsing of the line is successfull; false otherwise - public static bool DefaultInfoParser(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter) - { - string infoString; - string argString = null; - - // An info string cannot contain any backticks (unless it is a tilde block) - int firstSpace = -1; - if (openingCharacter == '`') - { - for (int i = line.Start; i <= line.End; i++) - { - char c = line.Text[i]; - if (c == '`') - { - return false; - } - - if (firstSpace < 0 && c.IsSpaceOrTab()) - { - firstSpace = i; - } - } - } - else - { - for (int i = line.Start; i <= line.End; i++) - { - if (line.Text[i].IsSpaceOrTab()) - { - firstSpace = i; - break; - } - } - } - - if (firstSpace > 0) - { - infoString = line.Text.Substring(line.Start, firstSpace - line.Start).Trim(); - - // Skip any spaces after info string - firstSpace++; - while (firstSpace <= line.End) - { - char c = line[firstSpace]; - if (c.IsSpaceOrTab()) - { - firstSpace++; - } - else - { - break; - } - } - - argString = line.Text.Substring(firstSpace, line.End - firstSpace + 1).Trim(); - } - else - { - var lineCopy = line; - lineCopy.Trim(); - infoString = lineCopy.ToString(); - } - - fenced.Info = HtmlHelper.Unescape(infoString); - fenced.Arguments = HtmlHelper.Unescape(argString); - - return true; - } - - public override BlockState TryOpen(BlockProcessor processor) - { - // We expect no indentation for a fenced code block. - if (processor.IsCodeIndent) - { - return BlockState.None; - } - - var startPosition = processor.Start; - - // Match fenced char - int count = 0; - var line = processor.Line; - char c = line.CurrentChar; - var matchChar = c; - while (c != '\0') - { - if (c != matchChar) - { - break; - } - count++; - c = line.NextChar(); - } - - // A fenced codeblock requires at least 3 opening chars - if (count < MinimumMatchCount || count > MaximumMatchCount) - { - return BlockState.None; - } - - // specs spaces: Is space and tabs? or only spaces? Use space and tab for this case - line.TrimStart(); - - var fenced = CreateFencedBlock(processor); - { - fenced.Column = processor.Column; - fenced.FencedChar = matchChar; - fenced.FencedCharCount = count; - fenced.Span.Start = startPosition; - fenced.Span.End = line.Start; - }; - - // Try to parse any attached attributes - TryParseAttributes?.Invoke(processor, ref line, fenced); - - // If the info parser was not successfull, early exit - if (InfoParser != null && !InfoParser(processor, ref line, fenced, matchChar)) - { - return BlockState.None; - } - - // Add the language as an attribute by default - if (!string.IsNullOrEmpty(fenced.Info)) - { - if (string.IsNullOrEmpty(InfoPrefix)) - { - fenced.GetAttributes().AddClass(fenced.Info); - } - else - { - fenced.GetAttributes().AddClass(InfoPrefix + fenced.Info); - } - } - - // Store the number of matched string into the context - processor.NewBlocks.Push(fenced); - - // Discard the current line as it is already parsed - return BlockState.ContinueDiscard; - } - - protected abstract T CreateFencedBlock(BlockProcessor processor); - - public override BlockState TryContinue(BlockProcessor processor, Block block) - { - var fence = (IFencedBlock)block; - var count = fence.FencedCharCount; - var matchChar = fence.FencedChar; - var c = processor.CurrentChar; - - // Match if we have a closing fence - var line = processor.Line; - while (c == matchChar) - { - c = line.NextChar(); - count--; - } - - // If we have a closing fence, close it and discard the current line - // The line must contain only fence opening character followed only by whitespaces. - if (count <= 0 && !processor.IsCodeIndent && (c == '\0' || c.IsWhitespace()) && line.TrimEnd()) - { - block.UpdateSpanEnd(line.Start - 1); - - // Don't keep the last line - return BlockState.BreakDiscard; - } - - // Reset the indentation to the column before the indent - processor.GoToColumn(processor.ColumnBeforeIndent); - - return BlockState.Continue; - } - } +// 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 Markdig.Helpers; +using Markdig.Renderers.Html; +using Markdig.Syntax; + +namespace Markdig.Parsers +{ + public abstract class FencedBlockParserBase : BlockParser, IAttributesParseable + { + /// + /// Delegate used to parse the string on the first line after the fenced code block special characters (usually ` or ~) + /// + /// The parser processor. + /// The being processed line. + /// The fenced code block. + /// The opening character for the fenced code block (usually ` or ~) + /// true if parsing of the line is successfull; false otherwise + public delegate bool InfoParserDelegate(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter); + + + /// + /// Gets or sets the information parser. + /// + public InfoParserDelegate InfoParser { get; set; } + + /// + /// A delegates that allows to process attached attributes + /// + public TryParseAttributesDelegate TryParseAttributes { get; set; } + } + + /// + /// Base parser for fenced blocks (opened by 3 or more character delimiters on a first line, and closed by at least the same number of delimiters) + /// + /// + public abstract class FencedBlockParserBase : FencedBlockParserBase where T : Block, IFencedBlock + { + + /// + /// Initializes a new instance of the class. + /// + protected FencedBlockParserBase() + { + InfoParser = DefaultInfoParser; + MinimumMatchCount = 3; + MaximumMatchCount = Int32.MaxValue; + } + + /// + /// Gets or sets the language prefix (default is "language-") + /// + public string InfoPrefix { get; set; } + + public int MinimumMatchCount { get; set; } + + public int MaximumMatchCount { get; set; } + + /// + /// The default parser for the information after the fenced code block special characters (usually ` or ~) + /// + /// The parser processor. + /// The line. + /// The fenced code block. + /// true if parsing of the line is successfull; false otherwise + public static bool DefaultInfoParser(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter) + { + string infoString; + string argString = null; + + // An info string cannot contain any backticks (unless it is a tilde block) + int firstSpace = -1; + if (openingCharacter == '`') + { + for (int i = line.Start; i <= line.End; i++) + { + char c = line.Text[i]; + if (c == '`') + { + return false; + } + + if (firstSpace < 0 && c.IsSpaceOrTab()) + { + firstSpace = i; + } + } + } + else + { + for (int i = line.Start; i <= line.End; i++) + { + if (line.Text[i].IsSpaceOrTab()) + { + firstSpace = i; + break; + } + } + } + + if (firstSpace > 0) + { + infoString = line.Text.Substring(line.Start, firstSpace - line.Start).Trim(); + + // Skip any spaces after info string + firstSpace++; + while (firstSpace <= line.End) + { + char c = line[firstSpace]; + if (c.IsSpaceOrTab()) + { + firstSpace++; + } + else + { + break; + } + } + + argString = line.Text.Substring(firstSpace, line.End - firstSpace + 1).Trim(); + } + else + { + var lineCopy = line; + lineCopy.Trim(); + infoString = lineCopy.ToString(); + } + + fenced.Info = HtmlHelper.Unescape(infoString); + fenced.Arguments = HtmlHelper.Unescape(argString); + + return true; + } + + public override BlockState TryOpen(BlockProcessor processor) + { + // We expect no indentation for a fenced code block. + if (processor.IsCodeIndent) + { + return BlockState.None; + } + + var startPosition = processor.Start; + + // Match fenced char + int count = 0; + var line = processor.Line; + char c = line.CurrentChar; + var matchChar = c; + while (c != '\0') + { + if (c != matchChar) + { + break; + } + count++; + c = line.NextChar(); + } + + // A fenced codeblock requires at least 3 opening chars + if (count < MinimumMatchCount || count > MaximumMatchCount) + { + return BlockState.None; + } + + // specs spaces: Is space and tabs? or only spaces? Use space and tab for this case + line.TrimStart(); + + var fenced = CreateFencedBlock(processor); + { + fenced.Column = processor.Column; + fenced.FencedChar = matchChar; + fenced.FencedCharCount = count; + fenced.Span.Start = startPosition; + fenced.Span.End = line.Start; + }; + + // Try to parse any attached attributes + TryParseAttributes?.Invoke(processor, ref line, fenced); + + // If the info parser was not successfull, early exit + if (InfoParser != null && !InfoParser(processor, ref line, fenced, matchChar)) + { + return BlockState.None; + } + + // Add the language as an attribute by default + if (!string.IsNullOrEmpty(fenced.Info)) + { + if (string.IsNullOrEmpty(InfoPrefix)) + { + fenced.GetAttributes().AddClass(fenced.Info); + } + else + { + fenced.GetAttributes().AddClass(InfoPrefix + fenced.Info); + } + } + + // Store the number of matched string into the context + processor.NewBlocks.Push(fenced); + + // Discard the current line as it is already parsed + return BlockState.ContinueDiscard; + } + + protected abstract T CreateFencedBlock(BlockProcessor processor); + + public override BlockState TryContinue(BlockProcessor processor, Block block) + { + var fence = (IFencedBlock)block; + var count = fence.FencedCharCount; + var matchChar = fence.FencedChar; + var c = processor.CurrentChar; + + // Match if we have a closing fence + var line = processor.Line; + while (c == matchChar) + { + c = line.NextChar(); + count--; + } + + // If we have a closing fence, close it and discard the current line + // The line must contain only fence opening character followed only by whitespaces. + if (count <= 0 && !processor.IsCodeIndent && (c == '\0' || c.IsWhitespace()) && line.TrimEnd()) + { + block.UpdateSpanEnd(line.Start - 1); + + // Don't keep the last line + return BlockState.BreakDiscard; + } + + // Reset the indentation to the column before the indent + processor.GoToColumn(processor.ColumnBeforeIndent); + + return BlockState.Continue; + } + } } \ No newline at end of file diff --git a/src/Markdig/Parsers/HeadingBlockParser.cs b/src/Markdig/Parsers/HeadingBlockParser.cs index 183454f5..bb9b23d4 100644 --- a/src/Markdig/Parsers/HeadingBlockParser.cs +++ b/src/Markdig/Parsers/HeadingBlockParser.cs @@ -1,7 +1,7 @@ // 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.Diagnostics; +using System.Diagnostics; using Markdig.Helpers; using Markdig.Syntax; @@ -22,8 +22,8 @@ namespace Markdig.Parsers OpeningCharacters = new[] {'#'}; } - /// - /// Gets or sets the max count of the leading unescaped # characters + /// + /// Gets or sets the max count of the leading unescaped # characters /// public int MaxLeadingCount { get; set; } = 6; diff --git a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs index 10825169..6e550e32 100644 --- a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs @@ -1,126 +1,126 @@ -// 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; -using Markdig.Syntax.Inlines; - -namespace Markdig.Parsers.Inlines -{ - /// - /// An inline parser for a . - /// - /// - public class CodeInlineParser : InlineParser - { - /// - /// Initializes a new instance of the class. - /// - public CodeInlineParser() - { - OpeningCharacters = new[] { '`' }; - } - - public override bool Match(InlineProcessor processor, ref StringSlice slice) - { - var match = slice.CurrentChar; - if (slice.PeekCharExtra(-1) == match) - { - return false; - } - - var startPosition = slice.Start; - - int openSticks = 0; - int closeSticks = 0; - - // Match the opened sticks - char c = slice.CurrentChar; - while (c == match) - { - openSticks++; - c = slice.NextChar(); - } - - var builder = StringBuilderCache.Local(); - - // A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick. - // A code span begins with a backtick string and ends with a backtick string of equal length. - // The contents of the code span are the characters between the two backtick strings, normalized in the following ways: - - // 1. line endings are converted to spaces. - - // 2. If the resulting string both begins AND ends with a space character, but does not consist entirely - // of space characters, a single space character is removed from the front and back. - // This allows you to include code that begins or ends with backtick characters, which must be separated by - // whitespace from the opening or closing backtick strings. - - bool allSpace = true; - - while (c != '\0') - { - // Transform '\n' into a single space - if (c == '\n') - { - c = ' '; - } - - if (c == match) - { - do - { - closeSticks++; - c = slice.NextChar(); - } - while (c == match); - - if (openSticks == closeSticks) - { - break; - } - - allSpace = false; - builder.Append(match, closeSticks); - closeSticks = 0; - } - else - { - builder.Append(c); - if (c != ' ') - { - allSpace = false; - } - c = slice.NextChar(); - } - } - - bool isMatching = false; - if (closeSticks == openSticks) - { - string content; - - // Remove one space from front and back if the string is not all spaces - if (!allSpace && builder.Length > 2 && builder[0] == ' ' && builder[builder.Length - 1] == ' ') - { - content = builder.ToString(1, builder.Length - 2); - } - else - { - content = builder.ToString(); - } - - processor.Inline = new CodeInline() - { - Delimiter = match, - Content = content, - Span = new SourceSpan(processor.GetSourcePosition(startPosition, out int line, out int column), processor.GetSourcePosition(slice.Start - 1)), - Line = line, - Column = column - }; - isMatching = true; - } - - return isMatching; - } - } +// 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; +using Markdig.Syntax.Inlines; + +namespace Markdig.Parsers.Inlines +{ + /// + /// An inline parser for a . + /// + /// + public class CodeInlineParser : InlineParser + { + /// + /// Initializes a new instance of the class. + /// + public CodeInlineParser() + { + OpeningCharacters = new[] { '`' }; + } + + public override bool Match(InlineProcessor processor, ref StringSlice slice) + { + var match = slice.CurrentChar; + if (slice.PeekCharExtra(-1) == match) + { + return false; + } + + var startPosition = slice.Start; + + int openSticks = 0; + int closeSticks = 0; + + // Match the opened sticks + char c = slice.CurrentChar; + while (c == match) + { + openSticks++; + c = slice.NextChar(); + } + + var builder = StringBuilderCache.Local(); + + // A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick. + // A code span begins with a backtick string and ends with a backtick string of equal length. + // The contents of the code span are the characters between the two backtick strings, normalized in the following ways: + + // 1. line endings are converted to spaces. + + // 2. If the resulting string both begins AND ends with a space character, but does not consist entirely + // of space characters, a single space character is removed from the front and back. + // This allows you to include code that begins or ends with backtick characters, which must be separated by + // whitespace from the opening or closing backtick strings. + + bool allSpace = true; + + while (c != '\0') + { + // Transform '\n' into a single space + if (c == '\n') + { + c = ' '; + } + + if (c == match) + { + do + { + closeSticks++; + c = slice.NextChar(); + } + while (c == match); + + if (openSticks == closeSticks) + { + break; + } + + allSpace = false; + builder.Append(match, closeSticks); + closeSticks = 0; + } + else + { + builder.Append(c); + if (c != ' ') + { + allSpace = false; + } + c = slice.NextChar(); + } + } + + bool isMatching = false; + if (closeSticks == openSticks) + { + string content; + + // Remove one space from front and back if the string is not all spaces + if (!allSpace && builder.Length > 2 && builder[0] == ' ' && builder[builder.Length - 1] == ' ') + { + content = builder.ToString(1, builder.Length - 2); + } + else + { + content = builder.ToString(); + } + + processor.Inline = new CodeInline() + { + Delimiter = match, + Content = content, + Span = new SourceSpan(processor.GetSourcePosition(startPosition, out int line, out int column), processor.GetSourcePosition(slice.Start - 1)), + Line = line, + Column = column + }; + isMatching = true; + } + + return isMatching; + } + } } \ No newline at end of file diff --git a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs index 98a2c1e0..215729e2 100644 --- a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; +using System.Diagnostics; using Markdig.Helpers; using Markdig.Renderers.Html; using Markdig.Syntax; @@ -133,8 +133,8 @@ namespace Markdig.Parsers.Inlines } public override bool Match(InlineProcessor processor, ref StringSlice slice) - { - // First, some definitions. + { + // First, some definitions. // A delimiter run is a sequence of one or more delimiter characters that is not preceded or followed by the same delimiter character // The amount of delimiter characters in the delimiter run may exceed emphasisDesc.MaximumCount, as that is handeled in `ProcessEmphasis` @@ -153,9 +153,9 @@ namespace Markdig.Parsers.Inlines { pc = slice.PeekCharExtra(-1); if (pc == delimiterChar && slice.PeekCharExtra(-2) != '\\') - { - // If we get here, we determined that either: - // a) there weren't enough delimiters in the delimiter run to satisfy the MinimumCount condition + { + // If we get here, we determined that either: + // a) there weren't enough delimiters in the delimiter run to satisfy the MinimumCount condition // b) the previous character couldn't open/close return false; } @@ -221,8 +221,8 @@ namespace Markdig.Parsers.Inlines // 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]; - // Skip delimiters not supported by this instance + var closeDelimiter = delimiters[i]; + // Skip delimiters not supported by this instance EmphasisDescriptor emphasisDesc = emphasisMap[closeDelimiter.DelimiterChar]; if (emphasisDesc == null) { @@ -258,38 +258,38 @@ namespace Markdig.Parsers.Inlines } if (openDelimiter != null) - { + { process_delims: Debug.Assert(openDelimiter.DelimiterCount >= emphasisDesc.MinimumCount, "Extra emphasis should have been discarded by now"); Debug.Assert(closeDelimiter.DelimiterCount >= emphasisDesc.MinimumCount, "Extra emphasis should have been discarded by now"); - int delimiterDelta = Math.Min(Math.Min(openDelimiter.DelimiterCount, closeDelimiter.DelimiterCount), emphasisDesc.MaximumCount); - - // Insert an emph or strong emph node accordingly, after the text node corresponding to the opener. - EmphasisInline emphasis = null; - { - if (delimiterDelta <= 2) // We can try using the legacy delegate - { - #pragma warning disable CS0618 // Support fields marked as obsolete - emphasis = CreateEmphasisInline?.Invoke(closeDelimiter.DelimiterChar, isStrong: delimiterDelta == 2); - #pragma warning restore CS0618 // Support fields marked as obsolete - } - if (emphasis == null) - { - // Go in backwards order to give priority to newer delegates - for (int delegateIndex = TryCreateEmphasisInlineList.Count - 1; delegateIndex >= 0; delegateIndex--) - { - emphasis = TryCreateEmphasisInlineList[delegateIndex].Invoke(closeDelimiter.DelimiterChar, delimiterDelta); - if (emphasis != null) break; - } - - if (emphasis == null) - { - emphasis = new EmphasisInline() - { - DelimiterChar = closeDelimiter.DelimiterChar, - DelimiterCount = delimiterDelta - }; - } + int delimiterDelta = Math.Min(Math.Min(openDelimiter.DelimiterCount, closeDelimiter.DelimiterCount), emphasisDesc.MaximumCount); + + // Insert an emph or strong emph node accordingly, after the text node corresponding to the opener. + EmphasisInline emphasis = null; + { + if (delimiterDelta <= 2) // We can try using the legacy delegate + { + #pragma warning disable CS0618 // Support fields marked as obsolete + emphasis = CreateEmphasisInline?.Invoke(closeDelimiter.DelimiterChar, isStrong: delimiterDelta == 2); + #pragma warning restore CS0618 // Support fields marked as obsolete + } + if (emphasis == null) + { + // Go in backwards order to give priority to newer delegates + for (int delegateIndex = TryCreateEmphasisInlineList.Count - 1; delegateIndex >= 0; delegateIndex--) + { + emphasis = TryCreateEmphasisInlineList[delegateIndex].Invoke(closeDelimiter.DelimiterChar, delimiterDelta); + if (emphasis != null) break; + } + + if (emphasis == null) + { + emphasis = new EmphasisInline() + { + DelimiterChar = closeDelimiter.DelimiterChar, + DelimiterCount = delimiterDelta + }; + } } } Debug.Assert(emphasis != null); @@ -356,12 +356,12 @@ namespace Markdig.Parsers.Inlines { goto process_delims; } - else if (openDelimiter.DelimiterCount > 0) - { - // There are still delimiter characters left, there's just not enough of them - openDelimiter.ReplaceBy(openDelimiter.AsLiteralInline()); - delimiters.RemoveAt(openDelimiterIndex); - i--; + else if (openDelimiter.DelimiterCount > 0) + { + // There are still delimiter characters left, there's just not enough of them + openDelimiter.ReplaceBy(openDelimiter.AsLiteralInline()); + delimiters.RemoveAt(openDelimiterIndex); + i--; } else { diff --git a/src/Markdig/Renderers/Html/Inlines/EmphasisInlineRenderer.cs b/src/Markdig/Renderers/Html/Inlines/EmphasisInlineRenderer.cs index 11f499a4..8dd1dda7 100644 --- a/src/Markdig/Renderers/Html/Inlines/EmphasisInlineRenderer.cs +++ b/src/Markdig/Renderers/Html/Inlines/EmphasisInlineRenderer.cs @@ -2,8 +2,8 @@ // 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; -using System.Diagnostics; - +using System.Diagnostics; + namespace Markdig.Renderers.Html.Inlines { /// diff --git a/src/Markdig/Renderers/HtmlRenderer.cs b/src/Markdig/Renderers/HtmlRenderer.cs index befe5501..9074a15d 100644 --- a/src/Markdig/Renderers/HtmlRenderer.cs +++ b/src/Markdig/Renderers/HtmlRenderer.cs @@ -2,11 +2,11 @@ // 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.Globalization; +using System; +using System.Globalization; using System.IO; using System.Runtime.CompilerServices; -using System.Text; +using System.Text; using Markdig.Helpers; using Markdig.Renderers.Html; using Markdig.Renderers.Html.Inlines; @@ -191,8 +191,8 @@ namespace Markdig.Renderers Write(content, previousOffset, end - previousOffset); return this; - } - + } + private static readonly IdnMapping IdnMapping = new IdnMapping(); /// @@ -215,54 +215,54 @@ namespace Markdig.Renderers content = LinkRewriter(content); } - int previousPosition = 0; + int previousPosition = 0; // ab://c.d = 8 chars int schemeOffset = content.Length < 8 ? -1 : content.IndexOf("://", 2, StringComparison.Ordinal); - if (schemeOffset != -1) // This is an absolute URL - { - schemeOffset += 3; // skip :// - Write(content, 0, schemeOffset); - - bool idnaEncodeDomain = false; - int endOfDomain = schemeOffset; - for (; endOfDomain < content.Length; endOfDomain++) - { - char c = content[endOfDomain]; - if (c == '/' || c == '?' || c == '#' || c == ':') // End of domain part - { - break; - } - if (c > 127) - { - idnaEncodeDomain = true; - } - } - - if (idnaEncodeDomain) - { - string domainName = IdnMapping.GetAscii(content, schemeOffset, endOfDomain - schemeOffset); - - // Escape the characters (see Commonmark example 327 and think of it with a non-ascii symbol) - previousPosition = 0; - for (int i = 0; i < domainName.Length; i++) - { - var escape = HtmlHelper.EscapeUrlCharacter(domainName[i]); - if (escape != null) - { - Write(domainName, previousPosition, i - previousPosition); - previousPosition = i + 1; - Write(escape); - } - } - Write(domainName, previousPosition, domainName.Length - previousPosition); - - previousPosition = endOfDomain; - } - else - { - previousPosition = schemeOffset; // Don't write anything as we might need to escape it - } + if (schemeOffset != -1) // This is an absolute URL + { + schemeOffset += 3; // skip :// + Write(content, 0, schemeOffset); + + bool idnaEncodeDomain = false; + int endOfDomain = schemeOffset; + for (; endOfDomain < content.Length; endOfDomain++) + { + char c = content[endOfDomain]; + if (c == '/' || c == '?' || c == '#' || c == ':') // End of domain part + { + break; + } + if (c > 127) + { + idnaEncodeDomain = true; + } + } + + if (idnaEncodeDomain) + { + string domainName = IdnMapping.GetAscii(content, schemeOffset, endOfDomain - schemeOffset); + + // Escape the characters (see Commonmark example 327 and think of it with a non-ascii symbol) + previousPosition = 0; + for (int i = 0; i < domainName.Length; i++) + { + var escape = HtmlHelper.EscapeUrlCharacter(domainName[i]); + if (escape != null) + { + Write(domainName, previousPosition, i - previousPosition); + previousPosition = i + 1; + Write(escape); + } + } + Write(domainName, previousPosition, domainName.Length - previousPosition); + + previousPosition = endOfDomain; + } + else + { + previousPosition = schemeOffset; // Don't write anything as we might need to escape it + } } for (var i = previousPosition; i < content.Length; i++) @@ -409,6 +409,6 @@ namespace Markdig.Renderers } } return this; - } + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs index d6e5ece6..66419a31 100644 --- a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs +++ b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs @@ -3,7 +3,7 @@ // See the license.txt file in the project root for more information. using System; -using Markdig.Helpers; +using Markdig.Helpers; using Markdig.Parsers; using Markdig.Parsers.Inlines; @@ -50,16 +50,16 @@ namespace Markdig.Syntax.Inlines return DelimiterCount > 0 ? new string(DelimiterChar, DelimiterCount) : string.Empty; } - public LiteralInline AsLiteralInline() - { - return new LiteralInline() - { - Content = new StringSlice(ToLiteral()), - IsClosed = true, - Span = Span, - Line = Line, - Column = Column - }; + public LiteralInline AsLiteralInline() + { + return new LiteralInline() + { + Content = new StringSlice(ToLiteral()), + IsClosed = true, + Span = Span, + Line = Line, + Column = Column + }; } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/Inlines/EmphasisInline.cs b/src/Markdig/Syntax/Inlines/EmphasisInline.cs index 65dd5a1c..1fa6c1f3 100644 --- a/src/Markdig/Syntax/Inlines/EmphasisInline.cs +++ b/src/Markdig/Syntax/Inlines/EmphasisInline.cs @@ -2,7 +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; using System.Diagnostics; namespace Markdig.Syntax.Inlines @@ -24,14 +24,14 @@ namespace Markdig.Syntax.Inlines /// Marked obsolete as EmphasisInline can now be represented by more than two delimiter characters /// [Obsolete("Use `DelimiterCount == 2` instead", error: false)] - public bool IsDouble - { - get => DelimiterCount == 2; - set => DelimiterCount = value ? 2 : 1; + public bool IsDouble + { + get => DelimiterCount == 2; + set => DelimiterCount = value ? 2 : 1; } - /// - /// Gets or sets the number of delimiter characters for this emphasis. + /// + /// Gets or sets the number of delimiter characters for this emphasis. /// public int DelimiterCount { get; set; } } diff --git a/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs b/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs index 42da166b..c3b6a0f0 100644 --- a/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs +++ b/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs @@ -2,7 +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 Markdig.Helpers; +using Markdig.Helpers; using System; using System.Collections.Generic; @@ -29,14 +29,14 @@ namespace Markdig.Syntax public void Set(string label, LinkReferenceDefinition link) { - if (link == null) ThrowHelper.ArgumentNullException(nameof(link)); - if (!Contains(link)) - { - Add(link); - if (!Links.ContainsKey(label)) - { - Links[label] = link; - } + if (link == null) ThrowHelper.ArgumentNullException(nameof(link)); + if (!Contains(link)) + { + Add(link); + if (!Links.ContainsKey(label)) + { + Links[label] = link; + } } } diff --git a/src/Markdig/Syntax/ListItemBlock.cs b/src/Markdig/Syntax/ListItemBlock.cs index ec5c6f2a..d086a732 100644 --- a/src/Markdig/Syntax/ListItemBlock.cs +++ b/src/Markdig/Syntax/ListItemBlock.cs @@ -21,8 +21,8 @@ namespace Markdig.Syntax internal int ColumnWidth { get; set; } - /// - /// The number defined for this in an ordered list + /// + /// The number defined for this in an ordered list /// public int Order { get; set; } } diff --git a/src/Markdig/Syntax/MarkdownDocument.cs b/src/Markdig/Syntax/MarkdownDocument.cs index 7e091df9..f0853e21 100644 --- a/src/Markdig/Syntax/MarkdownDocument.cs +++ b/src/Markdig/Syntax/MarkdownDocument.cs @@ -1,8 +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.Collections.Generic; - +using System.Collections.Generic; + namespace Markdig.Syntax { /// @@ -18,14 +18,14 @@ namespace Markdig.Syntax { } - /// - /// Gets the number of lines in this + /// + /// Gets the number of lines in this /// public int LineCount; - /// - /// Gets a list of zero-based indexes of line beginnings in the source span - /// Available if is used, otherwise null + /// + /// Gets a list of zero-based indexes of line beginnings in the source span + /// Available if is used, otherwise null /// public List LineStartIndexes; } diff --git a/src/Markdig/Syntax/MarkdownObjectExtensions.cs b/src/Markdig/Syntax/MarkdownObjectExtensions.cs index 012bfb29..618dc924 100644 --- a/src/Markdig/Syntax/MarkdownObjectExtensions.cs +++ b/src/Markdig/Syntax/MarkdownObjectExtensions.cs @@ -1,175 +1,175 @@ -// 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.Diagnostics; -using Markdig.Syntax.Inlines; - -namespace Markdig.Syntax -{ - /// - /// Extensions for visiting or - /// - public static class MarkdownObjectExtensions - { - /// - /// Iterates over the descendant elements for the specified markdown element, including and . - /// The descendant elements are returned in DFS-like order. - /// - /// The markdown object. - /// An iteration over the descendant elements - public static IEnumerable Descendants(this MarkdownObject markdownObject) - { - Stack stack = new Stack(); - Stack pushStack = new Stack(); - - stack.Push(markdownObject); - pushStack.Push(false); - - while (stack.Count > 0) - { - var block = stack.Pop(); - if (pushStack.Pop()) yield return block; - if (block is ContainerBlock containerBlock) - { - int subBlockIndex = containerBlock.Count; - while (subBlockIndex-- > 0) - { - var subBlock = containerBlock[subBlockIndex]; - if (subBlock is LeafBlock leafBlock) - { - if (leafBlock.Inline != null) - { - stack.Push(leafBlock.Inline); - pushStack.Push(false); - } - } - stack.Push(subBlock); - pushStack.Push(true); - } - } - else if (block is ContainerInline containerInline) - { - var child = containerInline.LastChild; - while (child != null) - { - stack.Push(child); - pushStack.Push(true); - child = child.PreviousSibling; - } - } - } - } - - /// - /// Iterates over the descendant elements for the specified markdown element, including and and filters by the type . - /// The descendant elements are returned in DFS-like order. - /// - /// Type to use for filtering the descendants - /// The markdown object. - /// An iteration over the descendant elements - public static IEnumerable Descendants(this MarkdownObject markdownObject) where T : MarkdownObject - { - if (typeof(T).IsSubclassOf(typeof(Block))) - { - if (markdownObject is ContainerBlock containerBlock && containerBlock.Count > 0) - { - return BlockDescendantsInternal(containerBlock); - } - } - else // typeof(T).IsSubclassOf(typeof(Inline))) - { - if (markdownObject is ContainerBlock containerBlock) - { - if (containerBlock.Count > 0) - { - return InlineDescendantsInternal(containerBlock); - } - } - else if (markdownObject is ContainerInline containerInline && containerInline.FirstChild != null) - { - return containerInline.FindDescendantsInternal(); - } - } - - return Array.Empty(); - } - - /// - /// Iterates over the descendant elements for the specified markdown element and filters by the type . - /// - /// Type to use for filtering the descendants - /// The inline markdown object. - /// - /// An iteration over the descendant elements - /// - public static IEnumerable Descendants(this ContainerInline inline) where T : Inline - => inline.FindDescendants(); - - /// - /// Iterates over the descendant elements for the specified markdown element and filters by the type . - /// - /// Type to use for filtering the descendants - /// The markdown object. - /// - /// An iteration over the descendant elements - /// - public static IEnumerable Descendants(this ContainerBlock block) where T : Block - { - if (block != null && block.Count > 0) - { - return BlockDescendantsInternal(block); - } - else - { - return Array.Empty(); - } - } - - private static IEnumerable BlockDescendantsInternal(ContainerBlock block) where T : MarkdownObject - { - Debug.Assert(typeof(T).IsSubclassOf(typeof(Block))); - - Stack stack = new Stack(); - - int childrenCount = block.Count; - while (childrenCount-- > 0) - { - stack.Push(block[childrenCount]); - } - - while (stack.Count > 0) - { - var subBlock = stack.Pop(); - if (subBlock is T subBlockT) - { - yield return subBlockT; - } - - if (subBlock is ContainerBlock subBlockContainer) - { - childrenCount = subBlockContainer.Count; - while (childrenCount-- > 0) - { - stack.Push(subBlockContainer[childrenCount]); - } - } - } - } - - private static IEnumerable InlineDescendantsInternal(ContainerBlock block) where T : MarkdownObject - { - Debug.Assert(typeof(T).IsSubclassOf(typeof(Inline))); - - foreach (MarkdownObject descendant in block.Descendants()) - { - if (descendant is T descendantT) - { - yield return descendantT; - } - } - } - } +// 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.Diagnostics; +using Markdig.Syntax.Inlines; + +namespace Markdig.Syntax +{ + /// + /// Extensions for visiting or + /// + public static class MarkdownObjectExtensions + { + /// + /// Iterates over the descendant elements for the specified markdown element, including and . + /// The descendant elements are returned in DFS-like order. + /// + /// The markdown object. + /// An iteration over the descendant elements + public static IEnumerable Descendants(this MarkdownObject markdownObject) + { + Stack stack = new Stack(); + Stack pushStack = new Stack(); + + stack.Push(markdownObject); + pushStack.Push(false); + + while (stack.Count > 0) + { + var block = stack.Pop(); + if (pushStack.Pop()) yield return block; + if (block is ContainerBlock containerBlock) + { + int subBlockIndex = containerBlock.Count; + while (subBlockIndex-- > 0) + { + var subBlock = containerBlock[subBlockIndex]; + if (subBlock is LeafBlock leafBlock) + { + if (leafBlock.Inline != null) + { + stack.Push(leafBlock.Inline); + pushStack.Push(false); + } + } + stack.Push(subBlock); + pushStack.Push(true); + } + } + else if (block is ContainerInline containerInline) + { + var child = containerInline.LastChild; + while (child != null) + { + stack.Push(child); + pushStack.Push(true); + child = child.PreviousSibling; + } + } + } + } + + /// + /// Iterates over the descendant elements for the specified markdown element, including and and filters by the type . + /// The descendant elements are returned in DFS-like order. + /// + /// Type to use for filtering the descendants + /// The markdown object. + /// An iteration over the descendant elements + public static IEnumerable Descendants(this MarkdownObject markdownObject) where T : MarkdownObject + { + if (typeof(T).IsSubclassOf(typeof(Block))) + { + if (markdownObject is ContainerBlock containerBlock && containerBlock.Count > 0) + { + return BlockDescendantsInternal(containerBlock); + } + } + else // typeof(T).IsSubclassOf(typeof(Inline))) + { + if (markdownObject is ContainerBlock containerBlock) + { + if (containerBlock.Count > 0) + { + return InlineDescendantsInternal(containerBlock); + } + } + else if (markdownObject is ContainerInline containerInline && containerInline.FirstChild != null) + { + return containerInline.FindDescendantsInternal(); + } + } + + return Array.Empty(); + } + + /// + /// Iterates over the descendant elements for the specified markdown element and filters by the type . + /// + /// Type to use for filtering the descendants + /// The inline markdown object. + /// + /// An iteration over the descendant elements + /// + public static IEnumerable Descendants(this ContainerInline inline) where T : Inline + => inline.FindDescendants(); + + /// + /// Iterates over the descendant elements for the specified markdown element and filters by the type . + /// + /// Type to use for filtering the descendants + /// The markdown object. + /// + /// An iteration over the descendant elements + /// + public static IEnumerable Descendants(this ContainerBlock block) where T : Block + { + if (block != null && block.Count > 0) + { + return BlockDescendantsInternal(block); + } + else + { + return Array.Empty(); + } + } + + private static IEnumerable BlockDescendantsInternal(ContainerBlock block) where T : MarkdownObject + { + Debug.Assert(typeof(T).IsSubclassOf(typeof(Block))); + + Stack stack = new Stack(); + + int childrenCount = block.Count; + while (childrenCount-- > 0) + { + stack.Push(block[childrenCount]); + } + + while (stack.Count > 0) + { + var subBlock = stack.Pop(); + if (subBlock is T subBlockT) + { + yield return subBlockT; + } + + if (subBlock is ContainerBlock subBlockContainer) + { + childrenCount = subBlockContainer.Count; + while (childrenCount-- > 0) + { + stack.Push(subBlockContainer[childrenCount]); + } + } + } + } + + private static IEnumerable InlineDescendantsInternal(ContainerBlock block) where T : MarkdownObject + { + Debug.Assert(typeof(T).IsSubclassOf(typeof(Inline))); + + foreach (MarkdownObject descendant in block.Descendants()) + { + if (descendant is T descendantT) + { + yield return descendantT; + } + } + } + } } \ No newline at end of file diff --git a/src/Markdig/Syntax/SourceSpan.cs b/src/Markdig/Syntax/SourceSpan.cs index e9cecd3b..b62ca5d5 100644 --- a/src/Markdig/Syntax/SourceSpan.cs +++ b/src/Markdig/Syntax/SourceSpan.cs @@ -43,9 +43,9 @@ namespace Markdig.Syntax public bool IsEmpty => Start > End; - public SourceSpan MoveForward(int count) - { - return new SourceSpan(Start + count, End + count); + public SourceSpan MoveForward(int count) + { + return new SourceSpan(Start + count, End + count); } public bool Equals(SourceSpan other) diff --git a/src/SpecFileGen/Program.cs b/src/SpecFileGen/Program.cs index e07bc7b7..74cde2e2 100644 --- a/src/SpecFileGen/Program.cs +++ b/src/SpecFileGen/Program.cs @@ -1,390 +1,390 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; - -namespace SpecFileGen -{ - class Program - { - static readonly string SpecificationsDirectory = - Path.GetFullPath( - Path.Combine( - Path.GetDirectoryName(typeof(Spec).Assembly.Location), - "../../../../Markdig.Tests/")); - - enum RendererType - { - Html, - Normalize, - PlainText - } - - class Spec - { - public readonly string Name; - public readonly string Path; - public readonly string OutputPath; - public readonly string Extensions; - public readonly RendererType RendererType; - public int TestCount; - - public Spec(string name, string fileName, string extensions, RendererType rendererType = RendererType.Html) - { - Name = name; - Path = SpecificationsDirectory; - if (rendererType == RendererType.Html) Path += "Specs"; - else if (rendererType == RendererType.Normalize) Path += "NormalizeSpecs"; - else if (rendererType == RendererType.PlainText) Path += "PlainTextSpecs"; - Path += "/" + fileName; - OutputPath = System.IO.Path.ChangeExtension(Path, "generated.cs"); - Extensions = extensions; - RendererType = rendererType; - } - } - class NormalizeSpec : Spec - { - public NormalizeSpec(string name, string fileName, string extensions) - : base(name, fileName, extensions, rendererType: RendererType.Normalize) { } - } - class PlainTextSpec : Spec - { - public PlainTextSpec(string name, string fileName, string extensions) - : base(name, fileName, extensions, rendererType: RendererType.PlainText) { } - } - - // NOTE: Beware of Copy/Pasting spec files - some characters may change (non-breaking space into space)! - static readonly Spec[] Specs = new[] - { - new Spec("CommonMark v. 0.29", "CommonMark.md", ""), - new Spec("Pipe Tables", "PipeTableSpecs.md", "pipetables|advanced"), - new Spec("Footnotes", "FootnotesSpecs.md", "footnotes|advanced"), - new Spec("Generic Attributes", "GenericAttributesSpecs.md", "attributes|advanced"), - new Spec("Emphasis Extra", "EmphasisExtraSpecs.md", "emphasisextras|advanced"), - new Spec("Hardline Breaks", "HardlineBreakSpecs.md", "hardlinebreak|advanced+hardlinebreak"), - new Spec("Grid Tables", "GridTableSpecs.md", "gridtables|advanced"), - new Spec("Custom Containers", "CustomContainerSpecs.md", "customcontainers+attributes|advanced"), - new Spec("Definition Lists", "DefinitionListSpecs.md", "definitionlists+attributes|advanced"), - new Spec("Emoji", "EmojiSpecs.md", "emojis|advanced+emojis"), - new Spec("Abbreviations", "AbbreviationSpecs.md", "abbreviations|advanced"), - new Spec("List Extras", "ListExtraSpecs.md", "listextras|advanced"), - new Spec("Math", "MathSpecs.md", "mathematics|advanced"), - new Spec("Bootstrap", "BootstrapSpecs.md", "bootstrap+pipetables+figures+attributes"), - new Spec("Media", "MediaSpecs.md", "medialinks|advanced+medialinks"), - new Spec("Smarty Pants", "SmartyPantsSpecs.md", "pipetables+smartypants|advanced+smartypants"), - new Spec("Auto Identifiers", "AutoIdentifierSpecs.md", "autoidentifiers|advanced"), - new Spec("Task Lists", "TaskListSpecs.md", "tasklists|advanced"), - new Spec("Diagrams", "DiagramsSpecs.md", "diagrams|advanced"), - new Spec("No Html", "NoHtmlSpecs.md", "nohtml"), - new Spec("Yaml", "YamlSpecs.md", "yaml"), - new Spec("Auto Links", "AutoLinks.md", "autolinks|advanced"), - new Spec("Jira Links", "JiraLinks.md", "jiralinks"), - new Spec("Globalization", "GlobalizationSpecs.md", "globalization+advanced+emojis"), - new Spec("Figures, Footers and Cites", "FigureFooterAndCiteSpecs.md", "figures+footers+citations|advanced"), - - new NormalizeSpec("Headings", "Headings.md", ""), - - new PlainTextSpec("Sample", "SamplePlainText.md", ""), - }; - - static void Main() - { - Console.WriteLine("Generating {0} specs ...", Specs.Length); - - bool anyChanged = false; - List missingSpecs = new List(); - int totalTests = 0; - - foreach (var spec in Specs) - { - if (!File.Exists(spec.Path)) - { - missingSpecs.Add(spec); - continue; - } - - string source = ParseSpecification(spec, File.ReadAllText(spec.Path)).Replace("\r\n", "\n", StringComparison.Ordinal); - totalTests += spec.TestCount; - - if (File.Exists(spec.OutputPath)) // If the source hasn't changed, don't bump the generated tag - { - string previousSource = File.ReadAllText(spec.OutputPath).Replace("\r\n", "\n", StringComparison.Ordinal); - int start = previousSource.IndexOf('\n', StringComparison.Ordinal) + 1; - int previousLength = previousSource.Length - start; - if (start != 0 && previousLength == source.Length) - { - if (previousSource.IndexOf(source, start, previousLength, StringComparison.Ordinal) == start) - { - // The source did not change - continue; - } - } - } - - string generated = "// Generated: " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + '\n'; - File.WriteAllText(spec.OutputPath, generated + source); - anyChanged = true; - } - - if (missingSpecs.Count != 0) - { - foreach (var spec in missingSpecs) - { - EmitError("Could not find the specification file at " + spec.Path); - } - Environment.Exit(1); - } - - if (anyChanged && Environment.GetEnvironmentVariable("CI") != null) - { - EmitError("Error - Specification files have changed. You must run SpecFileGen when changing specification files."); - Environment.Exit(1); - } - - Console.WriteLine("There are {0} tests in total", totalTests); - } - static void EmitError(string error) - { - var prevColor = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine(new string('-', error.Length)); - Console.WriteLine(error); - Console.WriteLine(new string('-', error.Length)); - Console.WriteLine(); - Console.ForegroundColor = prevColor; - } - - static readonly StringBuilder StringBuilder = new StringBuilder(1 << 20); // 1 MB - static void Write(string text) - { - StringBuilder.Append(text); - } - static void Line(string text = null) - { - if (text != null) StringBuilder.Append(text); - StringBuilder.Append('\n'); - } - static void Indent(int count = 1) - { - StringBuilder.Append(new string(' ', 4 * count)); - } - - static string ParseSpecification(Spec spec, string specSource) - { - Line(); - Write("// "); Line(new string('-', 32)); - Write("// "); Write(new string(' ', 16 - spec.Name.Length / 2)); Line(spec.Name); - Write("// "); Line(new string('-', 32)); - Line(); - Line("using System;"); - Line("using NUnit.Framework;"); - Line(); - Write("namespace Markdig.Tests.Specs."); - if (spec.RendererType == RendererType.Normalize) Write("Normalize."); - else if (spec.RendererType == RendererType.PlainText) Write("PlainText."); - Line(CompressedName(spec.Name).Replace('.', '_')); - Line("{"); - - var lines = specSource.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); - - bool nameChanged = true; - string name = ""; - string compressedName = ""; - int number = 0; - int commentOffset = 0, commentEnd = 0, markdownOffset = 0, markdownEnd = 0, htmlOffset = 0, htmlEnd = 0; - bool first = true; - LinkedList<(string Heading, string Compressed, int Level)> headings = new LinkedList<(string, string, int)>(); - StringBuilder nameBuilder = new StringBuilder(64); - - int i = 0; - while (i < lines.Length) - { - commentOffset = commentEnd = i; - while (!lines[i].Equals("```````````````````````````````` example", StringComparison.Ordinal)) - { - string line = lines[i]; - if (line.Length > 2 && line[0] == '#') - { - int level = line.IndexOf(' ', StringComparison.Ordinal); - while (headings.Count != 0) - { - if (headings.Last.Value.Level < level) break; - headings.RemoveLast(); - } - string heading = line.Substring(level + 1); - headings.AddLast((heading, CompressedName(heading), level)); - - foreach (var (Heading, _, _) in headings) - nameBuilder.Append(Heading + " / "); - nameBuilder.Length -= 3; - name = nameBuilder.ToString(); - nameBuilder.Length = 0; - - foreach (var (_, Compressed, _) in headings) - nameBuilder.Append(Compressed); - compressedName = nameBuilder.ToString(); - nameBuilder.Length = 0; - - nameChanged = true; - } - i++; - - if (!IsEmpty(line)) - commentEnd = i; - - if (i == lines.Length) - { - if (commentOffset != commentEnd) - { - while (commentOffset < commentEnd && IsEmpty(lines[commentOffset])) commentOffset++; - for (i = commentOffset; i < commentEnd; i++) - { - Indent(2); Write("// "); Line(lines[i]); - } - } - goto End; - } - }; - - markdownOffset = ++i; - while (!(lines[i].Length == 1 && lines[i][0] == '.')) i++; - markdownEnd = i++; - - htmlOffset = i; - while (!lines[i].Equals("````````````````````````````````", StringComparison.Ordinal)) i++; - htmlEnd = i++; - - if (nameChanged) - { - if (!first) - { - Indent(); Line("}"); - Line(); - } - Indent(); Line("[TestFixture]"); - Indent(); Line("public class Test" + compressedName); - Indent(); Line("{"); - first = false; - nameChanged = false; - } - else Line(); - - WriteTest(name, compressedName, ++number, spec.Extensions, lines, - commentOffset, commentEnd, - markdownOffset, markdownEnd, - htmlOffset, htmlEnd, - spec.RendererType); - } - - End: - if (!first) - { - Indent(); Line("}"); - } - Line("}"); - - string source = StringBuilder.ToString(); - StringBuilder.Length = 0; - - spec.TestCount = number; - return source; - } - - static void WriteTest(string name, string compressedName, int number, string extensions, string[] lines, int commentOffset, int commentEnd, int markdownOffset, int markdownEnd, int htmlOffset, int htmlEnd, RendererType rendererType) - { - if (commentOffset != commentEnd) - { - while (commentOffset < commentEnd && IsEmpty(lines[commentOffset])) commentOffset++; - for (int i = commentOffset; i < commentEnd; i++) - { - Indent(2); Write("// "); Line(lines[i]); - } - } - - Indent(2); Line("[Test]"); - Indent(2); Line("public void " + compressedName + "_Example" + number.ToString().PadLeft(3, '0') + "()"); - Indent(2); Line("{"); - Indent(3); Line("// Example " + number); - Indent(3); Line("// Section: " + name); - - Indent(3); Line("//"); - Indent(3); Line("// The following Markdown:"); - for (int i = markdownOffset; i < markdownEnd; i++) - { - Indent(3); Write("// "); Indent(); Line(lines[i]); - } - - Indent(3); Line("//"); - Indent(3); Line("// Should be rendered as:"); - for (int i = htmlOffset; i < htmlEnd; i++) - { - Indent(3); Write("// "); Indent(); Line(lines[i]); - } - if (htmlOffset >= htmlEnd) - { - Indent(3); Write("//"); - } - - Line(); - Indent(3); Line($"Console.WriteLine(\"Example {number}\\nSection {name}\\n\");"); - - Indent(3); - if (rendererType == RendererType.Html) Write("TestParser"); - else if (rendererType == RendererType.Normalize) Write("TestNormalize"); - else if (rendererType == RendererType.PlainText) Write("TestPlainText"); - Write(".TestSpec(\""); - for (int i = markdownOffset; i < markdownEnd; i++) - { - Write(Escape(lines[i])); - if (i != markdownEnd - 1) Write("\\n"); - } - Write("\", \""); - for (int i = htmlOffset; i < htmlEnd; i++) - { - Write(Escape(lines[i])); - if (i != htmlEnd - 1) Write("\\n"); - } - Write("\", \""); - Write(extensions); - Line("\");"); - - Indent(2); Line("}"); - } - static string Escape(string input) - { - return input - .Replace("→", "\t") - .Replace("\\", "\\\\") - .Replace("\"", "\\\"") - .Replace("\0", "\\0") - .Replace("\a", "\\a") - .Replace("\b", "\\b") - .Replace("\f", "\\f") - .Replace("\n", "\\n") - .Replace("\r", "\\r") - .Replace("\t", "\\t") - .Replace("\v", "\\v") - ; - } - static string CompressedName(string name) - { - string compressedName = ""; - foreach (var part in name.Replace(',', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries)) - { - compressedName += char.IsLower(part[0]) - ? char.ToUpper(part[0]) + (part.Length > 1 ? part.Substring(1) : "") - : part; - } - return compressedName; - } - static bool IsEmpty(string str) - { - for (int i = 0; i < str.Length; i++) - { - if (str[i] != ' ') return false; - } - return true; - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace SpecFileGen +{ + class Program + { + static readonly string SpecificationsDirectory = + Path.GetFullPath( + Path.Combine( + Path.GetDirectoryName(typeof(Spec).Assembly.Location), + "../../../../Markdig.Tests/")); + + enum RendererType + { + Html, + Normalize, + PlainText + } + + class Spec + { + public readonly string Name; + public readonly string Path; + public readonly string OutputPath; + public readonly string Extensions; + public readonly RendererType RendererType; + public int TestCount; + + public Spec(string name, string fileName, string extensions, RendererType rendererType = RendererType.Html) + { + Name = name; + Path = SpecificationsDirectory; + if (rendererType == RendererType.Html) Path += "Specs"; + else if (rendererType == RendererType.Normalize) Path += "NormalizeSpecs"; + else if (rendererType == RendererType.PlainText) Path += "PlainTextSpecs"; + Path += "/" + fileName; + OutputPath = System.IO.Path.ChangeExtension(Path, "generated.cs"); + Extensions = extensions; + RendererType = rendererType; + } + } + class NormalizeSpec : Spec + { + public NormalizeSpec(string name, string fileName, string extensions) + : base(name, fileName, extensions, rendererType: RendererType.Normalize) { } + } + class PlainTextSpec : Spec + { + public PlainTextSpec(string name, string fileName, string extensions) + : base(name, fileName, extensions, rendererType: RendererType.PlainText) { } + } + + // NOTE: Beware of Copy/Pasting spec files - some characters may change (non-breaking space into space)! + static readonly Spec[] Specs = new[] + { + new Spec("CommonMark v. 0.29", "CommonMark.md", ""), + new Spec("Pipe Tables", "PipeTableSpecs.md", "pipetables|advanced"), + new Spec("Footnotes", "FootnotesSpecs.md", "footnotes|advanced"), + new Spec("Generic Attributes", "GenericAttributesSpecs.md", "attributes|advanced"), + new Spec("Emphasis Extra", "EmphasisExtraSpecs.md", "emphasisextras|advanced"), + new Spec("Hardline Breaks", "HardlineBreakSpecs.md", "hardlinebreak|advanced+hardlinebreak"), + new Spec("Grid Tables", "GridTableSpecs.md", "gridtables|advanced"), + new Spec("Custom Containers", "CustomContainerSpecs.md", "customcontainers+attributes|advanced"), + new Spec("Definition Lists", "DefinitionListSpecs.md", "definitionlists+attributes|advanced"), + new Spec("Emoji", "EmojiSpecs.md", "emojis|advanced+emojis"), + new Spec("Abbreviations", "AbbreviationSpecs.md", "abbreviations|advanced"), + new Spec("List Extras", "ListExtraSpecs.md", "listextras|advanced"), + new Spec("Math", "MathSpecs.md", "mathematics|advanced"), + new Spec("Bootstrap", "BootstrapSpecs.md", "bootstrap+pipetables+figures+attributes"), + new Spec("Media", "MediaSpecs.md", "medialinks|advanced+medialinks"), + new Spec("Smarty Pants", "SmartyPantsSpecs.md", "pipetables+smartypants|advanced+smartypants"), + new Spec("Auto Identifiers", "AutoIdentifierSpecs.md", "autoidentifiers|advanced"), + new Spec("Task Lists", "TaskListSpecs.md", "tasklists|advanced"), + new Spec("Diagrams", "DiagramsSpecs.md", "diagrams|advanced"), + new Spec("No Html", "NoHtmlSpecs.md", "nohtml"), + new Spec("Yaml", "YamlSpecs.md", "yaml"), + new Spec("Auto Links", "AutoLinks.md", "autolinks|advanced"), + new Spec("Jira Links", "JiraLinks.md", "jiralinks"), + new Spec("Globalization", "GlobalizationSpecs.md", "globalization+advanced+emojis"), + new Spec("Figures, Footers and Cites", "FigureFooterAndCiteSpecs.md", "figures+footers+citations|advanced"), + + new NormalizeSpec("Headings", "Headings.md", ""), + + new PlainTextSpec("Sample", "SamplePlainText.md", ""), + }; + + static void Main() + { + Console.WriteLine("Generating {0} specs ...", Specs.Length); + + bool anyChanged = false; + List missingSpecs = new List(); + int totalTests = 0; + + foreach (var spec in Specs) + { + if (!File.Exists(spec.Path)) + { + missingSpecs.Add(spec); + continue; + } + + string source = ParseSpecification(spec, File.ReadAllText(spec.Path)).Replace("\r\n", "\n", StringComparison.Ordinal); + totalTests += spec.TestCount; + + if (File.Exists(spec.OutputPath)) // If the source hasn't changed, don't bump the generated tag + { + string previousSource = File.ReadAllText(spec.OutputPath).Replace("\r\n", "\n", StringComparison.Ordinal); + int start = previousSource.IndexOf('\n', StringComparison.Ordinal) + 1; + int previousLength = previousSource.Length - start; + if (start != 0 && previousLength == source.Length) + { + if (previousSource.IndexOf(source, start, previousLength, StringComparison.Ordinal) == start) + { + // The source did not change + continue; + } + } + } + + string generated = "// Generated: " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + '\n'; + File.WriteAllText(spec.OutputPath, generated + source); + anyChanged = true; + } + + if (missingSpecs.Count != 0) + { + foreach (var spec in missingSpecs) + { + EmitError("Could not find the specification file at " + spec.Path); + } + Environment.Exit(1); + } + + if (anyChanged && Environment.GetEnvironmentVariable("CI") != null) + { + EmitError("Error - Specification files have changed. You must run SpecFileGen when changing specification files."); + Environment.Exit(1); + } + + Console.WriteLine("There are {0} tests in total", totalTests); + } + static void EmitError(string error) + { + var prevColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine(new string('-', error.Length)); + Console.WriteLine(error); + Console.WriteLine(new string('-', error.Length)); + Console.WriteLine(); + Console.ForegroundColor = prevColor; + } + + static readonly StringBuilder StringBuilder = new StringBuilder(1 << 20); // 1 MB + static void Write(string text) + { + StringBuilder.Append(text); + } + static void Line(string text = null) + { + if (text != null) StringBuilder.Append(text); + StringBuilder.Append('\n'); + } + static void Indent(int count = 1) + { + StringBuilder.Append(new string(' ', 4 * count)); + } + + static string ParseSpecification(Spec spec, string specSource) + { + Line(); + Write("// "); Line(new string('-', 32)); + Write("// "); Write(new string(' ', 16 - spec.Name.Length / 2)); Line(spec.Name); + Write("// "); Line(new string('-', 32)); + Line(); + Line("using System;"); + Line("using NUnit.Framework;"); + Line(); + Write("namespace Markdig.Tests.Specs."); + if (spec.RendererType == RendererType.Normalize) Write("Normalize."); + else if (spec.RendererType == RendererType.PlainText) Write("PlainText."); + Line(CompressedName(spec.Name).Replace('.', '_')); + Line("{"); + + var lines = specSource.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); + + bool nameChanged = true; + string name = ""; + string compressedName = ""; + int number = 0; + int commentOffset = 0, commentEnd = 0, markdownOffset = 0, markdownEnd = 0, htmlOffset = 0, htmlEnd = 0; + bool first = true; + LinkedList<(string Heading, string Compressed, int Level)> headings = new LinkedList<(string, string, int)>(); + StringBuilder nameBuilder = new StringBuilder(64); + + int i = 0; + while (i < lines.Length) + { + commentOffset = commentEnd = i; + while (!lines[i].Equals("```````````````````````````````` example", StringComparison.Ordinal)) + { + string line = lines[i]; + if (line.Length > 2 && line[0] == '#') + { + int level = line.IndexOf(' ', StringComparison.Ordinal); + while (headings.Count != 0) + { + if (headings.Last.Value.Level < level) break; + headings.RemoveLast(); + } + string heading = line.Substring(level + 1); + headings.AddLast((heading, CompressedName(heading), level)); + + foreach (var (Heading, _, _) in headings) + nameBuilder.Append(Heading + " / "); + nameBuilder.Length -= 3; + name = nameBuilder.ToString(); + nameBuilder.Length = 0; + + foreach (var (_, Compressed, _) in headings) + nameBuilder.Append(Compressed); + compressedName = nameBuilder.ToString(); + nameBuilder.Length = 0; + + nameChanged = true; + } + i++; + + if (!IsEmpty(line)) + commentEnd = i; + + if (i == lines.Length) + { + if (commentOffset != commentEnd) + { + while (commentOffset < commentEnd && IsEmpty(lines[commentOffset])) commentOffset++; + for (i = commentOffset; i < commentEnd; i++) + { + Indent(2); Write("// "); Line(lines[i]); + } + } + goto End; + } + }; + + markdownOffset = ++i; + while (!(lines[i].Length == 1 && lines[i][0] == '.')) i++; + markdownEnd = i++; + + htmlOffset = i; + while (!lines[i].Equals("````````````````````````````````", StringComparison.Ordinal)) i++; + htmlEnd = i++; + + if (nameChanged) + { + if (!first) + { + Indent(); Line("}"); + Line(); + } + Indent(); Line("[TestFixture]"); + Indent(); Line("public class Test" + compressedName); + Indent(); Line("{"); + first = false; + nameChanged = false; + } + else Line(); + + WriteTest(name, compressedName, ++number, spec.Extensions, lines, + commentOffset, commentEnd, + markdownOffset, markdownEnd, + htmlOffset, htmlEnd, + spec.RendererType); + } + + End: + if (!first) + { + Indent(); Line("}"); + } + Line("}"); + + string source = StringBuilder.ToString(); + StringBuilder.Length = 0; + + spec.TestCount = number; + return source; + } + + static void WriteTest(string name, string compressedName, int number, string extensions, string[] lines, int commentOffset, int commentEnd, int markdownOffset, int markdownEnd, int htmlOffset, int htmlEnd, RendererType rendererType) + { + if (commentOffset != commentEnd) + { + while (commentOffset < commentEnd && IsEmpty(lines[commentOffset])) commentOffset++; + for (int i = commentOffset; i < commentEnd; i++) + { + Indent(2); Write("// "); Line(lines[i]); + } + } + + Indent(2); Line("[Test]"); + Indent(2); Line("public void " + compressedName + "_Example" + number.ToString().PadLeft(3, '0') + "()"); + Indent(2); Line("{"); + Indent(3); Line("// Example " + number); + Indent(3); Line("// Section: " + name); + + Indent(3); Line("//"); + Indent(3); Line("// The following Markdown:"); + for (int i = markdownOffset; i < markdownEnd; i++) + { + Indent(3); Write("// "); Indent(); Line(lines[i]); + } + + Indent(3); Line("//"); + Indent(3); Line("// Should be rendered as:"); + for (int i = htmlOffset; i < htmlEnd; i++) + { + Indent(3); Write("// "); Indent(); Line(lines[i]); + } + if (htmlOffset >= htmlEnd) + { + Indent(3); Write("//"); + } + + Line(); + Indent(3); Line($"Console.WriteLine(\"Example {number}\\nSection {name}\\n\");"); + + Indent(3); + if (rendererType == RendererType.Html) Write("TestParser"); + else if (rendererType == RendererType.Normalize) Write("TestNormalize"); + else if (rendererType == RendererType.PlainText) Write("TestPlainText"); + Write(".TestSpec(\""); + for (int i = markdownOffset; i < markdownEnd; i++) + { + Write(Escape(lines[i])); + if (i != markdownEnd - 1) Write("\\n"); + } + Write("\", \""); + for (int i = htmlOffset; i < htmlEnd; i++) + { + Write(Escape(lines[i])); + if (i != htmlEnd - 1) Write("\\n"); + } + Write("\", \""); + Write(extensions); + Line("\");"); + + Indent(2); Line("}"); + } + static string Escape(string input) + { + return input + .Replace("→", "\t") + .Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\0", "\\0") + .Replace("\a", "\\a") + .Replace("\b", "\\b") + .Replace("\f", "\\f") + .Replace("\n", "\\n") + .Replace("\r", "\\r") + .Replace("\t", "\\t") + .Replace("\v", "\\v") + ; + } + static string CompressedName(string name) + { + string compressedName = ""; + foreach (var part in name.Replace(',', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries)) + { + compressedName += char.IsLower(part[0]) + ? char.ToUpper(part[0]) + (part.Length > 1 ? part.Substring(1) : "") + : part; + } + return compressedName; + } + static bool IsEmpty(string str) + { + for (int i = 0; i < str.Length; i++) + { + if (str[i] != ' ') return false; + } + return true; + } + } +} diff --git a/src/SpecFileGen/SpecFileGen.csproj b/src/SpecFileGen/SpecFileGen.csproj index 715adf50..798dcad3 100644 --- a/src/SpecFileGen/SpecFileGen.csproj +++ b/src/SpecFileGen/SpecFileGen.csproj @@ -1,9 +1,9 @@ - - - - Exe - netcoreapp2.1 - false - - - + + + + Exe + netcoreapp2.1 + false + + + diff --git a/src/markdig.sln b/src/markdig.sln index e6e6a54d..31418231 100644 --- a/src/markdig.sln +++ b/src/markdig.sln @@ -1,81 +1,81 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28407.52 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{061866E2-005C-4D13-A338-EA464BBEC60F}" - ProjectSection(SolutionItems) = preProject - ..\changelog.md = ..\changelog.md - ..\.github\workflows\ci.yml = ..\.github\workflows\ci.yml - ..\license.txt = ..\license.txt - ..\readme.md = ..\readme.md - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig", "Markdig\Markdig.csproj", "{8A58A7E2-627C-4F41-933F-5AC92ADFAB48}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Signed", "Markdig.Signed\Markdig.Signed.csproj", "{C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Tests", "Markdig.Tests\Markdig.Tests.csproj", "{A0C5CB5F-5568-40AB-B945-D6D2664D51B0}" - ProjectSection(ProjectDependencies) = postProject - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} = {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Benchmarks", "Markdig.Benchmarks\Markdig.Benchmarks.csproj", "{6A19F040-BC7C-4283-873A-177B5324F1ED}" - ProjectSection(ProjectDependencies) = postProject - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} = {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.WebApp", "Markdig.WebApp\Markdig.WebApp.csproj", "{3CAD9801-9976-46BE-BACA-F6D0D21FDC00}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnicodeNormDApp", "UnicodeNormDApp\UnicodeNormDApp.csproj", "{33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mdtoc", "mdtoc\mdtoc.csproj", "{E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpecFileGen", "SpecFileGen\SpecFileGen.csproj", "{DB6E2ED5-7884-4E97-84AF-35E2480CF685}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Release|Any CPU.Build.0 = Release|Any CPU - {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Release|Any CPU.Build.0 = Release|Any CPU - {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Release|Any CPU.Build.0 = Release|Any CPU - {6A19F040-BC7C-4283-873A-177B5324F1ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6A19F040-BC7C-4283-873A-177B5324F1ED}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6A19F040-BC7C-4283-873A-177B5324F1ED}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6A19F040-BC7C-4283-873A-177B5324F1ED}.Release|Any CPU.Build.0 = Release|Any CPU - {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Release|Any CPU.Build.0 = Release|Any CPU - {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Debug|Any CPU.Build.0 = Debug|Any CPU - {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Release|Any CPU.ActiveCfg = Release|Any CPU - {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Release|Any CPU.Build.0 = Release|Any CPU - {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Release|Any CPU.Build.0 = Release|Any CPU - {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {D068F7B6-6ACC-456C-A2E1-10EA746D956D} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28407.52 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{061866E2-005C-4D13-A338-EA464BBEC60F}" + ProjectSection(SolutionItems) = preProject + ..\changelog.md = ..\changelog.md + ..\.github\workflows\ci.yml = ..\.github\workflows\ci.yml + ..\license.txt = ..\license.txt + ..\readme.md = ..\readme.md + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig", "Markdig\Markdig.csproj", "{8A58A7E2-627C-4F41-933F-5AC92ADFAB48}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Signed", "Markdig.Signed\Markdig.Signed.csproj", "{C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Tests", "Markdig.Tests\Markdig.Tests.csproj", "{A0C5CB5F-5568-40AB-B945-D6D2664D51B0}" + ProjectSection(ProjectDependencies) = postProject + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} = {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.Benchmarks", "Markdig.Benchmarks\Markdig.Benchmarks.csproj", "{6A19F040-BC7C-4283-873A-177B5324F1ED}" + ProjectSection(ProjectDependencies) = postProject + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} = {8A58A7E2-627C-4F41-933F-5AC92ADFAB48} + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Markdig.WebApp", "Markdig.WebApp\Markdig.WebApp.csproj", "{3CAD9801-9976-46BE-BACA-F6D0D21FDC00}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnicodeNormDApp", "UnicodeNormDApp\UnicodeNormDApp.csproj", "{33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mdtoc", "mdtoc\mdtoc.csproj", "{E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpecFileGen", "SpecFileGen\SpecFileGen.csproj", "{DB6E2ED5-7884-4E97-84AF-35E2480CF685}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8A58A7E2-627C-4F41-933F-5AC92ADFAB48}.Release|Any CPU.Build.0 = Release|Any CPU + {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C37C7B94-1219-4ED9-ABAC-C0B4B8FE8750}.Release|Any CPU.Build.0 = Release|Any CPU + {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0C5CB5F-5568-40AB-B945-D6D2664D51B0}.Release|Any CPU.Build.0 = Release|Any CPU + {6A19F040-BC7C-4283-873A-177B5324F1ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A19F040-BC7C-4283-873A-177B5324F1ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A19F040-BC7C-4283-873A-177B5324F1ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A19F040-BC7C-4283-873A-177B5324F1ED}.Release|Any CPU.Build.0 = Release|Any CPU + {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3CAD9801-9976-46BE-BACA-F6D0D21FDC00}.Release|Any CPU.Build.0 = Release|Any CPU + {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33FFC0B9-0187-44F9-9424-BB5AF5B4FB84}.Release|Any CPU.Build.0 = Release|Any CPU + {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}.Release|Any CPU.Build.0 = Release|Any CPU + {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB6E2ED5-7884-4E97-84AF-35E2480CF685}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D068F7B6-6ACC-456C-A2E1-10EA746D956D} + EndGlobalSection +EndGlobal