diff --git a/src/Parser/BlockParser.php b/src/Parser/BlockParser.php index fbf47c2..dd3e1a0 100644 --- a/src/Parser/BlockParser.php +++ b/src/Parser/BlockParser.php @@ -1055,18 +1055,6 @@ protected function tryParseBlockAttributes(array $lines, int $start): ?int return null; } - // Check if attributes precede a reference definition - if so, skip storing them - // (they were already applied during extractReferences) - $count = count($lines); - $nextIdx = $start + 1; - if ($nextIdx < $count && preg_match('/^\[([^\]]*)\]:(?=[ \t]|$)/', $lines[$nextIdx])) { - if ($this->collectingReferences) { - $this->parseAttributeString($attrStr); - } - - return 1; - } - $this->parseAttributeString($attrStr); $this->pendingAttributeSourceLines[] = $line; @@ -1474,7 +1462,9 @@ protected function tryParseRawBlock(Node $parent, array $lines, int $start): ?in $this->addWarning('Unclosed raw block', $start, 1, true); } - $rawBlock = new RawBlock(trim($content, "\n"), $format); + // Only blank lines: keep them, so the block stays distinct from one with no lines. + $trimmed = trim($content, "\n"); + $rawBlock = new RawBlock($trimmed !== '' ? $trimmed : $content, $format); $this->applyPendingAttributes($rawBlock); $parent->appendChild($rawBlock); @@ -3118,6 +3108,8 @@ protected function tryParseList(Node $parent, array $lines, int $start, ?array $ if ( $parseItemLinesAsBlocks || $this->isBlockElementStart($firstLine) || preg_match('/^\[[^\]]+\]:(?:[ \t]+\S*)?[ \t]*$/', $firstLine) + // A marker-line `{...}` may be a block attribute for the lines below. + || preg_match('/^\{.*\}[ \t]*$/', $firstLine) ) { // Content starts with a block element (blockquote, code fence, // etc.) or we pushed a {...} back into itemLines that must be @@ -4131,7 +4123,7 @@ protected function tryParseTable(Node $parent, array $lines, int $start): ?int if ($trimmedContent !== '' && $this->isPlainText($trimmedContent)) { $cell->appendChild(new Text($trimmedContent)); } else { - $this->inlineParser->parse($cell, $trimmedContent, $baseLineForRow); + $this->inlineParser->parse($cell, $trimmedContent, $baseLineForRow, endsLine: false); } if (!$indexedSpans) { $row->appendChild($cell); diff --git a/src/Parser/InlineParser.php b/src/Parser/InlineParser.php index 14efcca..4c99283 100644 --- a/src/Parser/InlineParser.php +++ b/src/Parser/InlineParser.php @@ -65,6 +65,11 @@ class InlineParser */ protected int $inlineDepth = 0; + /** + * Whether the end of the text passed to parse() is the end of a line. + */ + protected bool $textEndsLine = true; + /** * @var array */ @@ -357,6 +362,7 @@ public function getQuoteCharacters(): array * @param int $sourceLine Source line number (0-indexed) for error reporting * @param list|null $sourceLineMap Original source line for each line in $text * @param int|null $sourceColumn Authored column where the first line starts + * @param bool $endsLine False when the text ends before a delimiter on the same line (table cell) */ public function parse( Node $parent, @@ -364,7 +370,9 @@ public function parse( int $sourceLine = 0, ?array $sourceLineMap = null, ?int $sourceColumn = null, + bool $endsLine = true, ): void { + $this->textEndsLine = $endsLine; $this->abbreviationSegments = new WeakMap(); $this->apostropheNodes = new WeakMap(); $this->abbreviationRun = 0; @@ -579,7 +587,10 @@ protected function parseInlinesImpl(Node $parent, string $text): void while ($lookAhead < $length && ($text[$lookAhead] === ' ' || $text[$lookAhead] === "\t")) { $lookAhead++; } - if ($lookAhead < $length && $text[$lookAhead] === "\n") { + $atLineEnd = $lookAhead < $length + ? $text[$lookAhead] === "\n" + : $this->textEndsLine && $this->inlineDepth === 1; + if ($atLineEnd) { // This is a hard break - strip trailing whitespace from text buffer $textBuffer = rtrim($textBuffer, " \t"); $this->flushText($parent, $textBuffer); @@ -598,8 +609,8 @@ protected function parseInlinesImpl(Node $parent, string $text): void continue; } - // Escaped tab becomes literal tab - $textBuffer .= $escaped; + // A tab is not escapable: the backslash stays literal. + $textBuffer .= '\\' . $escaped; $pos += 2; continue; diff --git a/src/Renderer/AnsiRenderer.php b/src/Renderer/AnsiRenderer.php index 99ca1ab..92c4bc1 100644 --- a/src/Renderer/AnsiRenderer.php +++ b/src/Renderer/AnsiRenderer.php @@ -897,7 +897,7 @@ protected function renderRawBlock(RawBlock $node): string { // Show raw blocks dimmed $format = $this->documentText($node->getFormat()); - $content = $this->documentText($node->getContent()); + $content = $this->documentText(trim($node->getContent(), "\n")); return $this->style('[raw:' . $format . '] ' . $content, self::DIM) . "\n\n"; } diff --git a/src/Renderer/HeadingIdTracker.php b/src/Renderer/HeadingIdTracker.php index cddd4df..875adc5 100644 --- a/src/Renderer/HeadingIdTracker.php +++ b/src/Renderer/HeadingIdTracker.php @@ -174,6 +174,9 @@ public function normalizeId(string $text): string */ protected function slug(string $text): string { + // An escaped space (placeholder U+E000) separates words like a space. + $text = str_replace("\u{E000}", ' ', $text); + return trim(preg_replace('/[^0-9A-Za-z\x{0080}-\x{10FFFF}]+/u', '-', $text) ?? $text, '-'); } diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index e4e61e9..36c5f35 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -1410,6 +1410,8 @@ protected function renderRawBlock(RawBlock $node): string } $content = $node->getContent(); + // A blank-only block keeps its own newlines; an empty one prints nothing. + $newline = $content === '' || str_ends_with($content, "\n") ? '' : "\n"; // Handle raw HTML according to safe mode if ($this->safeMode !== null) { @@ -1418,11 +1420,11 @@ protected function renderRawBlock(RawBlock $node): string return ''; } if ($mode === SafeMode::RAW_HTML_ESCAPE) { - return $this->escape($content) . "\n"; + return $this->escape($content) . $newline; } } - return $content . "\n"; + return $content . $newline; } protected function renderRawInline(RawInline $node): string diff --git a/src/Renderer/MarkdownRenderer.php b/src/Renderer/MarkdownRenderer.php index bf443bd..d496c71 100644 --- a/src/Renderer/MarkdownRenderer.php +++ b/src/Renderer/MarkdownRenderer.php @@ -661,7 +661,9 @@ protected function renderMath(Math $node): string protected function renderRawBlock(RawBlock $node): string { if ($node->getFormat() === 'html') { - return $node->getContent() . "\n\n"; + $content = $node->getContent(); + + return trim($content, "\n") === '' ? '' : $content . "\n\n"; } return ''; diff --git a/tests/TestCase/SmallConformanceLeftoversTest.php b/tests/TestCase/SmallConformanceLeftoversTest.php new file mode 100644 index 0000000..7bae910 --- /dev/null +++ b/tests/TestCase/SmallConformanceLeftoversTest.php @@ -0,0 +1,115 @@ +assertSame($expected, (new DjotConverter())->convert($djot)); + $this->assertSame($expected, (new DjotConverter(warnings: true))->convert($djot)); + } + + /** + * @return array + */ + public static function casesProvider(): array + { + return [ + 'attributes before a reference-like line' => [ + "{.c}\n[r]: p q\n", + "

