Skip to content

Commit

Permalink
Merge pull request #169 from doctrine/0.4.x-merge-up-into-0.5.x_GMXPuyGS
Browse files Browse the repository at this point in the history
Merge release 0.4.4 into 0.5.x
  • Loading branch information
greg0ire authored Oct 21, 2021
2 parents 1dda6fd + 73992ea commit 79c8532
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 32 deletions.
20 changes: 14 additions & 6 deletions lib/Nodes/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\RST\Renderers\RenderedNode;

use function implode;
use function ltrim;
use function strlen;
use function substr;
use function trim;
Expand Down Expand Up @@ -137,18 +138,25 @@ public function getValueString(): string
protected function normalizeLines(array $lines): string
{
if ($lines !== []) {
$firstLine = $lines[0];
$indentLevel = null;

$k = 0;
// find the indentation by locating the line with the fewest preceding whitespace
foreach ($lines as $line) {
// skip empty lines
if (trim($line) === '') {
continue;
}

for ($k = 0; $k < strlen($firstLine); $k++) {
if (trim($firstLine[$k]) !== '') {
break;
$startingWhitespace = strlen($line) - strlen(ltrim($line));
if ($indentLevel !== null && $startingWhitespace > $indentLevel) {
continue;
}

$indentLevel = $startingWhitespace;
}

foreach ($lines as &$line) {
$line = substr($line, $k);
$line = substr($line, $indentLevel);
}
}

Expand Down
51 changes: 28 additions & 23 deletions lib/Parser/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,32 +284,12 @@ private function parseLine(string $line): bool
$this->buffer = new Buffer();
$this->flush();
$this->initDirective($line);
} elseif ($this->lineChecker->isIndented($this->lines->getNextLine())) {
$this->setState(State::DEFINITION_LIST);
$this->buffer->push($line);

return true;
} else {
$separatorLineConfig = $this->tableParser->parseTableSeparatorLine($line);

if ($separatorLineConfig === null) {
if ($this->getCurrentDirective() !== null && ! $this->getCurrentDirective()->appliesToNonBlockContent()) {
// If there is a directive set, it means we are the line *after* that directive
// But the state is being set to NORMAL, which means we are a non-indented line.
// Some special directives (like class) allow their content to be non-indented.
// But most do not, which means that our directive is now finished.
// We flush so that the directive can be processed. It will be passed a
// null node (We know because we are currently in a NEW state. If there
// had been legitimately-indented content, that would have matched some
// other state (e.g. BLOCK or CODE) and flushed when it finished.
$this->flush();
}

$this->setState(State::NORMAL);

return false;
}
}

$separatorLineConfig = $this->tableParser->parseTableSeparatorLine($line);
if ($separatorLineConfig !== null) {
$this->setState(State::TABLE);

$tableNode = $this->nodeFactory->createTableNode(
Expand All @@ -319,7 +299,32 @@ private function parseLine(string $line): bool
);

$this->nodeBuffer = $tableNode;

return true;
}

if ($this->lineChecker->isIndented($this->lines->getNextLine())) {
$this->setState(State::DEFINITION_LIST);
$this->buffer->push($line);

return true;
}

if ($this->getCurrentDirective() !== null && ! $this->getCurrentDirective()->appliesToNonBlockContent()) {
// If there is a directive set, it means we are the line *after* that directive
// But the state is being set to NORMAL, which means we are a non-indented line.
// Some special directives (like class) allow their content to be non-indented.
// But most do not, which means that our directive is now finished.
// We flush so that the directive can be processed. It will be passed a
// null node (We know because we are currently in a NEW state. If there
// had been legitimately-indented content, that would have matched some
// other state (e.g. BLOCK or CODE) and flushed when it finished.
$this->flush();
}

$this->setState(State::NORMAL);

return false;
}

break;
Expand Down
10 changes: 7 additions & 3 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class FunctionalTest extends TestCase
{
private const RENDER_DOCUMENT_FILES = ['main-directive'];
private const SKIP_INDENTER_FILES = ['code-block-diff'];

protected function setUp(): void
{
Expand All @@ -47,7 +48,8 @@ public function testFunctional(
string $renderMethod,
string $format,
string $rst,
string $expected
string $expected,
bool $useIndenter = true
): void {
$expectedLines = explode("\n", $expected);
$firstLine = $expectedLines[0];
Expand All @@ -68,7 +70,7 @@ public function testFunctional(

$rendered = $document->$renderMethod();

if ($format === Format::HTML) {
if ($format === Format::HTML && $useIndenter) {
$indenter = new Indenter();
$rendered = $indenter->indent($rendered);
}
Expand Down Expand Up @@ -133,7 +135,9 @@ public function getFunctionalTests(): array
? 'renderDocument'
: 'render';

$tests[$basename . '_' . $format] = [$basename, $parser, $renderMethod, $format, $rst, trim($expected)];
$useIndenter = ! in_array($basename, self::SKIP_INDENTER_FILES, true);

$tests[$basename . '_' . $format] = [$basename, $parser, $renderMethod, $format, $rst, trim($expected), $useIndenter];
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Functional/tests/code-block-diff/code-block-diff.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<pre><code class="diff">+ Added line
- Removed line
Normal line
- Removed line
+ Added line

</code></pre>
<pre><code class="diff"> Normal line
+ Added line
- Removed line
Normal line
- Removed line
+ Added line
</code></pre>
18 changes: 18 additions & 0 deletions tests/Functional/tests/code-block-diff/code-block-diff.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

.. code-block:: diff
+ Added line
- Removed line
Normal line
- Removed line
+ Added line
.. code-block:: diff
Normal line
+ Added line
- Removed line
Normal line
- Removed line
+ Added line
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<table>
<thead>
<tr>
<th>Col A</th>
<th>Col B</th>
<th>Col C</th>
</tr>
</thead>
<tbody>
<tr>
<td>Col X</td>
<td>Col Y</td>
<td>Col Z</td>
</tr>
<tr>
<td>Col U</td>
<td>Col J</td>
<td>Col K</td>
</tr>
</tbody>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
====== ===== =====
Col A Col B Col C
====== ===== =====
Col X Col Y Col Z
------ ----- -----
Col U Col J Col K
====== ===== =====

0 comments on commit 79c8532

Please sign in to comment.