Skip to content

Commit c2e3457

Browse files
linawolfjaapio
authored andcommitted
[FEATURE] Make csv-table directive work with content
1 parent 1663716 commit c2e3457

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

packages/guides-restructured-text/src/RestructuredText/Directives/CsvTableDirective.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use function array_filter;
2020
use function count;
21+
use function implode;
2122
use function trim;
2223

2324
/**
@@ -46,24 +47,37 @@ public function processNode(
4647
BlockContext $blockContext,
4748
Directive $directive,
4849
): Node {
49-
$csvStream = $blockContext->getDocumentParserContext()
50-
->getContext()
51-
->getOrigin()
52-
->readStream((string) $directive->getOption('file')->getValue());
50+
if ($directive->hasOption('file')) {
51+
$csvStream = $blockContext->getDocumentParserContext()
52+
->getContext()
53+
->getOrigin()
54+
->readStream((string) $directive->getOption('file')->getValue());
5355

54-
if ($csvStream === false) {
55-
$this->logger->error('Unable to read CSV file {file}', ['file' => $directive->getOption('file')->getValue()]);
56+
if ($csvStream === false) {
57+
$this->logger->error('Unable to read CSV file {file}', ['file' => $directive->getOption('file')->getValue()]);
5658

57-
return new GenericNode('csv-table');
59+
return new GenericNode('csv-table');
60+
}
61+
62+
$csv = Reader::createFromStream($csvStream);
63+
} else {
64+
$lines = $blockContext->getDocumentIterator()->toArray();
65+
$csv = Reader::createFromString(implode("\n", $lines));
5866
}
5967

60-
$csv = Reader::createFromStream($csvStream);
6168
if ($directive->getOption('header-rows')->getValue() !== null) {
6269
$csv->setHeaderOffset((int) ($directive->getOption('header-rows')->getValue()) - 1);
6370
}
6471

6572
$header = null;
66-
if (empty($csv->getHeader()) === false) {
73+
if ($directive->hasOption('header')) {
74+
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
75+
$header = new TableRow();
76+
foreach ($headerCsv->first() as $column) {
77+
$columnNode = new TableColumn($column, 1, []);
78+
$header->addColumn($this->buildColumn($columnNode, $blockContext, $this->productions));
79+
}
80+
} elseif (empty($csv->getHeader()) === false) {
6781
$header = new TableRow();
6882
foreach ($csv->getHeader() as $column) {
6983
$columnNode = new TableColumn($column, 1, []);

packages/guides-restructured-text/src/RestructuredText/Parser/DirectiveOption.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace phpDocumentor\Guides\RestructuredText\Parser;
66

7+
use function strval;
8+
79
class DirectiveOption
810
{
911
public function __construct(private readonly string $name, private string|int|float|bool|null $value = null)
@@ -20,6 +22,11 @@ public function getValue(): string|int|float|bool|null
2022
return $this->value;
2123
}
2224

25+
public function toString(): string
26+
{
27+
return strval($this->value);
28+
}
29+
2330
public function appendValue(string $append): void
2431
{
2532
$this->value = ((string) $this->value) . $append;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Csv table with content</title>
5+
6+
</head>
7+
<body>
8+
<div class="section" id="csv-table-with-content">
9+
<h1>Csv table with content</h1>
10+
11+
<table>
12+
<thead>
13+
<tr>
14+
<th>Header 1</th>
15+
<th>Header 2</th>
16+
</tr>
17+
</thead>
18+
19+
<tbody>
20+
<tr>
21+
<td>1</td>
22+
<td>one</td>
23+
</tr>
24+
<tr>
25+
<td>2</td>
26+
<td>two</td>
27+
</tr>
28+
</tbody>
29+
</table>
30+
31+
</div>
32+
33+
</body>
34+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
Csv table with content
3+
======================
4+
5+
.. csv-table:: Numbers
6+
:header: "Header 1", "Header 2"
7+
:widths: 15, 15
8+
9+
1, "one"
10+
2, "two"
11+

0 commit comments

Comments
 (0)