Skip to content

Commit

Permalink
Allow to configure Markdown extras using the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaupetit committed Jan 22, 2024
1 parent ea561d8 commit 28c7860
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 3 additions & 1 deletion md2pdf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
@click.argument("md", type=click.Path(exists=True))
@click.argument("pdf", type=click.Path())
@click.option("--css", "css", type=click.Path(exists=True))
@click.option("--extras", "-e", "extras", multiple=True, type=str)
@click.version_option(version=version("md2pdf"))
def cli(md, pdf, css):
def cli(md, pdf, css, extras):
"""md2pdf command line tool."""
md2pdf(
pdf,
md=md,
css=css,
base_url=Path.cwd(),
extras=extras
)
3 changes: 3 additions & 0 deletions md2pdf/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Configuration for md2pdf."""

MARKDOWN_EXTRAS: list = ["cuddled-lists", "tables", "footnotes"]
9 changes: 7 additions & 2 deletions md2pdf/core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""md2pdf core module."""
from pathlib import Path
from typing import Optional
from typing import List, Optional

from markdown2 import markdown, markdown_path
from weasyprint import CSS, HTML

from .conf import MARKDOWN_EXTRAS
from .exceptions import ValidationError


Expand All @@ -14,6 +15,7 @@ def md2pdf(
md: Optional[Path] = None,
css: Optional[Path] = None,
base_url: Optional[Path] = None,
extras: List[str] = MARKDOWN_EXTRAS,
):
"""Converts input markdown to styled HTML and renders it to a PDF file.
Expand All @@ -23,6 +25,7 @@ def md2pdf(
raw: input markdown raw string content.
css: input styles path (CSS).
base_url: absolute base path for markdown linked content (as images).
extras: markdown extras to activate
Returns:
None
Expand All @@ -32,7 +35,7 @@ def md2pdf(
"""
# Convert markdown to html
raw_html: str = ""
extras: list = ["cuddled-lists", "tables", "footnotes"]

if md:
raw_html = markdown_path(md, extras=extras)
elif raw:
Expand All @@ -42,6 +45,8 @@ def md2pdf(
raise ValidationError("Input markdown seems empty")

# Weasyprint HTML object
if base_url is None:
base_url = Path.cwd()
html: HTML = HTML(string=raw_html, base_url=str(base_url))

# Get styles
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ python = "^3.8"
weasyprint = "^60.2"
markdown2 = "^2.4.12"
click = "^8.1.7"
pygments = "^2.17.2"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.4"
Expand Down Expand Up @@ -77,6 +78,9 @@ select = [
"T20", # flake8-print
"W", # pycodestyle warning
]
ignore = [
"PLR0913", # Too many arguments in function definition
]

[tool.ruff.pydocstyle]
# Use Google-style docstrings.
Expand Down

0 comments on commit 28c7860

Please sign in to comment.