-
Notifications
You must be signed in to change notification settings - Fork 72
/
compile.py
executable file
·107 lines (91 loc) · 3.34 KB
/
compile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# make sure to run
# > python -m pip install bottle
# I used version bottle-0.12.19
from bottle import SimpleTemplate
from makeChapters import make_chapters
import os.path
import sys
from tqdm import tqdm
IGNORE_DIRS = [
]
TEMPLATE_DIR = 'chapters'
ROOT = 'pythonreader'
OUT_DIR = 'en'
# Use the -t flag if you want to compile for local tests
DEPLOY = False
class Compiler(object):
# Function: Run
# -------------
# This function compiles all the html files (recursively)
# from the templates dir into the current folder. Folder
# hierarchy is preserved
def run(self):
self.link_data = make_chapters()
templateFilePaths = self.getTemplateFilePaths('')
for templateFilePath in tqdm(templateFilePaths):
self.compileTemplate(templateFilePath)
#####################
# Private Helpers
#####################
def compileTemplate(self, relativePath):
pathToLangRoot = self.getPathToRoot(relativePath)
filePath = os.path.join(TEMPLATE_DIR, relativePath)
templateText = open(filePath, encoding='utf-8').read()
params = {
'pathToRoot': '../' + pathToLangRoot,
'pathToLang' : pathToLangRoot,
}
self.addChapterLinks(params)
compiledHtml = SimpleTemplate(templateText).render(params)
compiledHtml = compiledHtml.encode('utf8')
path = OUT_DIR + '/' + relativePath
self.makePath(path)
open(path, 'wb').write(compiledHtml)
def makePath(self, path):
dirPath = os.path.dirname(path)
if dirPath == '': return
if not os.path.exists(dirPath):
os.makedirs(dirPath)
def getPathToRoot(self, relativePath):
if DEPLOY:
return ROOT
return self.getRelPathToRoot(relativePath)
def getRelPathToRoot(self, relativePath):
dirs = self.splitDirs(relativePath)
depth = len(dirs) - 1
pathToRoot = ''
for i in range(depth, 0, -1):
curr = dirs[i]
pathToRoot += '../'
return pathToRoot
def splitDirs(self, filePath):
if filePath == '': return []
rootPath, last = os.path.split(filePath)
rootDirs = self.splitDirs(rootPath)
rootDirs.append(last)
return rootDirs
def isTemplateFile(self, fileName):
extension = os.path.splitext(fileName)[1]
return extension == '.html'
def getTemplateFilePaths(self, root):
if root in IGNORE_DIRS: return []
paths = []
templateDirPath = os.path.join(TEMPLATE_DIR, root)
for fileName in os.listdir(templateDirPath):
filePath = os.path.join(root, fileName)
templateFilePath = os.path.join(TEMPLATE_DIR, filePath)
if os.path.isdir(templateFilePath):
childPaths = self.getTemplateFilePaths(filePath)
for childPath in childPaths:
paths.append(childPath)
elif self.isTemplateFile(fileName):
paths.append(filePath)
return paths
def addChapterLinks(self, params):
for key, data in self.link_data.items():
compiled_path = SimpleTemplate(data['path']).render(params)
params[key] = '<a href="{}">{}</a>'.format(compiled_path, data['title'])
pass
if __name__ == '__main__':
Compiler().run()