Skip to content

Commit bd7052e

Browse files
committed
Clean up
1 parent 154c034 commit bd7052e

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

build_ebook_v2.py

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@
88

99
class VTLogger:
1010
"""A logger"""
11-
def __init__(self, filename:str, log_to_file:bool=True):
11+
def __init__(self, filename:str, log_to_file:bool=True) -> None:
1212
if log_to_file is True:
1313
self.log_file = open(filename, "w", encoding="utf-8")
1414

15-
def __del__(self):
15+
def __del__(self) -> None:
1616
if self.log_file is not None:
1717
self.log_file.close()
1818

19-
def detail(self, message: str):
19+
def detail(self, message: str) -> None:
2020
"""Logs a detail message without printing it."""
2121
self._log(message, True)
2222

23-
def error(self, message: str):
23+
def error(self, message: str) -> None:
2424
"""Logs an error."""
2525
message = f"Error: {message}"
2626
self._log(message)
2727

28-
def info(self, message: str):
28+
def info(self, message: str) -> None:
2929
"""Logs an info message."""
3030
print(message)
3131
self._log(f"Info: {message}", True)
3232

33-
def warning(self, message: str):
33+
def warning(self, message: str) -> None:
3434
"""Logs an warning."""
3535
message = f"Warning: {message}"
3636
self._log(message)
3737

38-
def _log(self, message: str, no_print:bool=False):
38+
def _log(self, message: str, no_print:bool=False) -> None:
3939
if no_print is False:
4040
print(message)
4141

@@ -51,13 +51,13 @@ def __init__(self, content: str, depth: int, prefix: str, title: str) -> None:
5151
self.prefix: str = prefix
5252
self.title: str = title
5353

54-
def __repr__(self):
54+
def __repr__(self) -> str:
5555
return (
5656
f"<VTEMarkdownFile depth: {self.depth}, prefix: '{self.prefix}',"
5757
f" title: '{self.title}', content: '{self.content}'>"
5858
)
5959

