1+ import subprocess
2+ import datetime
3+ import os
4+ import re
5+
6+ # Recursively gather all markdown files in the right order
7+ markdownFiles = []
8+
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 )
13+
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"
18+
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+ })
25+
26+ markdownFiles .sort (key = lambda entry : entry ['filename' ])
27+
28+ # Create concatenated document
29+ print ('processing markdown...' )
30+
31+ allMarkdown = ''
32+
33+ for entry in markdownFiles :
34+ contents = entry ['contents' ]
35+
36+ # Add title
37+ contents = '# ' + entry ['title' ] + '\n \n ' + contents
38+
39+ # Fix image links
40+ contents = re .sub (r'\/images\/' , 'images/' , contents )
41+ contents = re .sub (r'\.svg' , '.png' , contents )
42+
43+ # Fix remaining relative links (e.g. code files)
44+ contents = re .sub (r'\]\(\/' , '](https://vulkan-tutorial.com/' , contents )
45+
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 ]
52+
53+ return '](#' + target + ')'
54+
55+ contents = re .sub (r'\]\(!([^)]+)\)' , repl , contents )
56+
57+ allMarkdown += contents + '\n \n '
58+
59+ # Add title
60+ dateNow = datetime .datetime .now ()
61+
62+ metadata = '% Vulkan Tutorial\n '
63+ metadata += '% Alexander Overvoorde\n '
64+ metadata += '% ' + dateNow .strftime ('%B %Y' ) + '\n \n '
65+
66+ allMarkdown = metadata + allMarkdown
67+
68+ with open ('ebook.md' , 'w' ) as f :
69+ f .write (allMarkdown )
70+
71+ # Convert all SVG images to PNG for pandoc
72+ print ('converting svgs...' )
73+
74+ generatedPngs = []
75+
76+ for fn in os .listdir ('images' ):
77+ parts = fn .split ('.' )
78+
79+ if parts [1 ] == 'svg' :
80+ subprocess .check_output (['inkscape' , '-z' , '-e' , 'images/' + parts [0 ] + '.png' , 'images/' + fn ], stderr = subprocess .STDOUT )
81+ generatedPngs .append ('images/' + parts [0 ] + '.png' )
82+
83+ # Building PDF
84+ print ('building pdf...' )
85+
86+ subprocess .check_output (['pandoc' , 'ebook.md' , '-V' , 'documentclass=report' , '-t' , 'latex' , '-s' , '--toc' , '-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' )
94+
95+ for fn in generatedPngs :
96+ os .remove (fn )
0 commit comments