Optionally specify margin size #396 (#411)

Whoever wants to shrink the margins, they can now do so via the new
optional command line arguments

--geometry:left
--geometry:right
--geometry:top
--geometry:bottom

which are default options in pandoc (see pandoc's documentation
[here](https://pandoc.org/demo/example33/6.2-variables.html)).

Their default value is ''. These options are not passed to pandoc when
their value is ''.

The argument parser added can be extended easily with other pandoc's
argument options. If the new options' default value is '' then they will
not be passed to pandoc.
This commit is contained in:
Lluís Alemany Puig 2025-08-30 15:04:00 +02:00 committed by GitHub
parent 02e283d548
commit 527216eb95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 5 deletions

View File

@ -11,6 +11,9 @@ from dataclasses import dataclass
from subprocess import CalledProcessError
from re import Match
import shutil
import argparse
import sys
from make_parser import make_parser
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
@ -148,7 +151,7 @@ def compile_full_markdown(
return markdown_file
def build_pdf(markdown_file: Path, pdf_file: Path) -> Path:
def build_pdf(markdown_file: Path, pdf_file: Path, args: argparse.Namespace) -> Path:
"""Build combined Markdown file into a PDF."""
try:
@ -157,12 +160,21 @@ def build_pdf(markdown_file: Path, pdf_file: Path) -> Path:
raise RuntimeError(f"failed to build {pdf_file}: xelatex not installed")
try:
keys_values = [(arg, getattr(args, arg)) for arg in vars(args)]
opts = [f"{key}={val}" for key, val in keys_values if val != ""]
pandoc_args = [x for i in opts for x in ("-V", i)]
subprocess.check_output(
[
"pandoc",
markdown_file.as_posix(),
"-V",
"documentclass=report",
"documentclass=report"
]
+
pandoc_args
+
[
"-t",
"latex",
"-s",
@ -202,8 +214,10 @@ def build_epub(markdown_file: Path, epub_file: Path) -> Path:
return epub_file
def main() -> None:
parser = make_parser()
args = parser.parse_args(sys.argv[1:])
"""Build ebooks."""
with TemporaryDirectory() as raw_out_dir:
out_dir = Path(raw_out_dir)
@ -223,11 +237,11 @@ def main() -> None:
)
logging.info(f"{lang}: building pdf...")
pdf_file = build_pdf(markdown_file, out_dir / f"{lang}.pdf")
pdf_file = build_pdf(markdown_file, out_dir / f"{lang}.pdf", args)
logging.info(f"{lang}: building epub...")
epub_file = build_epub(markdown_file, out_dir / f"{lang}.epub")
shutil.copy(pdf_file, f"ebook/vulkan_tutorial_{lang}.pdf")
shutil.copy(epub_file, f"ebook/vulkan_tutorial_{lang}.epub")

37
make_parser.py Normal file
View File

@ -0,0 +1,37 @@
import argparse
def make_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Build the pdf and epub files of the Vulkan Tutorial."
)
parser.add_argument(
"--geometry:left",
type=str,
required=False,
default="",
help="Specify left margin space as a string. Example: 2cm.",
)
parser.add_argument(
"--geometry:right",
type=str,
required=False,
default="",
help="Specify right margin space as a string. Example: 2cm.",
)
parser.add_argument(
"--geometry:top",
type=str,
required=False,
default="",
help="Specify top margin space as a string. Example: 2cm.",
)
parser.add_argument(
"--geometry:bottom",
type=str,
required=False,
default="",
help="Specify bottom margin space as a string. Example: 2cm.",
)
return parser