Skip to content

Commit 527216e

Browse files
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.
1 parent 02e283d commit 527216e

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

build_ebook.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from subprocess import CalledProcessError
1212
from re import Match
1313
import shutil
14+
import argparse
15+
import sys
16+
from make_parser import make_parser
1417

1518
logging.basicConfig(
1619
format="%(asctime)s %(levelname)-8s %(message)s",
@@ -148,7 +151,7 @@ def compile_full_markdown(
148151
return markdown_file
149152

150153

151-
def build_pdf(markdown_file: Path, pdf_file: Path) -> Path:
154+
def build_pdf(markdown_file: Path, pdf_file: Path, args: argparse.Namespace) -> Path:
152155
"""Build combined Markdown file into a PDF."""
153156

154157
try:
@@ -157,12 +160,21 @@ def build_pdf(markdown_file: Path, pdf_file: Path) -> Path:
157160
raise RuntimeError(f"failed to build {pdf_file}: xelatex not installed")
158161

159162
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+
160167
subprocess.check_output(
161168
[
162169
"pandoc",
163170
markdown_file.as_posix(),
164171
"-V",
165-
"documentclass=report",
172+
"documentclass=report"
173+
]
174+
+
175+
pandoc_args
176+
+
177+
[
166178
"-t",
167179
"latex",
168180
"-s",
@@ -202,8 +214,10 @@ def build_epub(markdown_file: Path, epub_file: Path) -> Path:
202214

203215
return epub_file
204216

205-
206217
def main() -> None:
218+
parser = make_parser()
219+
args = parser.parse_args(sys.argv[1:])
220+
207221
"""Build ebooks."""
208222
with TemporaryDirectory() as raw_out_dir:
209223
out_dir = Path(raw_out_dir)
@@ -223,11 +237,11 @@ def main() -> None:
223237
)
224238

225239
logging.info(f"{lang}: building pdf...")
226-
pdf_file = build_pdf(markdown_file, out_dir / f"{lang}.pdf")
240+
pdf_file = build_pdf(markdown_file, out_dir / f"{lang}.pdf", args)
227241

228242
logging.info(f"{lang}: building epub...")
229243
epub_file = build_epub(markdown_file, out_dir / f"{lang}.epub")
230-
244+
231245
shutil.copy(pdf_file, f"ebook/vulkan_tutorial_{lang}.pdf")
232246
shutil.copy(epub_file, f"ebook/vulkan_tutorial_{lang}.epub")
233247

make_parser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import argparse
2+
3+
def make_parser() -> argparse.ArgumentParser:
4+
parser = argparse.ArgumentParser(
5+
description="Build the pdf and epub files of the Vulkan Tutorial."
6+
)
7+
8+
parser.add_argument(
9+
"--geometry:left",
10+
type=str,
11+
required=False,
12+
default="",
13+
help="Specify left margin space as a string. Example: 2cm.",
14+
)
15+
parser.add_argument(
16+
"--geometry:right",
17+
type=str,
18+
required=False,
19+
default="",
20+
help="Specify right margin space as a string. Example: 2cm.",
21+
)
22+
parser.add_argument(
23+
"--geometry:top",
24+
type=str,
25+
required=False,
26+
default="",
27+
help="Specify top margin space as a string. Example: 2cm.",
28+
)
29+
parser.add_argument(
30+
"--geometry:bottom",
31+
type=str,
32+
required=False,
33+
default="",
34+
help="Specify bottom margin space as a string. Example: 2cm.",
35+
)
36+
37+
return parser

0 commit comments

Comments
 (0)