jmaupetit / md2pdf

Markdown to PDF conversion tool
MIT License
268 stars 41 forks source link

Are Markdown Tables supported? #36

Closed johnthagen closed 3 years ago

johnthagen commented 3 years ago

Are Extended syntax tables supported? https://www.markdownguide.org/extended-syntax/#tables

Code

if __name__ == "__main__":
   table = """
| Syntax      | Description |
| ----------- | ----------- |
| Header      | Title       |
| Paragraph   | Text        |
"""

    output_path = Path("out.pdf")
    md2pdf(
        output_path,
        md_content=f"This is a **test**\n\n{table}",
        css_file_path=None,
        base_url=None
    )

Output

Expecting to see a table here:

Screen Shot 2021-01-20 at 12 56 29 PM

Version

Python 3.9.1

$ pip list
Package      Version
------------ -------
cairocffi    1.2.0
CairoSVG     2.5.1
cffi         1.14.4
cssselect2   0.4.1
defusedxml   0.6.0
docopt       0.6.2
html5lib     1.1
markdown2    2.3.10
md2pdf       0.4
Pillow       8.1.0
pip          20.3.3
prettytable  2.0.0
pycparser    2.20
Pyphen       0.10.0
setuptools   49.6.0
six          1.15.0
tinycss2     1.1.0
wcwidth      0.2.5
WeasyPrint   52.2
webencodings 0.5.1
wheel        0.35.1
johnthagen commented 3 years ago

Looking closer, it looks like the best thing to do would be to expose the extras attribute out to the user. markdown2 supports this extra:

* tables: Tables using the same format as GFM
  <https://help.github.com/articles/github-flavored-markdown#tables> and
  PHP-Markdown Extra <https://michelf.ca/projects/php-markdown/extra/#table>.

Exposing this to the user would allow them to enable whichever extras they need.

Like so:

def md2pdf(pdf_file_path, md_content=None, md_file_path=None,
           css_file_path=None, base_url=None, extras=None):
    """
    Converts input markdown to styled HTML and renders it to a PDF file.

    Args:
        pdf_file_path: output PDF file path.
        md_content: input markdown raw string content.
        md_file_path: input markdown file path.
        css_file_path: input styles path (CSS).
        base_url: absolute base path for markdown linked content (as images).

    Returns:
        None

    Raises:
        ValidationError: if md_content and md_file_path are empty.
    """
    if extras is None:
        extras = ["cuddled-lists"]
    ...
johnthagen commented 3 years ago

I can confirm that setting extras=["cuddled-lists", "tables"] fixes this issue.

johnthagen commented 3 years ago

Related to #10

jmaupetit commented 3 years ago

Great! Can you propose a PR?

johnthagen commented 3 years ago

@jmaupetit Looking at the master branch more closely, this actually is supported

https://github.com/jmaupetit/md2pdf/blob/b73deee01ffebc08f6883efd52014f7159f8ceee/md2pdf/core.py#L31

But there has not been a released on PyPI in 4 years:

https://pypi.org/project/md2pdf/

Could a new release from master be pushed to PyPI? That would close this issue.

jmaupetit commented 3 years ago

This is now fixed! https://pypi.org/project/md2pdf/0.5/