Build combined Markdown file into a PDF.
(markdown_file: Path, pdf_file: Path, args: argparse.Namespace)
| 152 | |
| 153 | |
| 154 | def build_pdf(markdown_file: Path, pdf_file: Path, args: argparse.Namespace) -> Path: |
| 155 | """Build combined Markdown file into a PDF.""" |
| 156 | |
| 157 | try: |
| 158 | subprocess.check_output(["xelatex", "--version"]) |
| 159 | except FileNotFoundError: |
| 160 | raise RuntimeError(f"failed to build {pdf_file}: xelatex not installed") |
| 161 | |
| 162 | try: |
| 163 | keys_values = [(arg, getattr(args, arg)) for arg in vars(args)] |
| 164 | opts = [f"{key}={val}" for key, val in keys_values if val != ""] |
| 165 | pandoc_args = [x for i in opts for x in ("-V", i)] |
| 166 | |
| 167 | subprocess.check_output( |
| 168 | [ |
| 169 | "pandoc", |
| 170 | markdown_file.as_posix(), |
| 171 | "-V", |
| 172 | "documentclass=report" |
| 173 | ] |
| 174 | + |
| 175 | pandoc_args |
| 176 | + |
| 177 | [ |
| 178 | "-t", |
| 179 | "latex", |
| 180 | "-s", |
| 181 | "--toc", |
| 182 | "--listings", |
| 183 | "-H", |
| 184 | "ebook/listings-setup.tex", |
| 185 | "-o", |
| 186 | pdf_file.as_posix(), |
| 187 | "--pdf-engine=xelatex", |
| 188 | ] |
| 189 | ) |
| 190 | except CalledProcessError as e: |
| 191 | raise RuntimeError( |
| 192 | f"failed to build {pdf_file}: pandoc failed: {e.output.decode()}" |
| 193 | ) |
| 194 | |
| 195 | return pdf_file |
| 196 | |
| 197 | |
| 198 | def build_epub(markdown_file: Path, epub_file: Path) -> Path: |