fix empty QuoteBlock lines

This commit is contained in:
Ruud Poutsma
2020-10-23 17:50:07 +02:00
parent 352443d9cd
commit a04aa8a4de
7 changed files with 73 additions and 14 deletions

View File

@@ -63,3 +63,4 @@ In order:
- Newline struct itself
- handling newlines
- should newlines be supported?
- Example 207, 209: Special-casing certain edgecases

View File

@@ -266,5 +266,20 @@ namespace Markdig.Tests.RoundtripSpecs
{
RoundTrip(value);
}
[TestCase(">\n>q")]
[TestCase(">\n>\n>q")]
[TestCase(">q\n>\n>q")]
[TestCase(">q\n>\n>\n>q")]
[TestCase(">q\n> \n>q")]
[TestCase(">q\n> \n>q")]
[TestCase(">q\n> \n>q")]
[TestCase(">q\n>\t\n>q")]
[TestCase(">q\n>\v\n>q")]
[TestCase(">q\n>\f\n>q")]
public void TestEmptyLines(string value)
{
RoundTrip(value);
}
}
}

View File

@@ -555,6 +555,10 @@ namespace Markdig.Parsers
{
break;
}
if (BeforeLines != null && BeforeLines.Count > 0)
{
block.LinesAfter = UseLinesBefore();
}
Close(i);
}
UpdateLastBlockAndContainer();

View File

@@ -117,7 +117,7 @@ namespace Markdig.Parsers
if (!lineReader.IsEndOfFile())
{
var lastBlock = blockProcessor.LastBlock;
if (lastBlock != null)
if (lastBlock != null && blockProcessor.BeforeLines != null)
{
lastBlock.LinesAfter = blockProcessor.BeforeLines ?? new List<StringSlice>();
}

View File

@@ -36,13 +36,6 @@ namespace Markdig.Parsers
var quoteChar = processor.CurrentChar;
var column = processor.Column;
var c = processor.NextChar();
//if (c.IsSpaceOrTab())
//{
// processor.NextColumn();
// hasSpaceAfterQuoteChar = true;
// whitespaceToAdd += 1;
//}
//beforeWhitespace.End -= 1 + (hasSpaceAfterQuoteChar ? 1 : 0);
var quoteBlock = new QuoteBlock(this)
{
@@ -51,9 +44,16 @@ namespace Markdig.Parsers
Span = new SourceSpan(sourcePosition, processor.Line.End),
LinesBefore = processor.UseLinesBefore()
};
StringSlice afterWhitespace = StringSlice.Empty;
if (processor.Line.IsEmptyOrWhitespace())
{
processor.Line.TrimStart();
afterWhitespace = new StringSlice(processor.Line.Text, sourcePosition + 1, processor.Line.End);
}
quoteBlock.QuoteLines.Add(new QuoteBlock.QuoteLine
{
BeforeWhitespace = processor.PopBeforeWhitespace(sourcePosition - 1),
AfterWhitespace = afterWhitespace,
QuoteChar = true,
Newline = processor.Line.Newline,
});
@@ -70,6 +70,7 @@ namespace Markdig.Parsers
}
var quote = (QuoteBlock) block;
var sourcePosition = processor.Start;
// 5.1 Block quotes
// A block quote marker consists of 0-3 spaces of initial indent, plus (a) the character > together with a following space, or (b) a single character > not followed by a space.
@@ -85,20 +86,27 @@ namespace Markdig.Parsers
quote.QuoteLines.Add(new QuoteBlock.QuoteLine
{
QuoteChar = false,
BeforeWhitespace = processor.PopBeforeWhitespace(processor.Start - 1),
BeforeWhitespace = processor.PopBeforeWhitespace(sourcePosition - 1),
Newline = processor.Line.Newline,
});
return BlockState.None;
}
}
processor.NextChar(); // Skip quote marker char
StringSlice afterWhitespace = StringSlice.Empty;
if (processor.Line.IsEmptyOrWhitespace())
{
processor.Line.TrimStart();
afterWhitespace = new StringSlice(processor.Line.Text, sourcePosition + 1, processor.Line.End);
}
quote.QuoteLines.Add(new QuoteBlock.QuoteLine
{
QuoteChar = true,
BeforeWhitespace = processor.PopBeforeWhitespace(processor.Start - 1),
BeforeWhitespace = processor.PopBeforeWhitespace(sourcePosition - 1),
AfterWhitespace = afterWhitespace,
Newline = processor.Line.Newline,
});
processor.NextChar(); // Skip quote marker char
processor.WhitespaceStart = processor.Start;
block.UpdateSpanEnd(processor.Line.End);
return BlockState.Continue;

View File

@@ -19,7 +19,7 @@ namespace Markdig.Renderers.Normalize
renderer.RenderLinesBefore(paragraph);
renderer.Write(paragraph.BeforeWhitespace);
renderer.WriteLeafInline(paragraph);
//renderer.Write(paragraph.Newline);
//renderer.Write(paragraph.Newline); // paragraph typically has LineBreakInlines as closing inline nodes
renderer.RenderLinesAfter(paragraph);
}
}

View File

@@ -3,6 +3,7 @@
// See the license.txt file in the project root for more information.
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using System.Collections.Generic;
namespace Markdig.Renderers.Normalize
@@ -21,14 +22,44 @@ namespace Markdig.Renderers.Normalize
var indents = new List<string>();
foreach (var quoteLine in quoteBlock.QuoteLines)
{
indents.Add(quoteLine.BeforeWhitespace.ToString() + (quoteLine.QuoteChar ? ">" : "") + quoteLine.AfterWhitespace.ToString());
var wsb = quoteLine.BeforeWhitespace.ToString();
var quoteChar = quoteLine.QuoteChar ? ">" : "";
var wsa = quoteLine.AfterWhitespace.ToString();
indents.Add(wsb + quoteChar + wsa);
}
bool noChildren = false;
if (quoteBlock.Count == 0)
{
noChildren = true;
// since this QuoteBlock instance has no children, indents will not be rendered. We
// work around this by adding empty LineBreakInlines to a ParagraphBlock.
// Wanted: a more elegant/better solution (although this is not *that* bad).
foreach (var quoteLine in quoteBlock.QuoteLines)
{
// TODO: RTP: introduce EmptyQuoteLine class deriving from LeafBlock?
var emptyLeafBlock = new ParagraphBlock
{
Newline = quoteLine.Newline
};
var newline = new LineBreakInline
{
Newline = quoteLine.Newline
};
var container = new ContainerInline();
container.AppendChild(newline);
emptyLeafBlock.Inline = container;
quoteBlock.Add(emptyLeafBlock);
}
}
renderer.PushIndent(indents);
renderer.WriteChildren(quoteBlock);
renderer.PopIndent();
renderer.RenderLinesAfter(quoteBlock);
if (!noChildren)
{
renderer.RenderLinesAfter(quoteBlock);
}
}
}
}