use StringSlice in IFencedBlock

This commit is contained in:
Ruud Poutsma
2020-11-06 17:33:27 +01:00
parent 8e8b95c3bb
commit 453e8239d2
10 changed files with 180 additions and 136 deletions

View File

@@ -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?

View File

@@ -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);

View File

@@ -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
{

View File

@@ -23,24 +23,40 @@ namespace Markdig.Extensions.CustomContainers
{
}
/// <inheritdoc />
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; }
/// <inheritdoc />
public int OpeningFencedCharCount { get; set; }
public int ClosingFencedCharCount { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterFencedChar { get; set; }
/// <inheritdoc />
public string Info { get; set; }
/// <inheritdoc />
public StringSlice UnescapedInfo { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterInfo { get; set; }
/// <inheritdoc />
public string Arguments { get; set; }
/// <inheritdoc />
public StringSlice UnescapedArguments { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterArguments { get; set; }
/// <inheritdoc />
public Newline InfoNewline { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceBeforeClosingFence { get; set; }
/// <inheritdoc />
public int ClosingFencedCharCount { get; set; }
}
}

View File

@@ -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);

View File

@@ -77,24 +77,19 @@ namespace Markdig.Parsers
/// <returns><c>true</c> if parsing of the line is successfull; <c>false</c> otherwise</returns>
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;

View File

@@ -19,7 +19,7 @@ namespace Markdig.Parsers.Inlines
/// </summary>
public LineBreakInlineParser()
{
OpeningCharacters = new[] { '\n', '\r' }; // TODO: RTP: this causes LineBreakInline to be parsed for non-trivia parser
OpeningCharacters = new[] { '\n', '\r' };
}
/// <summary>

View File

@@ -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);
}

View File

@@ -28,49 +28,46 @@ namespace Markdig.Syntax
IsBreakable = false;
}
/// <inheritdoc />
public string Info { get; set; }
public string UnescapedInfo { get; set; }
/// <inheritdoc />
public string WhitespaceAfterInfo { get; set; }
/// <summary>
/// Gets or sets the arguments after the <see cref="Info"/>.
/// May be null.
/// </summary>
public string Arguments { get; set; }
public string UnescapedArguments { get; set; }
/// <inheritdoc />
public string WhitespaceAfterArguments { get; set; }
/// <summary>
/// Gets or sets the fenced character count used to open this fenced code block.
/// </summary>
public int OpeningFencedCharCount { get; set; }
/// <summary>
/// Gets or sets the fenced character count used to open this fenced code block.
/// </summary>
public int ClosingFencedCharCount { get; set; }
public StringSlice WhitespaceBeforeClosingFence { get; set; }
/// <summary>
/// Gets or sets the fenced character used to open and close this fenced code block.
/// </summary>
public char FencedChar { get; set; }
/// <inheritdoc />
public string WhitespaceAfterFencedChar { get; set; }
public Newline InfoNewline { get; set; }
/// <summary>
/// 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.
/// </summary>
public int IndentCount { get; set; }
/// <inheritdoc />
public char FencedChar { get; set; }
/// <inheritdoc />
public int OpeningFencedCharCount { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterFencedChar { get; set; }
/// <inheritdoc />
public string Info { get; set; }
/// <inheritdoc />
public StringSlice UnescapedInfo { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterInfo { get; set; }
/// <inheritdoc />
public string Arguments { get; set; }
/// <inheritdoc />
public StringSlice UnescapedArguments { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceAfterArguments { get; set; }
/// <inheritdoc />
public Newline InfoNewline { get; set; }
/// <inheritdoc />
public StringSlice WhitespaceBeforeClosingFence { get; set; }
/// <inheritdoc />
public int ClosingFencedCharCount { get; set; }
}
}

View File

@@ -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
{
/// <summary>
/// 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.
/// </summary>
string Info { get; set; }
public string UnescapedInfo { get; set; }
/// <summary>
/// Gets or sets the language parsed after the first line of
/// the fenced code block and includes any whitespace. May be null.
/// </summary>
string WhitespaceAfterInfo { get; set; }
/// <summary>
/// Gets or sets the arguments after the <see cref="Info"/>.
/// May be null.
/// </summary>
string Arguments { get; set; }
public string UnescapedArguments { get; set; }
/// <summary>
/// Gets or sets the arguments after the <see cref="Arguments"/>.
/// Includes any whitespace. May be null.
/// </summary>
string WhitespaceAfterArguments { get; set; }
/// <summary>
/// Gets or sets the arguments after the <see cref="FencedChar"/>.
/// Includes any whitespace. May be null.
/// </summary>
string WhitespaceAfterFencedChar { get; set; }
char FencedChar { get; set; }
/// <summary>
/// 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; }
/// <summary>
/// Gets or sets the fenced character count used to open this fenced code block.
/// Gets or sets the whitespace after the <see cref="FencedChar"/>.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
public int ClosingFencedCharCount { get; set; }
public StringSlice WhitespaceBeforeClosingFence { get; set; }
StringSlice WhitespaceAfterFencedChar { get; set; }
/// <summary>
/// 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.
/// </summary>
char FencedChar { get; set; }
string Info { get; set; }
Newline Newline { get; set; }
/// <summary>
/// Non-escaped <see cref="Info"/> exactly as in source markdown.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
StringSlice UnescapedInfo { get; set; }
/// <summary>
/// Gets or sets the whitespace after the <see cref="Info"/>.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
StringSlice WhitespaceAfterInfo { get; set; }
/// <summary>
/// Gets or sets the arguments after the <see cref="Info"/>.
/// May be null.
/// </summary>
string Arguments { get; set; }
/// <summary>
/// Non-escaped <see cref="Arguments"/> exactly as in source markdown.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
StringSlice UnescapedArguments { get; set; }
/// <summary>
/// Gets or sets the whitespace after the <see cref="Arguments"/>.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
StringSlice WhitespaceAfterArguments { get; set; }
/// <summary>
/// Newline of the line with the opening fenced chars.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
Newline InfoNewline { get; set; }
/// <summary>
/// Whitespace before the closing fenced chars
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
StringSlice WhitespaceBeforeClosingFence { get; set; }
/// <summary>
/// Gets or sets the fenced character count used to close this fenced code block.
/// </summary>
int ClosingFencedCharCount { get; set; }
/// <summary>
/// Newline after the last line, which is always the line containing the closing fence chars.
/// "Inherited" from <see cref="Block.Newline"/>.
/// Trivia: only parsed when <see cref="MarkdownParser.TrackTrivia"/> is enabled, otherwise
/// <see cref="StringSlice.IsEmpty"/>.
/// </summary>
Newline Newline { get; set; }
}
}