Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/Parser/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions src/Parser/InlineParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{type: string, char: string, pos: int, node: \Djot\Node\Node}>
*/
Expand Down Expand Up @@ -357,14 +362,17 @@ public function getQuoteCharacters(): array
* @param int $sourceLine Source line number (0-indexed) for error reporting
* @param list<int>|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,
string $text,
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/AnsiRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
3 changes: 3 additions & 0 deletions src/Renderer/HeadingIdTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-');
}

Expand Down
6 changes: 4 additions & 2 deletions src/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Renderer/MarkdownRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down
115 changes: 115 additions & 0 deletions tests/TestCase/SmallConformanceLeftoversTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Djot\Test\TestCase;

use Djot\DjotConverter;
use Djot\Node\Block\RawBlock;
use Djot\Parser\BlockParser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Block attributes before reference-like lines, marker-line attributes,
* empty raw blocks and a backslash before trailing spaces (matches djot.js).
*/
class SmallConformanceLeftoversTest extends TestCase
{
#[DataProvider('casesProvider')]
public function testOutput(string $djot, string $expected): void
{
$this->assertSame($expected, (new DjotConverter())->convert($djot));
$this->assertSame($expected, (new DjotConverter(warnings: true))->convert($djot));
}

/**
* @return array<string, array{string, string}>
*/
public static function casesProvider(): array
{
return [
'attributes before a reference-like line' => [
"{.c}\n[r]: p q\n",
"<p class=\"c\">[r]: p q</p>\n",
],
'boolean attribute before a reference-like line' => [
"{y}\n[r]: p q\n",
"<p y=\"\">[r]: p q</p>\n",
],
'attributes before a real reference definition' => [
"{.c}\n[r]: p\n\n[a][r]\n",
"<p><a href=\"p\" class=\"c\">a</a></p>\n",
],
'marker-line attributes over an indented line' => [
"- {#t .p}\n x\n\n- b\n",
"<ul>\n<li>\n<p id=\"t\" class=\"p\">x</p>\n</li>\n<li>\n<p>b</p>\n</li>\n</ul>\n",
],
'marker-line attributes over two content-indented lines' => [
"1. {.c}\n x\n y\n",
"<ol>\n<li>\n<p class=\"c\">x\ny</p>\n</li>\n</ol>\n",
],
'marker-line braced inline stays inline' => [
"- {_a_}\n x\n",
"<ul>\n<li>\n<em>a</em>\nx\n</li>\n</ul>\n",
],
'empty unclosed raw block' => [
"```=html\n",
'',
],
'empty closed raw block in a quote' => [
"> ```=html\n> ```\n",
"<blockquote>\n</blockquote>\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",
"<p>x<br>\n</p>\n",
],
'backslash before a trailing tab without a final newline' => [
"x \\\t",
"<p>x<br>\n</p>\n",
],
'backslash before a trailing space in a heading' => [
"# h \\ \n\npara\n",
"<section id=\"h\">\n<h1>h<br>\n</h1>\n<p>para</p>\n</section>\n",
],
'backslash before a tab inside a line stays literal' => [
"x\\\ty\n",
"<p>x\\\ty</p>\n",
],
'escaped space separates words in a heading id' => [
"# x \\ y\n",
"<section id=\"x-y\">\n<h1>x &nbsp;y</h1>\n</section>\n",
],
'backslash before a space at the end of link text' => [
"[a \\ ](u)\n",
"<p><a href=\"u\">a &nbsp;</a></p>\n",
],
'backslash before a space at the end of a table cell' => [
"| a \\ |\n",
"<table>\n<tr>\n<td>a &nbsp;</td>\n</tr>\n</table>\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());
}
}
Loading