Skip to content

Commit dc9cb8c

Browse files
committed
Added additional checks and metadata
1 parent 638ce03 commit dc9cb8c

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

build_ebook_v2.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,36 @@ def build_pdf_book(self, language: str, markdown_filepath: pathlib.Path) -> None
7777
try:
7878
subprocess.check_output(
7979
[
80-
'pandoc',
80+
"xelatex",
81+
"--version",
82+
]
83+
)
84+
except subprocess.CalledProcessError as error:
85+
log.error(error)
86+
self.log.warning("Please, install 'xelatex'!")
87+
88+
raise RuntimeError from error
89+
90+
try:
91+
subprocess.check_output(
92+
[
93+
"pandoc",
8194
markdown_filepath.as_posix(),
82-
'-V', 'documentclass=report',
83-
'-t', 'latex',
84-
'-s',
85-
'--toc',
86-
'--listings',
87-
'-H', './ebook/listings-setup.tex',
88-
'-o', './ebook/Vulkan Tutorial ' + language + '.pdf',
89-
'--pdf-engine=xelatex'
95+
"-V", "documentclass=report",
96+
"-t", "latex",
97+
"-s",
98+
"--toc",
99+
"--listings",
100+
"-H", "./ebook/listings-setup.tex",
101+
"-o", f"./ebook/Vulkan Tutorial {language}.pdf",
102+
"--pdf-engine=xelatex",
103+
"--metadata=title:Vulkan Tutorial"
90104
]
91105
)
92106
except subprocess.CalledProcessError as error:
93107
log.error(error)
94108

95-
raise error
109+
raise RuntimeError from error
96110

97111
def build_epub_book(self, language: str, markdown_filepath: pathlib.Path) -> None:
98112
"""Buids a epub file"""
@@ -102,17 +116,18 @@ def build_epub_book(self, language: str, markdown_filepath: pathlib.Path) -> Non
102116
try:
103117
subprocess.check_output(
104118
[
105-
'pandoc',
119+
"pandoc",
106120
markdown_filepath.as_posix(),
107-
'--toc',
108-
'-o', './ebook/Vulkan Tutorial ' + language + '.epub',
109-
'--epub-cover-image=ebook/cover.png'
121+
"--toc",
122+
"-o", f"./ebook/Vulkan Tutorial {language}.epub",
123+
"--epub-cover-image=ebook/cover.png",
124+
"--metadata=title:Vulkan Tutorial"
110125
]
111126
)
112127
except subprocess.CalledProcessError as error:
113128
log.error(error)
114129

115-
raise error
130+
raise RuntimeError from error
116131

117132
def convert_svg_to_png(self, images_folder: str) -> list[pathlib.Path]:
118133
"""Converts *.svg images to *.png using Inkscape"""
@@ -128,8 +143,8 @@ def convert_svg_to_png(self, images_folder: str) -> list[pathlib.Path]:
128143
try:
129144
subprocess.check_output(
130145
[
131-
'inkscape',
132-
'--export-filename=' + new_path.as_posix(),
146+
"inkscape",
147+
f"--export-filename={new_path.as_posix()}",
133148
entry.as_posix()
134149
],
135150
stderr=subprocess.STDOUT
@@ -204,7 +219,11 @@ def _collect_markdown_files_from_source(
204219
if entry.is_dir() is True:
205220
log.info(f"Processing directory: {entry}")
206221

207-
self._collect_markdown_files_from_source(entry, (current_depth + 1), prefix, markdown_files)
222+
self._collect_markdown_files_from_source(
223+
entry,
224+
(current_depth + 1),
225+
prefix, markdown_files
226+
)
208227
else:
209228
log.info(f"Processing: {entry}")
210229

@@ -229,6 +248,8 @@ def _collect_markdown_files_from_source(
229248
log = VTLogger(f"{out_dir.as_posix()}/build_ebook.log")
230249
eBookBuilder = VTEBookBuilder(log)
231250

251+
log.info("--- Exporting ebooks:")
252+
232253
generated_pngs = eBookBuilder.convert_svg_to_png("./images")
233254

234255
LANGUAGES = [ "en", "fr" ]
@@ -237,13 +258,18 @@ def _collect_markdown_files_from_source(
237258
for lang in LANGUAGES:
238259
eBookBuilder.generate_joined_markdown(f"./{lang}", OUTPUT_MARKDOWN_FILEPATH)
239260

240-
eBookBuilder.build_epub_book(lang, OUTPUT_MARKDOWN_FILEPATH)
241-
eBookBuilder.build_pdf_book(lang, OUTPUT_MARKDOWN_FILEPATH)
261+
try:
262+
eBookBuilder.build_epub_book(lang, OUTPUT_MARKDOWN_FILEPATH)
263+
eBookBuilder.build_pdf_book(lang, OUTPUT_MARKDOWN_FILEPATH)
264+
except RuntimeError as runtimeError:
265+
log.error("Termininating...")
242266

243267
# Clean up
244268
if OUTPUT_MARKDOWN_FILEPATH.exists():
245269
OUTPUT_MARKDOWN_FILEPATH.unlink()
246270

271+
log.info("Cleaning up...")
272+
247273
# Clean up temporary files
248274
for png_path in generated_pngs:
249275
try:
@@ -254,3 +280,5 @@ def _collect_markdown_files_from_source(
254280
# Comment to view log
255281
if out_dir.exists():
256282
shutil.rmtree(out_dir)
283+
284+
log.info("---- DONE!")

0 commit comments

Comments
 (0)