Fix markdown character escaping bug (issue #1236)#1741
Fix markdown character escaping bug (issue #1236)#1741amidou-naba wants to merge 2 commits intopy-pdf:masterfrom
Conversation
|
Thank you for opening this PR @amidou-naba |
|
Hello, Thank you for the review. |
andersonhc
left a comment
There was a problem hiding this comment.
Thank you for the PR, it looks really good!
I am adding some comments on what needs to be fixed before we can merge it.
| text = text[2:] | ||
| continue | ||
|
|
||
|
|
There was a problem hiding this comment.
You are removing characters from the input text even when markdown=False. This portion of code should be inside an if markdown: block
| frags = tuple(FPDF()._parse_chars("****BOLD**", True)) | ||
| expected = (Fragment("****BOLD", GSTATE, k=PDF.k),) | ||
| expected = (Fragment("**BOLD", GSTATE_B, k=PDF.k),) | ||
| frags = merge_fragments(tuple(FPDF()._parse_chars("\\****BOLD**\\**", True))) |
There was a problem hiding this comment.
I guess you missing an assert after line 200
| if self.MARKDOWN_ESCAPE_CHARACTER == tlt[0] and tlt[1:] in [ | ||
| "**", | ||
| "__", | ||
| "~~", | ||
| "--", | ||
| ]: |
There was a problem hiding this comment.
| if self.MARKDOWN_ESCAPE_CHARACTER == tlt[0] and tlt[1:] in [ | |
| "**", | |
| "__", | |
| "~~", | |
| "--", | |
| ]: | |
| if tlt.startswith(self.MARKDOWN_ESCAPE_CHARACTER) and tlt[1:] in ( | |
| self.MARKDOWN_BOLD_MARKER, | |
| self.MARKDOWN_ITALICS_MARKER, | |
| self.MARKDOWN_STRIKETHROUGH_MARKER, | |
| self.MARKDOWN_UNDERLINE_MARKER, | |
| ): |
This change would make it easier to read - less 'magic values' across the code.
|
Hello,
Ok no problem
thank you
De: "Anderson Herzogenrath da Costa" ***@***.***>
À: "py-pdf/fpdf2" ***@***.***>
Cc: "Amidou Nabassaga" ***@***.***>, "Mention" ***@***.***>
Envoyé: Jeudi 5 Février 2026 14:31:39
Objet: Re: [py-pdf/fpdf2] Fix markdown character escaping bug (issue #1236) (PR #1741)
@andersonhc requested changes on this pull request.
Thank you for the PR, it looks really good!
I am adding some comments on what needs to be fixed before we can merge it.
In [ #1741 (comment) | fpdf/fpdf.py ] :
+ txt_frag.append(text[1])
+ txt_frag.append(text[2])
+ yield frag()
+ text = text[3:]
+ continue
+
+ if self.MARKDOWN_ESCAPE_CHARACTER == tlt[0:1] and \
+ self.MARKDOWN_ESCAPE_CHARACTER == tlt[1:2]:
+ # double-escape, juste produce it
+ txt_frag.append(text[0])
+ txt_frag.append(text[1])
+ yield frag()
+ text = text[2:]
+ continue
+
+
You are removing characters from the input text even when markdown=False . This portion of code should be inside an if markdown: block
In [ #1741 (comment) | test/text/test_markdown_parse.py ] :
expected = (Fragment("----------", GSTATE, k=PDF.k),)
assert frags == expected
frags = tuple(FPDF()._parse_chars("****BOLD**", True))
- expected = (Fragment("****BOLD", GSTATE, k=PDF.k),)
+ expected = (Fragment("**BOLD", GSTATE_B, k=PDF.k),)
+ frags = merge_fragments(tuple(FPDF()._parse_chars("\\****BOLD**\\**", True)))
I guess you missing an assert after line 200
In [ #1741 (comment) | fpdf/fpdf.py ] :
+ if self.MARKDOWN_ESCAPE_CHARACTER == tlt[0] and tlt[1:] in [
+ "**",
+ "__",
+ "~~",
+ "--",
+ ]:
⬇️ Suggested change
- if self.MARKDOWN_ESCAPE_CHARACTER == tlt[0] and tlt[1:] in [
- "**",
- "__",
- "~~",
- "--",
- ]:
+ if tlt.startswith(self.MARKDOWN_ESCAPE_CHARACTER) and tlt[1:] in (
+ self.MARKDOWN_BOLD_MARKER,
+ self.MARKDOWN_ITALICS_MARKER,
+ self.MARKDOWN_STRIKETHROUGH_MARKER,
+ self.MARKDOWN_UNDERLINE_MARKER,
+ ):
This change would make it easier to read - less 'magic values' across the code.
—
Reply to this email directly, [ #1741 (review) | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/BX2ZEYH54AUZQJNMT2KJPPL4KNA3XAVCNFSM6AAAAACT6P356KVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTONJWHE4TKMZZGU | unsubscribe ] .
You are receiving this because you were mentioned. Message ID: <py-pdf/fpdf2/pull/1741/review/3756995395 @ github . com>
|
e.g. Fixes #0
Checklist:
A unit test is covering the code added / modified by this PR
In case of a new feature, docstrings have been added, with also some documentation in the
docs/folderA mention of the change is present in
CHANGELOG.mdThis PR is ready to be merged
By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.