Skip to content

Commit

Permalink
Merge pull request #1065 from phpDocumentor/fix/955-table-content
Browse files Browse the repository at this point in the history
[BUGFIX] do not trim per line
  • Loading branch information
jaapio authored Sep 6, 2024
2 parents 28819a4 + 5e6d683 commit 5b95517
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\Productions\RuleContainer;
use Psr\Log\LoggerInterface;

use function array_map;
use function array_reverse;
use function count;
use function mb_strlen;
Expand All @@ -45,6 +46,7 @@ protected function compile(ParserContext $context): TableNode
{
$rows = $this->extractTableRows($context);
$rows = $this->concatenateTableRows($rows, $context);
$rows = $this->trimTableCellContents($rows);
$headers = $this->extractHeaderRows($rows, $context);

return new TableNode($rows, $headers);
Expand Down Expand Up @@ -407,4 +409,27 @@ private function hasRowSpan(string $line): bool
{
return preg_match('/\+[-]+\+/', $line) === 1;
}

/**
* @param array<int, TableRow> $rows
*
* @return array<int, TableRow>
*/
private function trimTableCellContents(array $rows): array
{
return array_map(
static fn (TableRow $row) => new TableRow(
array_map(
static fn (TableColumn $column) => new TableColumn(
trim($column->getContent()),
$column->getColSpan(),
[],
$column->getRowSpan(),
),
$row->getColumns(),
),
),
$rows,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function gridTableWithRowSpanProvider(): Generator
+===================================+===============+
| description | string |
+-----------------------------------+ |
| author | |
| author | test |
+-----------------------------------+---------------+
| keywords | string |
+-----------------------------------+---------------+
Expand All @@ -217,7 +217,7 @@ public static function gridTableWithRowSpanProvider(): Generator

$row1 = new TableRow();
$row1->addColumn(self::createColumnNode('description'));
$rowSpan = self::createColumnNode('string');
$rowSpan = self::createColumnNode("string\n\ntest");
$rowSpan->incrementRowSpan();
$row1->addColumn($rowSpan);

Expand Down
2 changes: 1 addition & 1 deletion packages/guides/src/Nodes/Table/TableColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getRowSpan(): int

public function addContent(string $content): void
{
$this->content = trim($this->content . $content);
$this->content .= $content;
}

public function incrementRowSpan(): void
Expand Down
6 changes: 4 additions & 2 deletions packages/guides/src/Nodes/Table/TableRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

final class TableRow
{
/** @var TableColumn[] */
private array $columns = [];
/** @param TableColumn[] $columns */
public function __construct(private array $columns = [])
{
}

public function addColumn(TableColumn $tableColumn): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ <h1>table</h1>
<tbody>
<tr>
<td><strong>Paragraphs</strong></td>
<td><p>Paragraph 1</p>
<p>Paragraph 2</p></td>
<td>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</td>
</tr>
<tr>
<td><strong>Lists</strong></td>
<td><p>See the list</p>
<td>
<p>See the list</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<p>A paragraph</p></td>
<p>A paragraph</p>
</td>
</tr>
</tbody>
</table>
</div>
<!-- content end -->
<!-- content end -->
Empty file.

0 comments on commit 5b95517

Please sign in to comment.