11
11
12
12
class VTLogger :
13
13
"""A logger"""
14
+
14
15
def __init__ (self , filename :str , log_to_file :bool = True ) -> None :
15
16
if log_to_file is True :
16
17
self .log_file = open (filename , "w" , encoding = "utf-8" )
@@ -45,10 +46,13 @@ def _log(self, message: str, no_print:bool=False) -> None:
45
46
if self .log_file is not None :
46
47
self .log_file .write (f"{ message } \n " )
47
48
49
+
48
50
class VTEMarkdownFile :
49
51
"""Markdown file."""
50
52
51
- def __init__ (self , content : str , depth : int , prefix : str , title : str ) -> None :
53
+ def __init__ (
54
+ self , content : str , depth : int , prefix : str , title : str
55
+ ) -> None :
52
56
self .content : str = content
53
57
self .depth : str = depth
54
58
self .prefix : str = prefix
@@ -66,13 +70,16 @@ def __str__(self) -> str:
66
70
f" title: '{ self .title } ', content: '{ self .content } '>"
67
71
)
68
72
73
+
69
74
class VTEBookBuilder :
70
75
"""A 'Markdown' to 'epub' and 'pdf' converter."""
71
76
72
77
def __init__ (self , logger : VTLogger ) -> None :
73
78
self .log = logger
74
79
75
- def build_pdf_book (self , language : str , markdown_filepath : pathlib .Path ) -> None :
80
+ def build_pdf_book (
81
+ self , language : str , markdown_filepath : pathlib .Path
82
+ ) -> None :
76
83
"""Builds a pdf file"""
77
84
78
85
self .log .info ("Building 'pdf'..." )
@@ -111,7 +118,9 @@ def build_pdf_book(self, language: str, markdown_filepath: pathlib.Path) -> None
111
118
112
119
raise RuntimeError from error
113
120
114
- def build_epub_book (self , language : str , markdown_filepath : pathlib .Path ) -> None :
121
+ def build_epub_book (
122
+ self , language : str , markdown_filepath : pathlib .Path
123
+ ) -> None :
115
124
"""Buids a epub file"""
116
125
117
126
self .log .info ("Building 'epub'..." )
@@ -156,13 +165,17 @@ def convert_svg_to_png(self, images_folder: str) -> list[pathlib.Path]:
156
165
pngs .append (new_path )
157
166
except FileNotFoundError as error :
158
167
self .log .error (error )
159
- self .log .warning ("Install 'Inkscape' (https://www.inkscape.org)!" )
168
+ self .log .warning (
169
+ "Install 'Inkscape' (https://www.inkscape.org)!"
170
+ )
160
171
161
172
raise RuntimeError from error
162
173
163
174
return pngs
164
175
165
- def generate_joined_markdown (self , language : str , output_filename : pathlib .Path ) -> None :
176
+ def generate_joined_markdown (
177
+ self , language : str , output_filename : pathlib .Path
178
+ ) -> None :
166
179
"""Processes the markdown sources and produces a joined file."""
167
180
168
181
self .log .info (
@@ -202,7 +215,11 @@ def repl_hash(match):
202
215
content = re .sub (r"\.svg" , ".png" , content )
203
216
204
217
# Fix remaining relative links (e.g. code files).
205
- content = re .sub (r"\]\(\/" , "](https://vulkan-tutorial.com/" , content )
218
+ content = re .sub (
219
+ r"\]\(\/" ,
220
+ "](https://vulkan-tutorial.com/" ,
221
+ content
222
+ )
206
223
207
224
# Fix chapter references.
208
225
def repl (match ):
@@ -242,7 +259,9 @@ def _collect_markdown_files_from_source(
242
259
243
260
title = " " .join (title_tokens [1 :])
244
261
245
- markdown_files .append (VTEMarkdownFile ("" , current_depth , prefix , title ))
262
+ markdown_files .append (
263
+ VTEMarkdownFile ("" , current_depth , prefix , title )
264
+ )
246
265
247
266
self ._collect_markdown_files_from_source (
248
267
entry ,
@@ -256,7 +275,9 @@ def _collect_markdown_files_from_source(
256
275
257
276
with open (entry , "r" , encoding = "utf-8" ) as file :
258
277
content = file .read ()
259
- markdown_files .append (VTEMarkdownFile (content , current_depth , prefix , title ))
278
+ markdown_files .append (
279
+ VTEMarkdownFile (content , current_depth , prefix , title )
280
+ )
260
281
261
282
return markdown_files
262
283
@@ -278,10 +299,15 @@ def _collect_markdown_files_from_source(
278
299
generated_pngs = eBookBuilder .convert_svg_to_png ("./images" )
279
300
280
301
LANGUAGES = [ "en" , "fr" ]
281
- OUTPUT_MARKDOWN_FILEPATH = pathlib .Path (f"{ out_dir .as_posix ()} /temp_ebook.md" )
302
+ OUTPUT_MARKDOWN_FILEPATH = pathlib .Path (
303
+ f"{ out_dir .as_posix ()} /temp_ebook.md"
304
+ )
282
305
283
306
for lang in LANGUAGES :
284
- eBookBuilder .generate_joined_markdown (f"./{ lang } " , OUTPUT_MARKDOWN_FILEPATH )
307
+ eBookBuilder .generate_joined_markdown (
308
+ f"./{ lang } " ,
309
+ OUTPUT_MARKDOWN_FILEPATH
310
+ )
285
311
286
312
try :
287
313
eBookBuilder .build_epub_book (lang , OUTPUT_MARKDOWN_FILEPATH )
0 commit comments