Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown not escaping MD special characters #1215

Open
exiva opened this issue Jun 25, 2024 · 2 comments
Open

Markdown not escaping MD special characters #1215

exiva opened this issue Jun 25, 2024 · 2 comments

Comments

@exiva
Copy link

exiva commented Jun 25, 2024

Error details
When using markdown in fpdf2 it should respect escaped characters like underscores and asterisks. It does not currently.

Minimal code
Please include some minimal Python code reproducing your issue:

from fpdf import FPDF

pdf = FPDF(format=(101.6, 152.4))
pdf.set_margin(2)
pdf.add_page()
pdf.set_font("Times", size=16)

txt = "**This** is a markdown test. Theres\__double underscores"
pdf.multi_cell(w=50, text=txt, align='L', new_x='RIGHT',
                new_y='TOP', border=0, markdown=True,)

pdf.output("md.pdf")

Should output "This is a markdown test. Theres__double underscores" but outputs
Screenshot 2024-06-25 at 10 04 24 AM

Environment
Please provide the following information:

  • Operating System: macOS 14.5 Apple Silicon M2 Mac Mini
  • Python version: 3.11.9
  • fpdf2 version used: 2.7.9
@exiva exiva added the bug label Jun 25, 2024
@gmischler
Copy link
Collaborator

Thanks for reporting this, and welcome to fpdf2, @exiva !

Well, the documentation doesn't promise that our markup parser would honor backslash escapes, so technically this isn't a bug... 😉

it is a perfectly valid feature request, though!
Any chance you might want to submit a PR?

@andersonhc
Copy link
Collaborator

andersonhc commented Jun 25, 2024

If you want a head start, markdown markers are handled on fpdf _parse_chars() function.

fpdf2/fpdf/fpdf.py

Lines 3500 to 3515 in d574b07

if markdown:
if (
is_marker
and (not txt_frag or txt_frag[-1] != half_marker)
and (len(text) < 3 or text[2] != half_marker)
):
if txt_frag:
yield frag()
if text[:2] == self.MARKDOWN_BOLD_MARKER:
in_bold = not in_bold
if text[:2] == self.MARKDOWN_ITALICS_MARKER:
in_italics = not in_italics
if text[:2] == self.MARKDOWN_UNDERLINE_MARKER:
in_underline = not in_underline
text = text[2:]
continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants