Skip to content

Commit c116fed

Browse files
committed
[BUGFIX] Display order numbers correctly
Resolves #1003
1 parent 2d1cf56 commit c116fed

File tree

17 files changed

+211
-49
lines changed

17 files changed

+211
-49
lines changed

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/EnumeratedListRule.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
116116
}
117117

118118
$items[] = $this->parseListItem($listConfig, $buffer, $blockContext);
119+
$start = null;
120+
$orderType = null;
121+
if (isset($items[0])) {
122+
$start = $items[0]->getOrderNumber();
123+
$orderType = $items[0]->getOrderType();
124+
}
119125

120-
return new ListNode($items, true);
126+
return new ListNode($items, true, $start, $orderType);
121127
}
122128

123129
private function isListLine(string|null $line): bool
@@ -168,11 +174,16 @@ private function isListItemStart(string|null $line, string|null $listMarker = nu
168174
return true;
169175
}
170176

171-
/** @param array{marker: string, indenting: int} $listConfig */
177+
/** @param array{marker: string, indenting: int, marker_type: string} $listConfig */
172178
private function parseListItem(array $listConfig, Buffer $buffer, BlockContext $blockContext): ListItemNode
173179
{
174180
$marker = trim($listConfig['marker'], '.()');
175-
$listItem = new ListItemNode($marker, false, []);
181+
$orderNumber = null;
182+
if ($marker !== '#') {
183+
$orderNumber = $marker;
184+
}
185+
186+
$listItem = new ListItemNode($marker, false, [], $orderNumber);
176187
$subContext = new BlockContext($blockContext->getDocumentParserContext(), $buffer->getLinesString(), false, $blockContext->getDocumentIterator()->key());
177188
while ($subContext->getDocumentIterator()->valid()) {
178189
$this->productions->apply($subContext, $listItem);
@@ -185,7 +196,7 @@ private function parseListItem(array $listConfig, Buffer $buffer, BlockContext $
185196

186197
// the list item offset is determined by the offset of the first text
187198
if ($nodes[0] instanceof ParagraphNode) {
188-
return new ListItemNode($marker, false, $nodes[0]->getChildren());
199+
return new ListItemNode($marker, false, $nodes[0]->getChildren(), $orderNumber, $listConfig['marker_type']);
189200
}
190201

191202
return $listItem;

packages/guides-restructured-text/tests/unit/Parser/Productions/EnumeratedListRuleTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ public function testSimpleListCreation(): void
119119
self::assertEquals(
120120
new ListNode(
121121
[
122-
new ListItemNode('1', false, [new RawNode('first items')]),
123-
new ListItemNode('2', false, [new RawNode('second item')]),
122+
new ListItemNode('1', false, [new RawNode('first items')], '1'),
123+
new ListItemNode('2', false, [new RawNode('second item')], '2'),
124124
],
125125
true,
126+
'1',
127+
null,
126128
),
127129
$result,
128130
);
@@ -151,16 +153,18 @@ public function testListWithoutNewLineInParagraphResultsInWarning(): void
151153
self::assertEquals(
152154
new ListNode(
153155
[
154-
new ListItemNode('1', false, [new RawNode('first items')]),
155-
new ListItemNode('2', false, [new RawNode('second item')]),
156+
new ListItemNode('1', false, [new RawNode('first items')], '1'),
157+
new ListItemNode('2', false, [new RawNode('second item')], '2'),
156158
],
157159
true,
160+
'1',
161+
null,
158162
),
159163
$result,
160164
);
161165
}
162166

163-
public function testListFistTekstOnNewLine(): void
167+
public function testListWithTextOnNewLine(): void
164168
{
165169
$input = <<<'INPUT'
166170
(#)
@@ -191,6 +195,8 @@ public function testListFistTekstOnNewLine(): void
191195
new ListItemNode('#', false, [new RawNode("second item\nother line")]),
192196
],
193197
true,
198+
null,
199+
null,
194200
),
195201
$result,
196202
);
@@ -222,10 +228,12 @@ public function testListWithOddIndenting(): void
222228
self::assertEquals(
223229
new ListNode(
224230
[
225-
new ListItemNode('1', false, [new RawNode('first items')]),
226-
new ListItemNode('2', false, [new RawNode("second item\nother line")]),
231+
new ListItemNode('1', false, [new RawNode('first items')], '1'),
232+
new ListItemNode('2', false, [new RawNode("second item\nother line")], '2'),
227233
],
228234
true,
235+
'1',
236+
null,
229237
),
230238
$result,
231239
);

packages/guides/resources/template/html/body/list/list-item.html.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<li{{- node.prefix == '-' ? ' class="dash"' : '' -}}>
1+
<li{{- node.prefix == '-' ? ' class="dash"' : '' -}}
2+
{%- if node.orderNumber %} value="{{ node.orderNumber }}"{% endif -%}>
23
{%- for child in node.children -%}
34
{{ renderNode(child) }}
45
{%- endfor -%}

packages/guides/resources/template/html/body/list/list.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
{% set keyword = 'ol' %}
55
{% endif %}
66

7-
<{{ keyword }}{% if node.classes %} class="{{ node.classesString }}"{% endif %}>
7+
<{{ keyword }}{% if node.classes %} class="{{ node.classesString }}"{% endif %}
8+
{%- if node.start %} start="{{ node.start }}"{% endif -%}
9+
{%- if node.orderingType %} type="{{ renderOrderedListType(node.orderingType) }}"{% endif -%}>
810
{% for child in node.children %}
911
{{ renderNode(child) }}
1012
{% endfor %}

packages/guides/src/Nodes/ListItemNode.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function __construct(
2525
private readonly string $prefix,
2626
private readonly bool $ordered,
2727
array $contents,
28+
private readonly string|null $orderNumber = null,
29+
private readonly string|null $orderType = null,
2830
) {
2931
parent::__construct($contents);
3032
}
@@ -38,4 +40,14 @@ public function isOrdered(): bool
3840
{
3941
return $this->ordered;
4042
}
43+
44+
public function getOrderNumber(): string|null
45+
{
46+
return $this->orderNumber;
47+
}
48+
49+
public function getOrderType(): string|null
50+
{
51+
return $this->orderType;
52+
}
4153
}

