fpdf2 icon indicating copy to clipboard operation
fpdf2 copied to clipboard

Escape character escaping multiple consecutive markdown characters

Open JoseMezaMendieta opened this issue 1 year ago • 4 comments

Error details The documentation says that the escape character \ behaves in the same way that it generally does for Python. To my understanding, in Python the escape character only applies to 1 single instance immediately after the escape character. However, the escape character seems to be escaping multiple instances of special characters immediately after it. In the following code example, I am trying to escape the first instance of double asterisks, then enable bold, print the word in bold, disable bold, then print double asterisks again. All this should be done with no spaces in between, however, the output is not what was expected.

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

from fpdf import FPDF

pdf = FPDF(format=(30, 10))
pdf.set_margin(2)
pdf.add_page()
pdf.set_font("Times", size=12)

txt = "\\****Lorem**\\**"
pdf.cell(w=50, text=txt, align='L', new_x='RIGHT', new_y='TOP', border=0, markdown=True,)

pdf.output("md.pdf")

Expected output: image

Actual output: image

Environment Please provide the following information:

  • Operating System: Windows 10 Pro
  • Python version: Python 3.12.4
  • fpdf2 version used: fpdf2 @ git+https://github.com/py-pdf/fpdf2.git@e6a369d76e910dfb557478ef6e23d0b9d8392367

JoseMezaMendieta avatar Jul 30 '24 07:07 JoseMezaMendieta

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

The equivalence to the escape backslash in Python is obviously not identical, as in our case it is supposed to always escape exactly two characters at a time instead of one. But of course it should not escape any longer sequence than that. Pinging the implementer @david-fed to see if he has a good idea how to fix this.

gmischler avatar Jul 30 '24 13:07 gmischler

Or could we interest you in submitting a PR, @JoseMezaMendieta ?

gmischler avatar Aug 01 '24 06:08 gmischler

I'd be interested in taking a crack at it! It'd be my first contribution if I can figure it out.

JoseMezaMendieta avatar Aug 01 '24 09:08 JoseMezaMendieta

The equivalence to the escape backslash in Python is obviously not identical, as in our case it is supposed to always escape exactly two characters at a time instead of one. But of course it should not escape any longer sequence than that. Pinging the implementer @david-fed to see if he has a good idea how to fix this.

I just reported a similar problem in https://github.com/py-pdf/fpdf2/issues/1215#issuecomment-2296360896 😊

I did not see this issue from @JoseMezaMendieta beforehand (thank you for opening it 👍)

I think we should conform to the CommonMark standard regarding character escaping rules.

Lucas-C avatar Aug 19 '24 11:08 Lucas-C