md2star.pipeline module
md2star’s conversion engine: Markdown → DOCX / PPTX / PDF.
This module is the pipeline, not the command line. It used to live in
md2star/cli.py next to an argparse front-end, which meant the engine was
reachable only by handing it an argv list that it re-parsed — the GUI
server and the FastAPI surface both built fake command lines for that reason.
Since 3.10.0 the single command line is click (see md2star.cli), the
parsing happens there once, and every caller hands this module a typed
ConvertOptions instead of a string list.
convert() orchestrates the four steps:
Preprocess the input Markdown via
md2star.preprocess_markdown().Resolve the reference template: prefer
template.{docx,pptx}next to the input file, then the legacy.pandoc-reference.{docx,pptx}, then the XDG cache, then (the default since v2.5.0) a one-shot fetch fromderaison.aicached under XDG, and finally the bundled template.--no-remote-templates/--offlineskip the fetch.Invoke pandoc with the bundled Lua filter, metadata defaults, and resolved reference doc. Everything in
pandoc_args— the flags md2star does not know — is appended verbatim, last, so a user can override any choice md2star made.Postprocess (DOCX only), re-inject the
MyTable/MyTableSmalltable styles that Pandoc strips when rewritingword/styles.xml.
The PDF format is implemented as a wrapper: it produces the DOCX first
(so all of md2star’s polish, from mermaid diagrams to table styles to
slide-aware tweaks, is already applied) and then asks headless LibreOffice
(soffice --headless --convert-to pdf) to render that DOCX to PDF. This
guarantees the PDF visually matches the DOCX you would ship.
- class md2star.pipeline.ConvertOptions(input, output=None, author=None, bib=None, bibliography_name=None, lang=None, date=None, lint=False, skip_phase=(), reference_doc=None, offline=False, no_remote_templates=False, allow_remote_images=False, verbose=False, quiet=False, smart_layout=False, template=None, reference_slides=False, no_visual_confirm=False, eyeball_iterations=0, min_pt=None, refresh_catalog=False)[source]
Bases:
objectEverything
convert()needs, already parsed and typed.This replaces the argparse
Namespacethe engine used to receive. The difference is not cosmetic: aNamespaceis whatever the parser that built it happened to set, so the engine read five of these fields throughgetattr(args, name, False)because the pptx-only flags simply did not exist on the docx parser. Three in-tree callers (the CLI, the GUI server, the FastAPI app) each had to know the exact flag spellings to build an argv the engine would then re-parse, and nothing checked that they agreed.A frozen dataclass makes the contract explicit and total: every field is present for every format, with the same default the CLI advertises, and a caller that forgets one gets a
TypeErrorrather than a silent False.- Parameters:
input (str)
output (str | None)
author (str | None)
bib (str | None)
bibliography_name (str | None)
lang (str | None)
date (str | None)
lint (bool)
reference_doc (str | None)
offline (bool)
no_remote_templates (bool)
allow_remote_images (bool)
verbose (bool)
quiet (bool)
smart_layout (bool)
template (str | None)
reference_slides (bool)
no_visual_confirm (bool)
eyeball_iterations (int)
min_pt (float | None)
refresh_catalog (bool)
- output
Destination path.
Nonemeans “next to the input, with the target format’s extension”.- Type:
str or None
- author, lang, date
Pandoc metadata overrides.
dateis passed asdate_overrideso it does not collide with Pandoc’s owndate.- Type:
str or None
- bibliography_name
Heading for the appended references section.
Noneresolves to the localized default for the document’s language.- Type:
str or None
- verbose, quiet
Logging threshold: DEBUG / ERROR, default INFO.
- Type:
- md2star.pipeline.convert(fmt, opts, pandoc_args=())[source]
Run the full Markdown → .docx/.pptx/.pdf pipeline. Returns the exit code.
- Parameters:
fmt ({"docx", "pptx", "pdf"}) – Target format.
pdfrenders a DOCX first and hands it to soffice.opts (ConvertOptions) – The parsed md2star options. See the dataclass for each field.
pandoc_args (sequence of str, optional) – Flags md2star does not know, forwarded to Pandoc verbatim and last so they override anything md2star chose. This is the feature the whole tool is built around —
md2docx report.md --tochas to reach Pandoc as--toc— so it is a plain list here rather than something parsed.
- Returns:
0on success; a non-zero code (Pandoc’s, or soffice’s) otherwise.- Return type: