33import os
44import re
55
6- # Recursively gather all markdown files in the right order
7- markdownFiles = []
86
9- for root , subdirs , files in os .walk ('.' ):
10- for fn in files :
11- if 'md' in fn and 'ebook.md' not in fn :
12- path = os .path .join (root , fn )
7+ def create_ebook (path ):
138
14- # "02_Development_environment.md" -> "Development environment"
15- title = fn . split ( '.' )[ 0 ] # "02_Development_environment.md" -> "02_Development_environment"
16- title = title . replace ( '_' , ' ' ) # "02_Development_environment" -> "02 Development environment"
17- title = ' ' . join ( title . split ( ' ' )[ 1 :]) # "02 Development environment" -> "Development environment"
9+ name_path = path
10+ print ( 'create \" ' + name_path + ' \" ebook' )
11+ # Recursively gather all markdown files in the right order
12+ markdownFiles = []
1813
19- with open (path , 'r' ) as f :
20- markdownFiles .append ({
21- 'title' : title ,
22- 'filename' : os .path .join (root , fn ),
23- 'contents' : f .read ()
24- })
14+ for root , subdirs , files in os .walk (name_path ):
15+ for fn in files :
16+ if 'md' in fn and 'ebook.md' not in fn :
17+ path = os .path .join (root , fn )
2518
26- markdownFiles .sort (key = lambda entry : entry ['filename' ])
19+ # "02_Development_environment.md" -> "Development environment"
20+ # "02_Development_environment.md" -> "02_Development_environment"
21+ title = fn .split ('.' )[0 ]
22+ # "02_Development_environment" -> "02 Development environment"
23+ title = title .replace ('_' , ' ' )
24+ # "02 Development environment" -> "Development environment"
25+ title = ' ' .join (title .split (' ' )[1 :])
2726
28- # Create concatenated document
29- print ('processing markdown...' )
27+ with open (path , 'r' ) as f :
28+ markdownFiles .append ({
29+ 'title' : title ,
30+ 'filename' : os .path .join (root , fn ),
31+ 'contents' : f .read ()
32+ })
3033
31- allMarkdown = ''
34+ markdownFiles . sort ( key = lambda entry : entry [ 'filename' ])
3235
33- for entry in markdownFiles :
34- contents = entry [ 'contents' ]
36+ # Create concatenated document
37+ print ( 'processing markdown...' )
3538
36- # Add title
37- contents = '# ' + entry ['title' ] + '\n \n ' + contents
39+ allMarkdown = ''
40+
41+ for entry in markdownFiles :
42+ contents = entry ['contents' ]
43+
44+ # Add title
45+ contents = '# ' + entry ['title' ] + '\n \n ' + contents
46+
47+ # Fix image links
48+ contents = re .sub (r'\/images\/' , 'images/' , contents )
49+ contents = re .sub (r'\.svg' , '.png' , contents )
50+
51+ # Fix remaining relative links (e.g. code files)
52+ contents = re .sub (
53+ r'\]\(\/' , '](https://vulkan-tutorial.com/' , contents )
54+
55+ # Fix chapter references
56+ def repl (m ):
57+ target = m .group (1 )
58+ target = target .lower ()
59+ target = re .sub ('_' , '-' , target )
60+ target = target .split ('/' )[- 1 ]
61+
62+ return '](#' + target + ')'
3863
39- # Fix image links
40- contents = re .sub (r'\/images\/' , 'images/' , contents )
41- contents = re .sub (r'\.svg' , '.png' , contents )
64+ contents = re .sub (r'\]\(!([^)]+)\)' , repl , contents )
4265
43- # Fix remaining relative links (e.g. code files)
44- contents = re .sub (r'\]\(\/' , '](https://vulkan-tutorial.com/' , contents )
66+ allMarkdown += contents + '\n \n '
4567
46- # Fix chapter references
47- def repl ( m ):
48- target = m . group ( 1 )
49- target = target . lower ()
50- target = re . sub ( '_' , '-' , target )
51- target = target . split ( '/' )[ - 1 ]
68+ # Add title
69+ dateNow = datetime . datetime . now ()
70+
71+ metadata = '% Vulkan Tutorial \n '
72+ metadata += '% Alexander Overvoorde \n '
73+ metadata += '% ' + dateNow . strftime ( '%B %Y' ) + ' \n \n '
5274
53- return '](#' + target + ')'
75+ allMarkdown = metadata + allMarkdown
5476
55- contents = re .sub (r'\]\(!([^)]+)\)' , repl , contents )
77+ with open ('ebook.md' , 'w' ) as f :
78+ f .write (allMarkdown )
5679
57- allMarkdown += contents + '\n \n '
80+ # Building PDF
81+ print ('building pdf...' )
5882
59- # Add title
60- dateNow = datetime . datetime . now ( )
83+ subprocess . check_output ([ 'pandoc' , 'ebook.md' , '-V' , 'documentclass=report' , '-t' , 'latex' , '-s' ,
84+ '--toc' , '--listings' , '-H' , 'ebook/listings-setup.tex' , '-o' , 'ebook/Vulkan Tutorial ' + name_path + '.pdf' , '--pdf-engine=xelatex' ] )
6185
62- metadata = '% Vulkan Tutorial\n '
63- metadata += '% Alexander Overvoorde\n '
64- metadata += '% ' + dateNow .strftime ('%B %Y' ) + '\n \n '
86+ print ('building epub...' )
6587
66- allMarkdown = metadata + allMarkdown
88+ subprocess .check_output (
89+ ['pandoc' , 'ebook.md' , '--toc' , '-o' , 'ebook/Vulkan Tutorial ' + name_path + '.epub' ])
90+
91+ # Clean up
92+ os .remove ('ebook.md' )
6793
68- with open ('ebook.md' , 'w' ) as f :
69- f .write (allMarkdown )
7094
7195# Convert all SVG images to PNG for pandoc
7296print ('converting svgs...' )
@@ -77,20 +101,12 @@ def repl(m):
77101 parts = fn .split ('.' )
78102
79103 if parts [1 ] == 'svg' :
80- subprocess .check_output (['inkscape' , '-z' , '-e' , 'images/' + parts [0 ] + '.png' , 'images/' + fn ], stderr = subprocess .STDOUT )
104+ subprocess .check_output (['inkscape' , '-z' , '-e' , 'images/' +
105+ parts [0 ] + '.png' , 'images/' + fn ], stderr = subprocess .STDOUT )
81106 generatedPngs .append ('images/' + parts [0 ] + '.png' )
82107
83- # Building PDF
84- print ('building pdf...' )
85-
86- subprocess .check_output (['pandoc' , 'ebook.md' , '-V' , 'documentclass=report' , '-t' , 'latex' , '-s' , '--toc' , '--listings' , '-H' , 'ebook/listings-setup.tex' , '-o' , 'ebook/Vulkan Tutorial.pdf' ])
87-
88- print ('building epub...' )
89-
90- subprocess .check_output (['pandoc' , 'ebook.md' , '--toc' , '-o' , 'ebook/Vulkan Tutorial.epub' ])
91-
92- # Clean up
93- os .remove ('ebook.md' )
108+ create_ebook ('en' )
109+ create_ebook ('fr' )
94110
95111for fn in generatedPngs :
96112 os .remove (fn )
0 commit comments