Skip to content

Commit a0f295b

Browse files
committed
wrap captions (#305)
Signed-off-by: Michele Dolfi <dol@zurich.ibm.com>
1 parent aa5d127 commit a0f295b

10 files changed

+43
-34
lines changed

docling_core/transforms/serializer/html.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -949,22 +949,33 @@ def serialize_captions(
949949
excluded_refs = self.get_excluded_refs(**kwargs)
950950

951951
if DocItemLabel.CAPTION in params.labels:
952-
results.extend(
953-
[
954-
create_ser_result(text=html.escape(it.text), span_source=it)
955-
for cap in item.captions
956-
if isinstance(it := cap.resolve(self.doc), TextItem)
952+
for cap in item.captions:
953+
if (
954+
isinstance(it := cap.resolve(self.doc), TextItem)
957955
and it.self_ref not in excluded_refs
958-
]
959-
)
956+
):
957+
text_cap = it.text
958+
text_dir = get_text_direction(text_cap)
959+
dir_str = f' dir="{text_dir}"' if text_dir == "rtl" else ""
960+
cap_ser_res = create_ser_result(
961+
text=(
962+
f'<div class="caption"{dir_str}>'
963+
f"{html.escape(text_cap)}"
964+
f"</div>"
965+
),
966+
span_source=it,
967+
)
968+
results.append(cap_ser_res)
960969

961970
if params.include_annotations and item.self_ref not in excluded_refs:
962971
if isinstance(item, PictureItem):
963972
for ann in item.annotations:
964973
if ann_text := _get_picture_annotation_text(annotation=ann):
974+
text_dir = get_text_direction(ann_text)
975+
dir_str = f' dir="{text_dir}"' if text_dir == "rtl" else ""
965976
ann_ser_res = create_ser_result(
966977
text=(
967-
f'<div data-annotation-kind="{ann.kind}">'
978+
f'<div data-annotation-kind="{ann.kind}"{dir_str}>'
968979
f"{html.escape(ann_text)}"
969980
f"</div>"
970981
),
@@ -974,9 +985,7 @@ def serialize_captions(
974985

975986
text_res = params.caption_delim.join([r.text for r in results])
976987
if text_res:
977-
text_dir = get_text_direction(text_res)
978-
dir_str = f' dir="{text_dir}"' if text_dir == "rtl" else ""
979-
text_res = f"<{tag}{dir_str}>{text_res}</{tag}>"
988+
text_res = f"<{tag}>{text_res}</{tag}>"
980989
return create_ser_result(text=text_res, span_source=results)
981990

982991
def _generate_head(self) -> str:

test/data/doc/2206.01062.yaml.html

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

test/data/doc/2408.09869v3_enriched_split.gt.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

test/data/doc/activities.gt.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<h2>Summer activities</h2>
128128
<h2>Swimming in the lake</h2>
129129
<p>Duck</p>
130-
<figure><figcaption>Figure 1: This is a cute duckling</figcaption></figure>
130+
<figure><figcaption><div class="caption">Figure 1: This is a cute duckling</div></figcaption></figure>
131131
<h2>Let's swim!</h2>
132132
<p>To get started with swimming, first lay down in a water and try not to drown:</p>
133133
<ul>

test/data/doc/activities_p1.gt.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<h2>Summer activities</h2>
128128
<h2>Swimming in the lake</h2>
129129
<p>Duck</p>
130-
<figure><figcaption>Figure 1: This is a cute duckling</figcaption></figure>
130+
<figure><figcaption><div class="caption">Figure 1: This is a cute duckling</div></figcaption></figure>
131131
<h2>Let's swim!</h2>
132132
<p>To get started with swimming, first lay down in a water and try not to drown:</p>
133133
<ul>

test/data/doc/constructed_doc.embedded.html.gt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@
146146
</ol>
147147
<li>list item 4</li>
148148
</ul>
149-
<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>
150-
<figure><figcaption>This is the caption of figure 1.</figcaption></figure>
151-
<figure><figcaption>This is the caption of figure 2.</figcaption><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAIklEQVR4nO3BAQ0AAADCoPdPbQ8HFAAAAAAAAAAAAAAA8G4wQAABiwCo9wAAAABJRU5ErkJggg=="></figure>
149+
<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>
150+
<figure><figcaption><div class="caption">This is the caption of figure 1.</div></figcaption></figure>
151+
<figure><figcaption><div class="caption">This is the caption of figure 2.</div></figcaption><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAIklEQVR4nO3BAQ0AAADCoPdPbQ8HFAAAAAAAAAAAAAAA8G4wQAABiwCo9wAAAABJRU5ErkJggg=="></figure>
152152
<ul>
153153
<li>item 1 of list</li>
154154
</ul>

test/data/doc/constructed_doc.placeholder.html.gt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@
146146
</ol>
147147
<li>list item 4</li>
148148
</ul>
149-
<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>
150-
<figure><figcaption>This is the caption of figure 1.</figcaption></figure>
151-
<figure><figcaption>This is the caption of figure 2.</figcaption></figure>
149+
<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>
150+
<figure><figcaption><div class="caption">This is the caption of figure 1.</div></figcaption></figure>
151+
<figure><figcaption><div class="caption">This is the caption of figure 2.</div></figcaption></figure>
152152
<ul>
153153
<li>item 1 of list</li>
154154
</ul>

test/data/doc/constructed_doc.referenced.html.gt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@
146146
</ol>
147147
<li>list item 4</li>
148148
</ul>
149-
<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>
150-
<figure><figcaption>This is the caption of figure 1.</figcaption></figure>
151-
<figure><figcaption>This is the caption of figure 2.</figcaption><img src="constructed_images/image_000001_f3cc103136423a57975750907ebc1d367e2985ac6338976d4d5a439f50323f4a.png"></figure>
149+
<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>
150+
<figure><figcaption><div class="caption">This is the caption of figure 1.</div></figcaption></figure>
151+
<figure><figcaption><div class="caption">This is the caption of figure 2.</div></figcaption><img src="constructed_images/image_000001_f3cc103136423a57975750907ebc1d367e2985ac6338976d4d5a439f50323f4a.png"></figure>
152152
<ul>
153153
<li>item 1 of list</li>
154154
</ul>

test/data/doc/constructed_document.yaml.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ <h2>1. Introduction</h2>
146146
</ol>
147147
<li>list item 4</li>
148148
</ul>
149-
<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>
150-
<figure><figcaption>This is the caption of figure 1.</figcaption></figure>
151-
<figure><figcaption>This is the caption of figure 2.</figcaption></figure>
149+
<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>
150+
<figure><figcaption><div class="caption">This is the caption of figure 1.</div></figcaption></figure>
151+
<figure><figcaption><div class="caption">This is the caption of figure 2.</div></figcaption></figure>
152152
<ul>
153153
<li>item 1 of list</li>
154154
</ul>

test/data/doc/dummy_doc.yaml.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<body>
126126
<div class='page'>
127127
<h1>DocLayNet: A Large Human-Annotated Dataset for Document-Layout Analysis</h1>
128-
<figure><figcaption>Figure 1: Four examples of complex page layouts across different document categories <div data-annotation-kind="classification">bar chart</div> <div data-annotation-kind="description">...</div> <div data-annotation-kind="molecule_data">CC1=NNC(C2=CN3C=CN=C3C(CC3=CC(F)=CC(F)=C3)=N2)=N1</div></figcaption></figure>
128+
<figure><figcaption><div class="caption">Figure 1: Four examples of complex page layouts across different document categories</div> <div data-annotation-kind="classification">bar chart</div> <div data-annotation-kind="description">...</div> <div data-annotation-kind="molecule_data">CC1=NNC(C2=CN3C=CN=C3C(CC3=CC(F)=CC(F)=C3)=N2)=N1</div></figcaption></figure>
129129
</div>
130130
</body>
131131
</html>

0 commit comments

Comments
 (0)