[r]: p q

\n", + ], + 'boolean attribute before a reference-like line' => [ + "{y}\n[r]: p q\n", + "

[r]: p q

\n", + ], + 'attributes before a real reference definition' => [ + "{.c}\n[r]: p\n\n[a][r]\n", + "

a

\n", + ], + 'marker-line attributes over an indented line' => [ + "- {#t .p}\n x\n\n- b\n", + "
    \n
  • \n

    x

    \n
  • \n
  • \n

    b

    \n
  • \n
\n", + ], + 'marker-line attributes over two content-indented lines' => [ + "1. {.c}\n x\n y\n", + "
    \n
  1. \n

    x\ny

    \n
  2. \n
\n", + ], + 'marker-line braced inline stays inline' => [ + "- {_a_}\n x\n", + "
    \n
  • \na\nx\n
  • \n
\n", + ], + 'empty unclosed raw block' => [ + "```=html\n", + '', + ], + 'empty closed raw block in a quote' => [ + "> ```=html\n> ```\n", + "
\n
\n", + ], + 'raw block with one blank line' => [ + "```=html\n\n```\n", + "\n", + ], + 'raw block with two blank lines' => [ + "```=html\n\n\n", + "\n\n", + ], + 'backslash before a trailing space ends the paragraph with a break' => [ + "x \\ \n", + "

x
\n

\n", + ], + 'backslash before a trailing tab without a final newline' => [ + "x \\\t", + "

x
\n

\n", + ], + 'backslash before a trailing space in a heading' => [ + "# h \\ \n\npara\n", + "
\n

h
\n

\n

para

\n
\n", + ], + 'backslash before a tab inside a line stays literal' => [ + "x\\\ty\n", + "

x\\\ty

\n", + ], + 'escaped space separates words in a heading id' => [ + "# x \\ y\n", + "
\n

x  y

\n
\n", + ], + 'backslash before a space at the end of link text' => [ + "[a \\ ](u)\n", + "

a  

\n", + ], + 'backslash before a space at the end of a table cell' => [ + "| a \\ |\n", + "\n\n\n\n
a  
\n", + ], + ]; + } + + public function testRawBlockWithoutLinesIsEmpty(): void + { + $doc = (new BlockParser())->parse("``` =html\n"); + $block = $doc->getChildren()[0]; + $this->assertInstanceOf(RawBlock::class, $block); + $this->assertSame('', $block->getContent()); + + $doc = (new BlockParser())->parse("``` =html\n\n"); + $block = $doc->getChildren()[0]; + $this->assertInstanceOf(RawBlock::class, $block); + $this->assertSame("\n", $block->getContent()); + } +}