Skip to content

Commit da165c8

Browse files
authored
Fixes "Consecutive links in FPDF.multi_cell(...)" (#1815)
* Fix issue of consecutive md links do not split into seperate fragments * Fix links to own github page * Add doc of fix to changelog
1 parent 87daf93 commit da165c8

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2525
* transform application on user space gradients - _cf._ [issue #1784](https://github.com/py-pdf/fpdf2/issues/1784)
2626
* dependency extras for camelot-py and endesive on pyproject.toml - _cf._ [issue #1792](https://github.com/py-pdf/fpdf2/issues/1792)
2727
* preserve link annotations during dry-run of pdf.multi_cell - _cf._ [issue #1807](https://github.com/py-pdf/fpdf2/issues/1807)
28+
* preserve two consecutive markdown links (without space inbetween) - _cf._ [issue #1814](https://github.com/py-pdf/fpdf2/issues/1814)
2829
### Changed
2930
* skip byte-for-byte compressed data comparison when zlib-ng is detected, regardless of OS
3031

fpdf/line_break.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,10 @@ def add_character(
579579
# character attributes. If the last existing fragment doesn't match
580580
# the properties of the pending character -> add a new fragment.
581581
elif isinstance(original_fragment, Fragment):
582-
if isinstance(
583-
self.fragments[-1], Fragment
584-
) and not original_fragment.has_same_style(self.fragments[-1]):
582+
if isinstance(self.fragments[-1], Fragment) and not (
583+
original_fragment.has_same_style(self.fragments[-1])
584+
and url == self.fragments[-1].link
585+
):
585586
self.fragments.append(
586587
original_fragment.__class__(
587588
characters="",
1.9 KB
Binary file not shown.
-8 Bytes
Binary file not shown.

test/text/test_multi_cell_markdown.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_multi_cell_markdown_link_dry_run(tmp_path):
157157

158158
pdf.multi_cell(
159159
pdf.epw,
160-
text="**Start** [One Page Dungeon Context](https://www.dungeoncontest.com/) __End__",
160+
text="**Start** [fpdf2 github](https://github.com/py-pdf/fpdf2) __End__",
161161
dry_run=True,
162162
markdown=True,
163163
new_x="left",
@@ -167,7 +167,7 @@ def test_multi_cell_markdown_link_dry_run(tmp_path):
167167

168168
pdf.multi_cell(
169169
pdf.epw,
170-
text="**Start** [One Page Dungeon Context](https://www.dungeoncontest.com/) __End__",
170+
text="**Start** [fpdf2 github](https://github.com/py-pdf/fpdf2) __End__",
171171
markdown=True,
172172
new_x="left",
173173
new_y="next",
@@ -176,7 +176,7 @@ def test_multi_cell_markdown_link_dry_run(tmp_path):
176176

177177
pdf.multi_cell(
178178
pdf.epw,
179-
text="**Start** [One Page Dungeon Context](https://www.dungeoncontest.com/) __End__",
179+
text="**Start** [fpdf2 github](https://github.com/py-pdf/fpdf2) __End__",
180180
dry_run=True,
181181
markdown=True,
182182
new_x="left",
@@ -186,11 +186,37 @@ def test_multi_cell_markdown_link_dry_run(tmp_path):
186186

187187
pdf.multi_cell(
188188
pdf.epw,
189-
text="**Start** [One Page Dungeon Context](https://www.dungeoncontest.com/) __End__",
189+
text="**Start** [fpdf2 github](https://github.com/py-pdf/fpdf2) __End__",
190190
markdown=True,
191191
new_x="left",
192192
new_y="next",
193193
)
194194
assert len(pdf.pages[1].annots) == 2
195195

196196
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_link_dry_run.pdf", tmp_path)
197+
198+
199+
def test_multi_cell_markdown_consecutive_links(tmp_path):
200+
link1 = "[fpdf2 github](https://github.com/py-pdf/fpdf2)"
201+
link2 = "[fpdf2 github Releases](https://github.com/py-pdf/fpdf2/releases)"
202+
203+
pdf = fpdf.FPDF()
204+
pdf.set_font("Helvetica")
205+
pdf.add_page()
206+
pdf.multi_cell(
207+
pdf.epw,
208+
text=f"**Start** {link1:s} {link2:s} __End__",
209+
markdown=True,
210+
new_x="left",
211+
new_y="next",
212+
)
213+
assert len(pdf.pages[pdf.page].annots) == 2
214+
pdf.multi_cell(
215+
pdf.epw,
216+
text=f"**Start** {link1:s}{link2:s} __End__",
217+
markdown=True,
218+
new_x="left",
219+
new_y="next",
220+
)
221+
assert len(pdf.pages[pdf.page].annots) == 4
222+
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_consecutive_links.pdf", tmp_path)

0 commit comments

Comments
 (0)