fix: preserve markdown tags in multi_cell dry_run output#1843
fix: preserve markdown tags in multi_cell dry_run output#1843zekiyemeral wants to merge 2 commits intopy-pdf:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to make FPDF.multi_cell(..., markdown=True, dry_run=True, output=LINES) return wrapped lines that preserve the original markdown markers, addressing issue #1840 where markers were lost in dry-run output.
Changes:
- Updates
multi_cell()to rebuild returnedLINESby re-inserting markdown markers based on fragment styling. - Adds a standalone
deneme.pyscript to manually demonstrate/verify the behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
fpdf/fpdf.py |
Reconstructs output=LINES from TextLine.fragments, attempting to re-add markdown markers based on fragment emphasis. |
deneme.py |
Adds a manual test script intended to demonstrate that bold/italic markers are preserved in dry_run line output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if "B" in frag.font_style and "I" in frag.font_style: | ||
| frag_text = f"***{frag_text}***" | ||
| elif "B" in frag.font_style: | ||
| frag_text = f"**{frag_text}**" | ||
| elif "I" in frag.font_style: | ||
| frag_text = f"_{frag_text}_" |
| for frag in text_line.fragments: | ||
| characters.extend(frag.characters) | ||
| output_lines.append("".join(characters)) | ||
| frag_text = "".join(frag.characters) | ||
| if markdown: | ||
| if "B" in frag.font_style and "I" in frag.font_style: | ||
| frag_text = f"***{frag_text}***" | ||
| elif "B" in frag.font_style: | ||
| frag_text = f"**{frag_text}**" | ||
| elif "I" in frag.font_style: | ||
| frag_text = f"_{frag_text}_" | ||
| line_content += frag_text | ||
| output_lines.append(line_content) |
| line_content = "" | ||
| for frag in text_line.fragments: | ||
| characters.extend(frag.characters) | ||
| output_lines.append("".join(characters)) | ||
| frag_text = "".join(frag.characters) | ||
| if markdown: | ||
| if "B" in frag.font_style and "I" in frag.font_style: | ||
| frag_text = f"***{frag_text}***" | ||
| elif "B" in frag.font_style: | ||
| frag_text = f"**{frag_text}**" | ||
| elif "I" in frag.font_style: | ||
| frag_text = f"_{frag_text}_" | ||
| line_content += frag_text | ||
| output_lines.append(line_content) |
| if "B" in frag.font_style and "I" in frag.font_style: | ||
| frag_text = f"***{frag_text}***" | ||
| elif "B" in frag.font_style: | ||
| frag_text = f"**{frag_text}**" | ||
| elif "I" in frag.font_style: | ||
| frag_text = f"_{frag_text}_" |
| pdf = FPDF() | ||
| pdf.add_page() | ||
| pdf.set_font("helvetica", size=12) | ||
|
|
||
| # Test metnimiz: İçinde kalın (**) ve italik (_) kelimeler var. | ||
| test_text = "**Kalin** ve _Italik_ metin." | ||
|
|
||
| # Fonksiyonu dry_run=True ve markdown=True ile çağırıyoruz. | ||
| # Çıktı olarak satırları (LINES) istiyoruz. | ||
| result = pdf.multi_cell(w=0, h=10, text=test_text, markdown=True, dry_run=True, output="LINES") | ||
|
|
||
| print("\n--- TEST SONUCU ---") | ||
| print(f"Gelen Çıktı: {result}") | ||
|
|
||
| if "**Kalin**" in result[0] and "_Italik_" in result[0]: | ||
| print("\nBAŞARILI: Markdown etiketleri korundu! 🚀") | ||
| else: | ||
| print("\nHATA: Etiketler hala kayıp. ❌") | ||
| print("-------------------\n") No newline at end of file |
There was a problem hiding this comment.
"I have deleted this manual test script from the PR to keep the repository clean, as suggested. I'll rely on the library's existing test suite for final validation."
…ncluding links and underline
|
"I have addressed all the feedback from the code review. |
Hi, this PR addresses issue #1840.
The problem was that multi_cell() with dry_run=True and markdown=True was returning plain text without the markdown markers when output="LINES" was used.
I modified the fragment processing loop in multi_cell() to check the font style of each fragment. If a fragment is Bold or Italic, I add the ** or _ symbols back to the text. This way, the dry-run output is consistent with the input markdown.
I verified the fix with a test case:
Input: "Kalin ve Italik metin."
Result: ["Kalin ve Italik metin."]
(Before it was returning: ["Kalin ve Italik metin."])
Fixes #1840