60-
def __str__(self):
60+
def __str__(self) -> str:
6161
return (
6262
f"<VTEMarkdownFile depth: {self.depth}, prefix: '{self.prefix}',"
6363
f" title: '{self.title}', content: '{self.content}'>"
@@ -66,43 +66,53 @@ def __str__(self):
6666
class VTEBookBuilder:
6767
"""A 'Markdown' to 'epub' and 'pdf' converter."""
6868

69-
def __init__(self, logger: VTLogger):
69+
def __init__(self, logger: VTLogger) -> None:
7070
self.log = logger
7171

72-
def build_pdf_book(self, language: str):
72+
def build_pdf_book(self, language: str, markdown_filepath: pathlib.Path) -> None:
7373
"""Builds a pdf file"""
7474

7575
self.log.info("Building 'pdf'...")
7676

77-
subprocess.check_output(
78-
[
79-
'pandoc',
80-
'ebook.md',
81-
'-V', 'documentclass=report',
82-
'-t', 'latex',
83-
'-s',
84-
'--toc',
85-
'--listings',
86-
'-H', 'ebook/listings-setup.tex',
87-
'-o', 'ebook/Vulkan Tutorial ' + language + '.pdf',
88-
'--pdf-engine=xelatex'
89-
]
90-
)
91-
92-
def build_epub_book(self, language: str):
77+
try:
78+
subprocess.check_output(
79+
[
80+
'pandoc',
81+
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'
90+
]
91+
)
92+
except subprocess.CalledProcessError as error:
93+
log.error(error)
94+
95+
raise error
96+
97+
def build_epub_book(self, language: str, markdown_filepath: pathlib.Path) -> None:
9398
"""Buids a epub file"""
9499

95100
self.log.info("Building 'epub'...")
96101

97-
subprocess.check_output(
98-
[
99-
'pandoc',
100-
'ebook.md',
101-
'--toc',
102-
'-o', 'ebook/Vulkan Tutorial ' + language + '.epub',
103-
'--epub-cover-image=ebook/cover.png'
104-
]
105-
)
102+
try:
103+
subprocess.check_output(
104+
[
105+
'pandoc',
106+
markdown_filepath.as_posix(),
107+
'--toc',
108+
'-o', './ebook/Vulkan Tutorial ' + language + '.epub',
109+
'--epub-cover-image=ebook/cover.png'
110+
]
111+
)
112+
except subprocess.CalledProcessError as error:
113+
log.error(error)
114+
115+
raise error
106116

107117
def convert_svg_to_png(self, images_folder: str) -> list[pathlib.Path]:
108118
"""Converts *.svg images to *.png using Inkscape"""
@@ -132,15 +142,15 @@ def convert_svg_to_png(self, images_folder: str) -> list[pathlib.Path]:
132142

133143
return pngs
134144

135-
def generate_markdown_from_sources(self, language: str, output_filename: pathlib.Path):
145+
def generate_joined_markdown(self, language: str, output_filename: pathlib.Path) -> None:
136146
"""Processes the markdown sources and produces a joined file."""
137147

138148
self.log.info(
139149
f"Generating a temporary 'Markdown' file: '{output_filename}'" \
140150
f" for language '{language}'..."
141151
)
142152

143-
md_files = self._process_files_in_directory(language)
153+
md_files = self._collect_markdown_files_from_source(language)
144154
md_files = sorted(md_files, key=lambda file: file.prefix)
145155

146156
temp_markdown: str = str()
@@ -174,7 +184,7 @@ def repl(match):
174184
with open(output_filename, "w", encoding="utf-8") as file:
175185
file.write(temp_markdown)
176186

177-
def _process_files_in_directory(
187+
def _collect_markdown_files_from_source(
178188
self,
179189
directory_path: pathlib.Path,
180190
current_depth: int=int(0),
@@ -192,7 +202,7 @@ def _process_files_in_directory(
192202
if entry.is_dir() is True:
193203
log.info(f"Processing directory: {entry}")
194204

195-
self._process_files_in_directory(entry, (current_depth + 1), prefix, markdown_files)
205+
self._collect_markdown_files_from_source(entry, (current_depth + 1), prefix, markdown_files)
196206
else:
197207
log.info(f"Processing: {entry}")
198208

@@ -220,19 +230,19 @@ def _process_files_in_directory(
220230
generated_pngs = eBookBuilder.convert_svg_to_png("./images")
221231

222232
LANGUAGES = [ "en", "fr" ]
223-
OUTPUT_FILENAME = pathlib.Path(f"{out_dir.as_posix()}/temp_ebook.md")
233+
OUTPUT_MARKDOWN_FILEPATH = pathlib.Path(f"{out_dir.as_posix()}/temp_ebook.md")
224234

225235
for lang in LANGUAGES:
226236
lang = f"./{lang}"
227237

228-
eBookBuilder.generate_markdown_from_sources(lang, OUTPUT_FILENAME)
238+
eBookBuilder.generate_joined_markdown(lang, OUTPUT_MARKDOWN_FILEPATH)
229239

230-
# eBookBuilder.build_epub_book(lang)
231-
# eBookBuilder.build_pdf_book(lang)
240+
eBookBuilder.build_epub_book(lang, OUTPUT_MARKDOWN_FILEPATH)
241+
eBookBuilder.build_pdf_book(lang, OUTPUT_MARKDOWN_FILEPATH)
232242

233243
# Clean up
234-
if OUTPUT_FILENAME.exists():
235-
OUTPUT_FILENAME.unlink()
244+
if OUTPUT_MARKDOWN_FILEPATH.exists():
245+
OUTPUT_MARKDOWN_FILEPATH.unlink()
236246

237247
# Clean up temporary files
238248
for png_path in generated_pngs:

0 commit comments

Comments
 (0)