packages/guides/src/Nodes/ListNode.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,39 @@
1717
final class ListNode extends CompoundNode
1818
{
1919
/** @param ListItemNode[] $items */
20-
public function __construct(array $items, private readonly bool $ordered = false)
21-
{
20+
public function __construct(
21+
array $items,
22+
private readonly bool $ordered = false,
23+
private string|null $start = null,
24+
private string|null $orderingType = null,
25+
) {
2226
parent::__construct($items);
2327
}
2428

29+
public function getStart(): string|null
30+
{
31+
return $this->start;
32+
}
33+
34+
public function setStart(string|null $start): ListNode
35+
{
36+
$this->start = $start;
37+
38+
return $this;
39+
}
40+
41+
public function getOrderingType(): string|null
42+
{
43+
return $this->orderingType;
44+
}
45+
46+
public function setOrderingType(string|null $orderingType): ListNode
47+
{
48+
$this->orderingType = $orderingType;
49+
50+
return $this;
51+
}
52+
2553
public function isOrdered(): bool
2654
{
2755
return $this->ordered;

packages/guides/src/Twig/AssetsExtension.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getFunctions(): array
5959
new TwigFunction('renderBreadcrumb', $this->renderBreadcrumb(...), ['is_safe' => ['html'], 'needs_context' => true]),
6060
new TwigFunction('renderMenu', $this->renderMenu(...), ['is_safe' => ['html'], 'needs_context' => true, 'deprecated' => true]),
6161
new TwigFunction('renderTarget', $this->renderTarget(...), ['is_safe' => ['html'], 'needs_context' => true]),
62+
new TwigFunction('renderOrderedListType', $this->renderOrderedListType(...), ['is_safe' => ['html'], 'needs_context' => false]),
6263
];
6364
}
6465

@@ -207,4 +208,22 @@ private function getRenderContext(array $context): RenderContext
207208

208209
return $renderContext;
209210
}
211+
212+
public function renderOrderedListType(string $listType): string
213+
{
214+
switch ($listType) {
215+
case 'numberdot':
216+
case 'numberparentheses':
217+
case 'numberright-parenthesis':
218+
return '1';
219+
220+
case 'romandot':
221+
case 'romanparentheses':
222+
case 'romanright-parenthesis':
223+
return 'i';
224+
225+
default:
226+
return 'a';
227+
}
228+
}
210229
}

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
<code><![CDATA[scalarNode]]></code>
2424
</UndefinedInterfaceMethod>
2525
</file>
26-
<file src="packages/guides-restructured-text/src/RestructuredText/Parser/Productions/EnumeratedListRule.php">
27-
<InvalidArgument>
28-
<code><![CDATA[$listConfig]]></code>
29-
<code><![CDATA[$listConfig]]></code>
30-
</InvalidArgument>
31-
</file>
3226
<file src="packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineMarkupRule.php">
3327
<MoreSpecificImplementedParamType>
3428
<code><![CDATA[$on]]></code>
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<p>Testing an arabic numerals list:</p>
2-
<ol>
3-
<li>First item</li>
4-
<li>Second item</li>
5-
<li>Third item
2+
<ol start="1" type="1">
3+
<li value="1">First item</li>
4+
<li value="2">Second item</li>
5+
<li value="3">Third item
66
Multiline</li>
77
</ol>
88
<p>And an auto-enumerated list:</p>
9-
<ol>
9+
<ol type="1">
1010
<li>First item
1111
Multiline</li>
1212
<li>Second item</li>
1313
</ol>
1414
<p>Using parentheses:</p>
15-
<ol>
16-
<li>First item</li>
17-
<li>Second item</li>
15+
<ol start="1" type="1">
16+
<li value="1">First item</li>
17+
<li value="2">Second item</li>
1818
</ol>
1919
<p>Using right-parenthesis:</p>
20-
<ol>
21-
<li>First item</li>
22-
<li>Second item</li>
20+
<ol start="1" type="1">
21+
<li value="1">First item</li>
22+
<li value="2">Second item</li>
2323
</ol>

