Skip to content

Commit 8e5340f

Browse files
committed
for Chinese version
1 parent 75ef306 commit 8e5340f

File tree

1 file changed

+115
-112
lines changed

1 file changed

+115
-112
lines changed

build_ebook.py

Lines changed: 115 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,115 @@
1-
import subprocess
2-
import datetime
3-
import os
4-
import re
5-
6-
7-
def create_ebook(path):
8-
9-
name_path = path
10-
print('\n Creating \"' + name_path + '\" ebook')
11-
# Recursively gather all markdown files in the right order
12-
markdownFiles = []
13-
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)
18-
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:])
26-
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-
})
33-
34-
markdownFiles.sort(key=lambda entry: entry['filename'])
35-
36-
# Create concatenated document
37-
print('processing markdown...')
38-
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 + ')'
63-
64-
contents = re.sub(r'\]\(!([^)]+)\)', repl, contents)
65-
66-
allMarkdown += contents + '\n\n'
67-
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'
74-
75-
allMarkdown = metadata + allMarkdown
76-
77-
with open('ebook.md', 'w') as f:
78-
f.write(allMarkdown)
79-
80-
# Building PDF
81-
print('building pdf...')
82-
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'])
85-
86-
print('building epub...')
87-
88-
subprocess.check_output(
89-
['pandoc', 'ebook.md', '--toc', '-o', 'ebook/Vulkan Tutorial ' + name_path + '.epub', '--epub-cover-image=ebook/cover.png'])
90-
91-
# Clean up
92-
os.remove('ebook.md')
93-
94-
95-
# Convert all SVG images to PNG for pandoc
96-
print('converting svgs...')
97-
98-
generatedPngs = []
99-
100-
for fn in os.listdir('images'):
101-
parts = fn.split('.')
102-
103-
if parts[1] == 'svg':
104-
subprocess.check_output(['inkscape', '--export-filename=images/' +
105-
parts[0] + '.png', 'images/' + fn], stderr=subprocess.STDOUT)
106-
generatedPngs.append('images/' + parts[0] + '.png')
107-
108-
create_ebook('en')
109-
create_ebook('fr')
110-
111-
for fn in generatedPngs:
112-
os.remove(fn)
1+
import subprocess
2+
import datetime
3+
import os
4+
import re
5+
6+
7+
def create_ebook(path):
8+
9+
name_path = path
10+
print('\n Creating \"' + name_path + '\" ebook')
11+
# Recursively gather all markdown files in the right order
12+
markdownFiles = []
13+
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)
18+
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:])
26+
27+
with open(path, 'r', encoding='utf-8') as f:
28+
markdownFiles.append({
29+
'title': title,
30+
'filename': os.path.join(root, fn),
31+
'contents': f.read()
32+
})
33+
34+
markdownFiles.sort(key=lambda entry: entry['filename'])
35+
36+
# Create concatenated document
37+
print('processing markdown...')
38+
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 + ')'
63+
64+
contents = re.sub(r'\]\(!([^)]+)\)', repl, contents)
65+
66+
allMarkdown += contents + '\n\n'
67+
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'
74+
75+
allMarkdown = metadata + allMarkdown
76+
77+
with open('ebook.md', 'w', encoding='utf-8') as f:
78+
f.write(allMarkdown)
79+
80+
# Building PDF
81+
print('building pdf...')
82+
83+
print(' '.join(['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', '-V CJKmainfont="Microsoft YaHei"']))
85+
subprocess.run(['pandoc', 'ebook.md', '-V', 'documentclass=report', '-t', 'latex', '-s',
86+
'--toc', '--listings', '-H', 'ebook/listings-setup.tex', '-o', 'ebook/Vulkan Tutorial ' + name_path + '.pdf', '--pdf-engine=xelatex', '-V CJKmainfont="Microsoft YaHei"'])
87+
88+
print('building epub...')
89+
print(' '.join(['pandoc', 'ebook.md', '--toc', '-o', '\"ebook/Vulkan Tutorial ' + name_path + '.epub\"', '--epub-cover-image=ebook/cover.png', '-V CJKmainfont="Microsoft YaHei"']))
90+
subprocess.run(
91+
['pandoc', 'ebook.md', '--toc', '-o', 'ebook/Vulkan Tutorial ' + name_path + '.epub', '--epub-cover-image=ebook/cover.png', '-V CJKmainfont=Microsoft YaHei'])
92+
93+
# Clean up
94+
#os.remove('ebook.md')
95+
96+
97+
# Convert all SVG images to PNG for pandoc
98+
print('converting svgs...')
99+
100+
generatedPngs = []
101+
102+
for fn in os.listdir('images'):
103+
parts = fn.split('.')
104+
105+
if parts[1] == 'svg':
106+
subprocess.check_output(['inkscape', '--export-filename=images/' +
107+
parts[0] + '.png', 'images/' + fn], stderr=subprocess.STDOUT)
108+
generatedPngs.append('images/' + parts[0] + '.png')
109+
110+
create_ebook('ch')
111+
#create_ebook('en')
112+
#create_ebook('fr')
113+
114+
for fn in generatedPngs:
115+
os.remove(fn)

0 commit comments

Comments
 (0)