Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 990aa2c

Browse files
committed
Update e-book and add proper build process for it
1 parent 0f2a76c commit 990aa2c

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ This guide is now available in e-book formats as well:
2121
* [EPUB](https://raw.githubusercontent.com/Overv/VulkanTutorial/master/ebook/Vulkan%20Tutorial.epub)
2222
* [PDF](https://raw.githubusercontent.com/Overv/VulkanTutorial/master/ebook/Vulkan%20Tutorial.pdf)
2323

24+
The e-book can be built from the existing content by running:
25+
26+
python3 build_ebook.py
27+
28+
This script depends on the following utilities being available on the path:
29+
30+
* `inkscape`: SVG to PNG conversion
31+
* `pandoc`: Building a PDF and EPUB from the Markdown code
32+
33+
You also need to install a LaTeX distribution for PDF generation.
34+
2435
Changing code across chapters
2536
-----------------------------
2637

build_ebook.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)