diff --git a/src/Markdig.Tests/Roundtrip.md b/src/Markdig.Tests/Roundtrip.md index 254f0c9d..efb1bd4f 100644 --- a/src/Markdig.Tests/Roundtrip.md +++ b/src/Markdig.Tests/Roundtrip.md @@ -33,17 +33,18 @@ In order: - ~~support link parsing~~ - ~~support AutolinkInline~~ - ~~generate spec examples as tests for roundtrip~~ -- fix `TODO: RTP: ` - ~~check char.IsWhitespace() calls~~ - ~~check char.IsNewline() calls~~ - ~~introduce feature flag~~ - ~~extract MarkdownRenderer~~ - ~~cleanup NormalizeRenderer (MarkdownRenderer)~~ - ~~deduplicate MarkdownRenderer and NormalizeRenderer code~~ +- ~~merge from main~~ +- fix `TODO: RTP: ` - use StringSlice where possible instead of String - do pull request feedback - split HeadingBlock into AtxHeadingBlock and SetextHeadingBlock? -- fix broken pre-existing tests +- ~~fix broken pre-existing tests~~ - document newly added syntax properties - support extensions - review complete PR and follow conventions @@ -51,7 +52,6 @@ In order: - create todo list with perf optimization focus points - optimize perf - `\0` -- merge from main - document how trivia are handled generically and specifically - write tree comparison tests? - write tree visualization tool? diff --git a/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs b/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs index a384be97..f675956a 100644 --- a/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs +++ b/src/Markdig.Tests/RoundtripSpecs/TestFencedCodeBlock.cs @@ -86,20 +86,20 @@ namespace Markdig.Tests.RoundtripSpecs RoundTrip(value); } - [TestCase("```\ni a\n```")] - [TestCase("```\ni a a2\n```")] - [TestCase("```\ni a a2 a3\n```")] - [TestCase("```\ni a a2 a3 a4\n```")] + [TestCase("```i a\n```")] + [TestCase("```i a a2\n```")] + [TestCase("```i a a2 a3\n```")] + [TestCase("```i a a2 a3 a4\n```")] - [TestCase("```\ni\ta\n```")] - [TestCase("```\ni\ta a2\n```")] - [TestCase("```\ni\ta a2 a3\n```")] - [TestCase("```\ni\ta a2 a3 a4\n```")] + [TestCase("```i\ta\n```")] + [TestCase("```i\ta a2\n```")] + [TestCase("```i\ta a2 a3\n```")] + [TestCase("```i\ta a2 a3 a4\n```")] - [TestCase("```\ni\ta \n```")] - [TestCase("```\ni\ta a2 \n```")] - [TestCase("```\ni\ta a2 a3 \n```")] - [TestCase("```\ni\ta a2 a3 a4 \n```")] + [TestCase("```i\ta \n```")] + [TestCase("```i\ta a2 \n```")] + [TestCase("```i\ta a2 a3 \n```")] + [TestCase("```i\ta a2 a3 a4 \n```")] public void TestInfoArguments(string value) { RoundTrip(value); diff --git a/src/Markdig.Tests/RoundtripSpecs/TestLinkReferenceDefinition.cs b/src/Markdig.Tests/RoundtripSpecs/TestLinkReferenceDefinition.cs index 1d7a1017..c569764b 100644 --- a/src/Markdig.Tests/RoundtripSpecs/TestLinkReferenceDefinition.cs +++ b/src/Markdig.Tests/RoundtripSpecs/TestLinkReferenceDefinition.cs @@ -3,7 +3,6 @@ using static Markdig.Tests.TestRoundtrip; namespace Markdig.Tests.RoundtripSpecs { - // TODO: RTP: ".[a] /r" dont seem to be parsed into LRD! [TestFixture] public class TestLinkReferenceDefinition { diff --git a/src/Markdig/Extensions/CustomContainers/CustomContainer.cs b/src/Markdig/Extensions/CustomContainers/CustomContainer.cs index ca456922..e252de31 100644 --- a/src/Markdig/Extensions/CustomContainers/CustomContainer.cs +++ b/src/Markdig/Extensions/CustomContainers/CustomContainer.cs @@ -23,24 +23,40 @@ namespace Markdig.Extensions.CustomContainers { } + /// public char FencedChar { get; set; } - public string WhitespaceAfterFencedChar { get; set; } - - public string Info { get; set; } - public string UnescapedInfo { get; set; } - - public string WhitespaceAfterInfo { get; set; } - - public string Arguments { get; set; } - public string UnescapedArguments { get; set; } - public string WhitespaceAfterArguments { get; set; } - + /// public int OpeningFencedCharCount { get; set; } - public int ClosingFencedCharCount { get; set; } + /// + public StringSlice WhitespaceAfterFencedChar { get; set; } + /// + public string Info { get; set; } + + /// + public StringSlice UnescapedInfo { get; set; } + + /// + public StringSlice WhitespaceAfterInfo { get; set; } + + /// + public string Arguments { get; set; } + + /// + public StringSlice UnescapedArguments { get; set; } + + /// + public StringSlice WhitespaceAfterArguments { get; set; } + + /// public Newline InfoNewline { get; set; } + + /// public StringSlice WhitespaceBeforeClosingFence { get; set; } + + /// + public int ClosingFencedCharCount { get; set; } } } \ No newline at end of file diff --git a/src/Markdig/Parsers/BlockProcessor.cs b/src/Markdig/Parsers/BlockProcessor.cs index b50269b8..b3ac3abf 100644 --- a/src/Markdig/Parsers/BlockProcessor.cs +++ b/src/Markdig/Parsers/BlockProcessor.cs @@ -834,7 +834,7 @@ namespace Markdig.Parsers } if (TrackTrivia) { - // TODO: RTP: delegate this to container parser classes + // TODO: RTP: delegate this to container parser classes? if (paragraph.Parent is QuoteBlock qb) { var afterWhitespace = UseWhitespace(Start - 1); diff --git a/src/Markdig/Parsers/FencedBlockParserBase.cs b/src/Markdig/Parsers/FencedBlockParserBase.cs index 55bb76ad..798febd8 100644 --- a/src/Markdig/Parsers/FencedBlockParserBase.cs +++ b/src/Markdig/Parsers/FencedBlockParserBase.cs @@ -77,24 +77,19 @@ namespace Markdig.Parsers /// true if parsing of the line is successfull; false otherwise public static bool RoundtripInfoParser(BlockProcessor blockProcessor, ref StringSlice line, IFencedBlock fenced, char openingCharacter) { - string afterFence = null; - string info = null; - string afterInfo = null; - string arg = null; - string afterArg = null; + var start = line.Start; + var end = start - 1; + StringSlice afterFence = new StringSlice(line.Text, start, end); + StringSlice info = new StringSlice(line.Text, start, end); + StringSlice afterInfo = new StringSlice(line.Text, start, end); + StringSlice arg = new StringSlice(line.Text, start, end); + StringSlice afterArg = new StringSlice(line.Text, start, end); ParseState state = ParseState.AfterFence; - // TODO: RTP: use StringSlices for all - // pattern: ``` info? args? - // after blockchar? - // after info? - // after args? - // info: between fencedchars and - - // An info string cannot contain any backticks (unless it is a tilde block) for (int i = line.Start; i <= line.End; i++) { char c = line.Text[i]; + // An info string cannot contain any backticks (unless it is a tilde block) if (c == '`' && openingCharacter == '`') { return false; @@ -104,57 +99,60 @@ namespace Markdig.Parsers case ParseState.AfterFence: if (c.IsSpaceOrTab()) { - afterFence += c; + afterFence.End += 1; } else { state = ParseState.Info; - info += c; + info.Start = i; + info.End = i; + afterFence.End = i - 1; } break; case ParseState.Info: if (c.IsSpaceOrTab()) { state = ParseState.AfterInfo; - afterInfo += c; + afterInfo.Start = i; + afterInfo.End = i; } else { - info += c; + info.End += 1; } break; case ParseState.AfterInfo: if (c.IsSpaceOrTab()) { - afterInfo += c; + afterInfo.End += 1; } else { - arg += c; + arg.Start = i; + arg.End = i; state = ParseState.Args; } break; case ParseState.Args: - var start = i - 1; - // walk from end, as rest (including spaces except trailing spaces) is args + // walk from end, as rest (except trailing spaces) is args for (int j = line.End; j > start; j--) { - char cc = line[j]; - if (cc.IsSpaceOrTab()) + c = line[j]; + if (c.IsSpaceOrTab()) { - afterArg = cc + afterArg; + afterArg.Start = i; } else { - var length = j - start + 1; - arg = line.Text.Substring(start, length); + arg.End = j; + afterArg.Start = j + 1; + afterArg.End = line.End; goto end; } } goto end; case ParseState.AfterArgs: { - //throw new Exception("should ot reach this code"); return false; } } @@ -162,10 +160,10 @@ namespace Markdig.Parsers end: fenced.WhitespaceAfterFencedChar = afterFence; - fenced.Info = HtmlHelper.Unescape(info); + fenced.Info = HtmlHelper.Unescape(info.ToString()); fenced.UnescapedInfo = info; fenced.WhitespaceAfterInfo = afterInfo; - fenced.Arguments = HtmlHelper.Unescape(arg); + fenced.Arguments = HtmlHelper.Unescape(arg.ToString()); fenced.UnescapedArguments = arg; fenced.WhitespaceAfterArguments = afterArg; fenced.InfoNewline = line.Newline; diff --git a/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs b/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs index 0244862e..d7062e5d 100644 --- a/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs @@ -19,7 +19,7 @@ namespace Markdig.Parsers.Inlines /// public LineBreakInlineParser() { - OpeningCharacters = new[] { '\n', '\r' }; // TODO: RTP: this causes LineBreakInline to be parsed for non-trivia parser + OpeningCharacters = new[] { '\n', '\r' }; } /// diff --git a/src/Markdig/Renderers/Roundtrip/CodeBlockRenderer.cs b/src/Markdig/Renderers/Roundtrip/CodeBlockRenderer.cs index 40cbfdc4..ad460a5e 100644 --- a/src/Markdig/Renderers/Roundtrip/CodeBlockRenderer.cs +++ b/src/Markdig/Renderers/Roundtrip/CodeBlockRenderer.cs @@ -24,7 +24,7 @@ namespace Markdig.Renderers.Roundtrip var opening = new string(fencedCodeBlock.FencedChar, fencedCodeBlock.OpeningFencedCharCount); renderer.Write(opening); - if (fencedCodeBlock.WhitespaceAfterFencedChar != null) + if (!fencedCodeBlock.WhitespaceAfterFencedChar.IsEmpty) { renderer.Write(fencedCodeBlock.WhitespaceAfterFencedChar); } @@ -32,7 +32,7 @@ namespace Markdig.Renderers.Roundtrip { renderer.Write(fencedCodeBlock.UnescapedInfo); } - if (fencedCodeBlock.WhitespaceAfterInfo != null) + if (!fencedCodeBlock.WhitespaceAfterInfo.IsEmpty) { renderer.Write(fencedCodeBlock.WhitespaceAfterInfo); } @@ -40,7 +40,7 @@ namespace Markdig.Renderers.Roundtrip { renderer.Write(fencedCodeBlock.UnescapedArguments); } - if (fencedCodeBlock.WhitespaceAfterArguments != null) + if (!fencedCodeBlock.WhitespaceAfterArguments.IsEmpty) { renderer.Write(fencedCodeBlock.WhitespaceAfterArguments); } diff --git a/src/Markdig/Syntax/FencedCodeBlock.cs b/src/Markdig/Syntax/FencedCodeBlock.cs index 65491d67..b75ab9e4 100644 --- a/src/Markdig/Syntax/FencedCodeBlock.cs +++ b/src/Markdig/Syntax/FencedCodeBlock.cs @@ -28,49 +28,46 @@ namespace Markdig.Syntax IsBreakable = false; } - /// - public string Info { get; set; } - public string UnescapedInfo { get; set; } - - /// - public string WhitespaceAfterInfo { get; set; } - - /// - /// Gets or sets the arguments after the . - /// May be null. - /// - public string Arguments { get; set; } - public string UnescapedArguments { get; set; } - - /// - public string WhitespaceAfterArguments { get; set; } - - /// - /// Gets or sets the fenced character count used to open this fenced code block. - /// - public int OpeningFencedCharCount { get; set; } - - /// - /// Gets or sets the fenced character count used to open this fenced code block. - /// - public int ClosingFencedCharCount { get; set; } - - public StringSlice WhitespaceBeforeClosingFence { get; set; } - - /// - /// Gets or sets the fenced character used to open and close this fenced code block. - /// - public char FencedChar { get; set; } - - /// - public string WhitespaceAfterFencedChar { get; set; } - - public Newline InfoNewline { get; set; } - /// /// Gets or sets the indent count when the fenced code block was indented /// and we need to remove up to indent count chars spaces from the begining of a line. /// public int IndentCount { get; set; } + + /// + public char FencedChar { get; set; } + + /// + public int OpeningFencedCharCount { get; set; } + + /// + public StringSlice WhitespaceAfterFencedChar { get; set; } + + /// + public string Info { get; set; } + + /// + public StringSlice UnescapedInfo { get; set; } + + /// + public StringSlice WhitespaceAfterInfo { get; set; } + + /// + public string Arguments { get; set; } + + /// + public StringSlice UnescapedArguments { get; set; } + + /// + public StringSlice WhitespaceAfterArguments { get; set; } + + /// + public Newline InfoNewline { get; set; } + + /// + public StringSlice WhitespaceBeforeClosingFence { get; set; } + + /// + public int ClosingFencedCharCount { get; set; } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/IFencedBlock.cs b/src/Markdig/Syntax/IFencedBlock.cs index 0245fd3b..7bdcc90c 100644 --- a/src/Markdig/Syntax/IFencedBlock.cs +++ b/src/Markdig/Syntax/IFencedBlock.cs @@ -3,6 +3,7 @@ // See the license.txt file in the project root for more information. using Markdig.Helpers; +using Markdig.Parsers; namespace Markdig.Syntax { @@ -12,36 +13,9 @@ namespace Markdig.Syntax public interface IFencedBlock : IBlock { /// - /// Gets or sets the language parsed after the first line of - /// the fenced code block. May be null. + /// Gets or sets the fenced character used to open and close this fenced code block. /// - string Info { get; set; } - public string UnescapedInfo { get; set; } - - /// - /// Gets or sets the language parsed after the first line of - /// the fenced code block and includes any whitespace. May be null. - /// - string WhitespaceAfterInfo { get; set; } - - /// - /// Gets or sets the arguments after the . - /// May be null. - /// - string Arguments { get; set; } - public string UnescapedArguments { get; set; } - - /// - /// Gets or sets the arguments after the . - /// Includes any whitespace. May be null. - /// - string WhitespaceAfterArguments { get; set; } - - /// - /// Gets or sets the arguments after the . - /// Includes any whitespace. May be null. - /// - string WhitespaceAfterFencedChar { get; set; } + char FencedChar { get; set; } /// /// Gets or sets the fenced character count used to open this fenced code block. @@ -49,17 +23,77 @@ namespace Markdig.Syntax public int OpeningFencedCharCount { get; set; } /// - /// Gets or sets the fenced character count used to open this fenced code block. + /// Gets or sets the whitespace after the . + /// Trivia: only parsed when is enabled, otherwise + /// . /// - public int ClosingFencedCharCount { get; set; } - public StringSlice WhitespaceBeforeClosingFence { get; set; } + StringSlice WhitespaceAfterFencedChar { get; set; } /// - /// Gets or sets the fenced character used to open and close this fenced code block. + /// Gets or sets the language parsed after the first line of + /// the fenced code block. May be null. /// - char FencedChar { get; set; } + string Info { get; set; } - Newline Newline { get; set; } + /// + /// Non-escaped exactly as in source markdown. + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + StringSlice UnescapedInfo { get; set; } + + /// + /// Gets or sets the whitespace after the . + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + StringSlice WhitespaceAfterInfo { get; set; } + + /// + /// Gets or sets the arguments after the . + /// May be null. + /// + string Arguments { get; set; } + + /// + /// Non-escaped exactly as in source markdown. + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + StringSlice UnescapedArguments { get; set; } + + /// + /// Gets or sets the whitespace after the . + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + StringSlice WhitespaceAfterArguments { get; set; } + + /// + /// Newline of the line with the opening fenced chars. + /// Trivia: only parsed when is enabled, otherwise + /// . + /// Newline InfoNewline { get; set; } + + /// + /// Whitespace before the closing fenced chars + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + StringSlice WhitespaceBeforeClosingFence { get; set; } + + /// + /// Gets or sets the fenced character count used to close this fenced code block. + /// + int ClosingFencedCharCount { get; set; } + + /// + /// Newline after the last line, which is always the line containing the closing fence chars. + /// "Inherited" from . + /// Trivia: only parsed when is enabled, otherwise + /// . + /// + Newline Newline { get; set; } } } \ No newline at end of file