py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
1.05k stars 241 forks source link

Escape character escaping multiple consecutive markdown characters #1236

Open JoseMezaMendieta opened 1 month ago

JoseMezaMendieta commented 1 month ago

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:

gmischler commented 1 month ago

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 commented 1 month ago

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

JoseMezaMendieta commented 1 month ago

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

Lucas-C commented 3 weeks ago

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.