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
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
}

$items[] = $this->parseListItem($listConfig, $buffer, $blockContext);
$start = null;
$orderType = null;
if (isset($items[0])) {
$start = $items[0]->getOrderNumber();
$orderType = $items[0]->getOrderType();
}

return new ListNode($items, true);
return new ListNode($items, true, $start, $orderType);
}

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

/** @param array{marker: string, indenting: int} $listConfig */
/** @param array{marker: string, indenting: int, marker_type: string} $listConfig */
private function parseListItem(array $listConfig, Buffer $buffer, BlockContext $blockContext): ListItemNode
{
$marker = trim($listConfig['marker'], '.()');
$listItem = new ListItemNode($marker, false, []);
$orderNumber = null;
if ($marker !== '#') {
$orderNumber = $marker;
}

$listItem = new ListItemNode($marker, false, [], $orderNumber);
$subContext = new BlockContext($blockContext->getDocumentParserContext(), $buffer->getLinesString(), false, $blockContext->getDocumentIterator()->key());
while ($subContext->getDocumentIterator()->valid()) {
$this->productions->apply($subContext, $listItem);
Expand All @@ -185,7 +196,7 @@ private function parseListItem(array $listConfig, Buffer $buffer, BlockContext $

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

return $listItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ public function testSimpleListCreation(): void
self::assertEquals(
new ListNode(
[
new ListItemNode('1', false, [new RawNode('first items')]),
new ListItemNode('2', false, [new RawNode('second item')]),
new ListItemNode('1', false, [new RawNode('first items')], '1'),
new ListItemNode('2', false, [new RawNode('second item')], '2'),
],
true,
'1',
null,
),
$result,
);
Expand Down Expand Up @@ -151,16 +153,18 @@ public function testListWithoutNewLineInParagraphResultsInWarning(): void
self::assertEquals(
new ListNode(
[
new ListItemNode('1', false, [new RawNode('first items')]),
new ListItemNode('2', false, [new RawNode('second item')]),
new ListItemNode('1', false, [new RawNode('first items')], '1'),
new ListItemNode('2', false, [new RawNode('second item')], '2'),
],
true,
'1',
null,
),
$result,
);
}

public function testListFistTekstOnNewLine(): void
public function testListWithTextOnNewLine(): void
{
$input = <<<'INPUT'
(#)
Expand Down Expand Up @@ -191,6 +195,8 @@ public function testListFistTekstOnNewLine(): void
new ListItemNode('#', false, [new RawNode("second item\nother line")]),
],
true,
null,
null,
),
$result,
);
Expand Down Expand Up @@ -222,10 +228,12 @@ public function testListWithOddIndenting(): void
self::assertEquals(
new ListNode(
[
new ListItemNode('1', false, [new RawNode('first items')]),
new ListItemNode('2', false, [new RawNode("second item\nother line")]),
new ListItemNode('1', false, [new RawNode('first items')], '1'),
new ListItemNode('2', false, [new RawNode("second item\nother line")], '2'),
],
true,
'1',
null,
),
$result,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<li{{- node.prefix == '-' ? ' class="dash"' : '' -}}>
<li{{- node.prefix == '-' ? ' class="dash"' : '' -}}
{%- if node.orderNumber %} value="{{ node.orderNumber }}"{% endif -%}>
{%- for child in node.children -%}
{{ renderNode(child) }}
{%- endfor -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
{% set keyword = 'ol' %}
{% endif %}

<{{ keyword }}{% if node.classes %} class="{{ node.classesString }}"{% endif %}>
<{{ keyword }}{% if node.classes %} class="{{ node.classesString }}"{% endif %}
{%- if node.start %} start="{{ node.start }}"{% endif -%}
{%- if node.orderingType %} type="{{ renderOrderedListType(node.orderingType) }}"{% endif -%}>
{% for child in node.children %}
{{ renderNode(child) }}
{% endfor %}
Expand Down
12 changes: 12 additions & 0 deletions packages/guides/src/Nodes/ListItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __construct(
private readonly string $prefix,
private readonly bool $ordered,
array $contents,
private readonly string|null $orderNumber = null,
private readonly string|null $orderType = null,
) {
parent::__construct($contents);
}
Expand All @@ -38,4 +40,14 @@ public function isOrdered(): bool
{
return $this->ordered;
}

public function getOrderNumber(): string|null
{
return $this->orderNumber;
}

public function getOrderType(): string|null
{
return $this->orderType;
}
}
32 changes: 30 additions & 2 deletions packages/guides/src/Nodes/ListNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,39 @@
final class ListNode extends CompoundNode
{
/** @param ListItemNode[] $items */
public function __construct(array $items, private readonly bool $ordered = false)
{
public function __construct(
array $items,
private readonly bool $ordered = false,
private string|null $start = null,
private string|null $orderingType = null,
) {
parent::__construct($items);
}

public function getStart(): string|null
{
return $this->start;
}

public function setStart(string|null $start): ListNode
{
$this->start = $start;

return $this;
}

public function getOrderingType(): string|null
{
return $this->orderingType;
}

public function setOrderingType(string|null $orderingType): ListNode
{
$this->orderingType = $orderingType;

return $this;
}

public function isOrdered(): bool
{
return $this->ordered;
Expand Down
19 changes: 19 additions & 0 deletions packages/guides/src/Twig/AssetsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getFunctions(): array
new TwigFunction('renderBreadcrumb', $this->renderBreadcrumb(...), ['is_safe' => ['html'], 'needs_context' => true]),
new TwigFunction('renderMenu', $this->renderMenu(...), ['is_safe' => ['html'], 'needs_context' => true, 'deprecated' => true]),
new TwigFunction('renderTarget', $this->renderTarget(...), ['is_safe' => ['html'], 'needs_context' => true]),
new TwigFunction('renderOrderedListType', $this->renderOrderedListType(...), ['is_safe' => ['html'], 'needs_context' => false]),
];
}

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

return $renderContext;
}

public function renderOrderedListType(string $listType): string
{
switch ($listType) {
case 'numberdot':
case 'numberparentheses':
case 'numberright-parenthesis':
return '1';

case 'romandot':
case 'romanparentheses':
case 'romanright-parenthesis':
return 'i';

default:
return 'a';
}
}
}
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
<code><![CDATA[scalarNode]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="packages/guides-restructured-text/src/RestructuredText/Parser/Productions/EnumeratedListRule.php">
<InvalidArgument>
<code><![CDATA[$listConfig]]></code>
<code><![CDATA[$listConfig]]></code>
</InvalidArgument>
</file>
<file src="packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineMarkupRule.php">
<MoreSpecificImplementedParamType>
<code><![CDATA[$on]]></code>
Expand Down
22 changes: 11 additions & 11 deletions tests/Functional/tests/enumerated/enumerated.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<p>Testing an arabic numerals list:</p>
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item
<ol start="1" type="1">
<li value="1">First item</li>
<li value="2">Second item</li>
<li value="3">Third item
Multiline</li>
</ol>
<p>And an auto-enumerated list:</p>
<ol>
<ol type="1">
<li>First item
Multiline</li>
<li>Second item</li>
</ol>
<p>Using parentheses:</p>
<ol>
<li>First item</li>
<li>Second item</li>
<ol start="1" type="1">
<li value="1">First item</li>
<li value="2">Second item</li>
</ol>
<p>Using right-parenthesis:</p>
<ol>
<li>First item</li>
<li>Second item</li>
<ol start="1" type="1">
<li value="1">First item</li>
<li value="2">Second item</li>
</ol>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<p>To create a numbered list:</p>
<ol>
<ol type="1">
<li>add a blank line before and after the list</li>
<li>indent the list item text by 4 spaces - including the item sign</li>
<li>
<p>to create a nested list:</p>
<ol>
<ol type="1">
<li>indent the items by 4 spaces (left-align with parent item text)</li>
<li>apply rules of parent list (blank lines, item text indentation, ..)</li>
</ol>
Expand Down
14 changes: 7 additions & 7 deletions tests/Integration/tests/class/class-in-list/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
<h1>rst-class within list</h1>


<ol class="bignums-xxl">
<li>
<ol class="bignums-xxl" start="1">
<li value="1">
<p>ONE One one bignums-xxl</p>

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


<ol class="bignums">
<li>
<ol class="bignums" start="1">
<li value="1">
<p>When I was young</p>


<ol>
<ol type="1">
<li>this</li>
<li>and that</li>
<li>and this</li>
</ol>
</li>
<li>
<li value="2">
<p>When I was grown</p>

<p>Oops, ...</p>
</li>
<li>
<li value="3">
<p>When I was old</p>

<p>Oh dear, ...</p>
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/tests/lists/big-numbers/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<h1>Some Document</h1>


<ol class="bignums">
<li>
<ol class="bignums" start="1">
<li value="1">
<p>First Item</p>

<p>This is the body text</p>
</li>
<li>
<li value="2">
<p>Second Item</p>

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



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

<p>This is the body text</p>
</li>
<li>
<li value="2">
<p>The second xxl item</p>

<p>More body text ....</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- content start -->
<div class="section" id="document-title">
<h1>Document Title</h1>

<p>1. First numbered list item.</p>


<p>Lorem Ipsum</p>


<p>2. This is the second numbered list item.</p>


<p>Lorem Ipsum</p>


<p>3. and then the third numbered list item.</p>


<p>Lorem Ipsum</p>

</div>
<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
==============
Document Title
==============

1\. First numbered list item.

Lorem Ipsum

2\. This is the second numbered list item.

Lorem Ipsum

3\. and then the third numbered list item.

Lorem Ipsum

Loading