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
17 changes: 17 additions & 0 deletions docling_core/transforms/serializer/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,23 @@ def serialize(
**kwargs,
)

# Append nested list to parent list item:
i = 0
while i < len(parts):
prt = parts[i]
if prt.text.startswith(("<ul>", "<ol>")):
for j in range(i - 1, -1, -1):
if parts[j].text.startswith(("<li>", "<li ")) and parts[
j
].text.endswith("</li>"):
before, _, _ = parts[j].text.rpartition("</li>")
parts[j].text = f"{before}\n{prt.text}\n</li>"
break
if j > -1:
parts.pop(i)
else:
i += 1

# Add all child parts
text_res = "\n".join(
[
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_doc.embedded.html.gt
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@
<ul>
<li style="list-style-type: '■ ';">list item 1</li>
<li style="list-style-type: '■ ';">list item 2</li>
<li style="list-style-type: '■ ';">list item 3</li>
<li style="list-style-type: '■ ';">list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li style="list-style-type: '■ ';">list item 4</li>
</ul>
<table><caption><div class="caption">This is the caption of table 1.</div></caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -158,12 +160,13 @@
</ul>
<ul>
<li style="list-style-type: '■ ';">item 1 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list
<ul>
<li style="list-style-type: '□ ';">item 1 of sub list</li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a code snippet: <code>print("Hello world")</code> (to be displayed inline)</span></li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -185,16 +188,18 @@
<ol>
<li style="list-style-type: '(i) ';">Item 1 in A</li>
<li style="list-style-type: '(ii) ';">Item 2 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A
<ol>
<li>Item 1 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li style="list-style-type: '(iv) ';">Item 4 in A</li>
</ol>
<ul>
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ <h2>1. Introduction</h2>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li>list item 4</li>
</ul>
<table><caption>This is the caption of table 1.</caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -157,12 +159,13 @@ <h2>1. Introduction</h2>
</ul>
<ul>
<li>item 1 of neighboring list</li>
<li>item 2 of neighboring list</li>
<li>item 2 of neighboring list
<ul>
<li>item 1 of sub list</li>
<li><span class='inline-group'>Here a code snippet: <code>&lt;p&gt;Hello world&lt;/p&gt;</code> (to be displayed inline)</span></li>
<li><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -186,16 +189,18 @@ <h2>1. Introduction</h2>
<ol>
<li>Item 1 in A</li>
<li>Item 2 in A</li>
<li>Item 3 in A</li>
<li>Item 3 in A
<ol>
<li>Item 1 in B</li>
<li>Item 2 in B</li>
<li>Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li>Item 4 in A</li>
</ol>
<p>The end.</p>
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_doc.placeholder.html.gt
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@
<ul>
<li style="list-style-type: '■ ';">list item 1</li>
<li style="list-style-type: '■ ';">list item 2</li>
<li style="list-style-type: '■ ';">list item 3</li>
<li style="list-style-type: '■ ';">list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li style="list-style-type: '■ ';">list item 4</li>
</ul>
<table><caption><div class="caption">This is the caption of table 1.</div></caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -158,12 +160,13 @@
</ul>
<ul>
<li style="list-style-type: '■ ';">item 1 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list
<ul>
<li style="list-style-type: '□ ';">item 1 of sub list</li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a code snippet: <code>print("Hello world")</code> (to be displayed inline)</span></li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -185,16 +188,18 @@
<ol>
<li style="list-style-type: '(i) ';">Item 1 in A</li>
<li style="list-style-type: '(ii) ';">Item 2 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A
<ol>
<li>Item 1 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li style="list-style-type: '(iv) ';">Item 4 in A</li>
</ol>
<ul>
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_doc.referenced.html.gt
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@
<ul>
<li style="list-style-type: '■ ';">list item 1</li>
<li style="list-style-type: '■ ';">list item 2</li>
<li style="list-style-type: '■ ';">list item 3</li>
<li style="list-style-type: '■ ';">list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li style="list-style-type: '■ ';">list item 4</li>
</ul>
<table><caption><div class="caption">This is the caption of table 1.</div></caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -158,12 +160,13 @@
</ul>
<ul>
<li style="list-style-type: '■ ';">item 1 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list
<ul>
<li style="list-style-type: '□ ';">item 1 of sub list</li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a code snippet: <code>print("Hello world")</code> (to be displayed inline)</span></li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -185,16 +188,18 @@
<ol>
<li style="list-style-type: '(i) ';">Item 1 in A</li>
<li style="list-style-type: '(ii) ';">Item 2 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A
<ol>
<li>Item 1 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li style="list-style-type: '(iv) ';">Item 4 in A</li>
</ol>
<ul>
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_document.yaml.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ <h2>1. Introduction</h2>
<ul>
<li style="list-style-type: '■ ';">list item 1</li>
<li style="list-style-type: '■ ';">list item 2</li>
<li style="list-style-type: '■ ';">list item 3</li>
<li style="list-style-type: '■ ';">list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li style="list-style-type: '■ ';">list item 4</li>
</ul>
<table><caption><div class="caption">This is the caption of table 1.</div></caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -158,12 +160,13 @@ <h2>1. Introduction</h2>
</ul>
<ul>
<li style="list-style-type: '■ ';">item 1 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list</li>
<li style="list-style-type: '■ ';">item 2 of neighboring list
<ul>
<li style="list-style-type: '□ ';">item 1 of sub list</li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a code snippet: <code>print("Hello world")</code> (to be displayed inline)</span></li>
<li style="list-style-type: '□ ';"><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -185,16 +188,18 @@ <h2>1. Introduction</h2>
<ol>
<li style="list-style-type: '(i) ';">Item 1 in A</li>
<li style="list-style-type: '(ii) ';">Item 2 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A</li>
<li style="list-style-type: '(iii) ';">Item 3 in A
<ol>
<li>Item 1 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B</li>
<li style="list-style-type: '42. ';">Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li style="list-style-type: '(iv) ';">Item 4 in A</li>
</ol>
<ul>
Expand Down
15 changes: 10 additions & 5 deletions test/data/doc/constructed_orig_false.gt.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ <h2>1. Introduction</h2>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 3
<ol>
<li>list item 3.a</li>
<li>list item 3.b</li>
<li>list item 3.c</li>
<li>list item 3.c
<ol>
<li>list item 3.c.i</li>
</ol>
</li>
</ol>
</li>
<li>list item 4</li>
</ul>
<table><caption><div class="caption">This is the caption of table 1.</div></caption><tbody><tr><td rowspan="2">Product</td><td colspan="2">Years</td></tr><tr><td>2016</td><td>2017</td></tr><tr><td>Apple</td><td>49823</td><td>695944</td></tr></tbody></table>
Expand All @@ -158,12 +160,13 @@ <h2>1. Introduction</h2>
</ul>
<ul>
<li>item 1 of neighboring list</li>
<li>item 2 of neighboring list</li>
<li>item 2 of neighboring list
<ul>
<li>item 1 of sub list</li>
<li><span class='inline-group'>Here a code snippet: <code>print("Hello world")</code> (to be displayed inline)</span></li>
<li><span class='inline-group'>Here a formula: <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>E</mi><mo>&#x0003D;</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="TeX">E=mc^2</annotation></math> (to be displayed inline)</span></li>
</ul>
</li>
</ul>
<p>Here a code block:</p>
<pre><code>print("Hello world")</code></pre>
Expand All @@ -185,16 +188,18 @@ <h2>1. Introduction</h2>
<ol>
<li>Item 1 in A</li>
<li>Item 2 in A</li>
<li>Item 3 in A</li>
<li>Item 3 in A
<ol>
<li>Item 1 in B</li>
<li>Item 2 in B</li>
<li>Item 2 in B
<ol>
<li>Item 1 in C</li>
<li>Item 2 in C</li>
</ol>
</li>
<li>Item 3 in B</li>
</ol>
</li>
<li>Item 4 in A</li>
</ol>
<ul>
Expand Down
Loading
Loading