tests/Functional/tests/list-ordered-nested/list-ordered-nested.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<p>To create a numbered list:</p>
2-
<ol>
2+
<ol type="1">
33
<li>add a blank line before and after the list</li>
44
<li>indent the list item text by 4 spaces - including the item sign</li>
55
<li>
66
<p>to create a nested list:</p>
7-
<ol>
7+
<ol type="1">
88
<li>indent the items by 4 spaces (left-align with parent item text)</li>
99
<li>apply rules of parent list (blank lines, item text indentation, ..)</li>
1010
</ol>

tests/Integration/tests/class/class-in-list/expected/index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
<h1>rst-class within list</h1>
44

55

6-
<ol class="bignums-xxl">
7-
<li>
6+
<ol class="bignums-xxl" start="1">
7+
<li value="1">
88
<p>ONE One one bignums-xxl</p>
99

1010
<p>This is the story of my life ...</p>
1111

1212

13-
<ol class="bignums">
14-
<li>
13+
<ol class="bignums" start="1">
14+
<li value="1">
1515
<p>When I was young</p>
1616

1717

18-
<ol>
18+
<ol type="1">
1919
<li>this</li>
2020
<li>and that</li>
2121
<li>and this</li>
2222
</ol>
2323
</li>
24-
<li>
24+
<li value="2">
2525
<p>When I was grown</p>
2626

2727
<p>Oops, ...</p>
2828
</li>
29-
<li>
29+
<li value="3">
3030
<p>When I was old</p>
3131

3232
<p>Oh dear, ...</p>

tests/Integration/tests/lists/big-numbers/expected/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<h1>Some Document</h1>
44

55

6-
<ol class="bignums">
7-
<li>
6+
<ol class="bignums" start="1">
7+
<li value="1">
88
<p>First Item</p>
99

1010
<p>This is the body text</p>
1111
</li>
12-
<li>
12+
<li value="2">
1313
<p>Second Item</p>
1414

1515
<p>More body text</p>
@@ -18,13 +18,13 @@ <h1>Some Document</h1>
1818

1919

2020

21-
<ol class="bignums-xxl">
22-
<li>
21+
<ol class="bignums-xxl" start="1">
22+
<li value="1">
2323
<p>And then with xxl big numbers</p>
2424

2525
<p>This is the body text</p>
2626
</li>
27-
<li>
27+
<li value="2">
2828
<p>The second xxl item</p>
2929

3030
<p>More body text ....</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- content start -->
2+
<div class="section" id="document-title">
3+
<h1>Document Title</h1>
4+
5+
<p>1. First numbered list item.</p>
6+
7+
8+
<p>Lorem Ipsum</p>
9+
10+
11+
<p>2. This is the second numbered list item.</p>
12+
13+
14+
<p>Lorem Ipsum</p>
15+
16+
17+
<p>3. and then the third numbered list item.</p>
18+
19+
20+
<p>Lorem Ipsum</p>
21+
22+
</div>
23+
<!-- content end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==============
2+
Document Title
3+
==============
4+
5+
1\. First numbered list item.
6+
7+
Lorem Ipsum
8+
9+
2\. This is the second numbered list item.
10+
11+
Lorem Ipsum
12+
13+
3\. and then the third numbered list item.
14+
15+
Lorem Ipsum
16+

0 commit comments

Comments
 (0)