-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile.py
More file actions
executable file
·155 lines (132 loc) · 5.17 KB
/
compile.py
File metadata and controls
executable file
·155 lines (132 loc) · 5.17 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# coding=utf8
from plugins.bottle.bottle import SimpleTemplate
import os
import sys
import time
TEMPLATES_DIR = 'templates'
PAGES_DIR = 'pages'
DOCS_ROOT = 'docs'
PROG_LANGUAGES = [
'java',
'python'
]
URL_ROOT = '//compedu.stanford.edu/codeInternational/docs/'
LANGUAGES = {
'en':'English',
'es':'Español',
'de':'Deutsch',
'sw':'Kiswahili',
'zh':'普通话',
'ms':'Melayu',
'tr':'Türk',
'cs':'Češky',
'fr':'Français',
'ar':'Arabe',
'hi':'हिंदी',
'he':'עברית',
'pt':'Português',
'ja':'日本人'
}
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):
print('compiling')
print('---------')
# first compile the root index.html
fromPath = os.path.join(TEMPLATES_DIR, 'index.html')
toPath = os.path.join(DOCS_ROOT, 'index.html')
self.compileTemplate('index.html', fromPath, toPath, '', 'en')
# first compile the root index.html
fromPath = os.path.join(TEMPLATES_DIR, 'reader.html')
toPath = os.path.join(DOCS_ROOT, 'reader.html')
self.compileTemplate('reader.html', fromPath, toPath, '', 'en')
for progLang in PROG_LANGUAGES:
print('compiling for {} ({}):'.format(progLang, progLang))
# then compile the landing page for java and python
fromPath = os.path.join(TEMPLATES_DIR, progLang+'/index.html')
toPath = os.path.join(DOCS_ROOT, progLang+'/index.html')
self.compileTemplate('index.html', fromPath, toPath, progLang, 'en')
for lang in LANGUAGES:
print(' - compiling for {} ({}):'.format(lang, self.getLangName(lang)))
webpageFilePaths = self.getWebpageFilePaths(progLang, lang)
if len(webpageFilePaths) == 0:
print(' no templates found for {} ({})'.format(lang, self.getLangName(lang)))
continue
# Create directory for final pages if this is the first time compiling for a language
langPath = DOCS_ROOT + '/' + progLang + '/' + lang
self.makePath(langPath)
for webpage in webpageFilePaths:
fromPath = os.path.join(TEMPLATES_DIR, progLang, lang, PAGES_DIR, webpage)
toPath = os.path.join(langPath, webpage)
print(' - ' + fromPath)
self.compileTemplate(webpage, fromPath, toPath, progLang, lang)
print('')
#####################
# Private Helpers
#####################
def compileTemplate(self, page, fromPath, toPath, progLang, lang):
# print(toPath)
# note: all pages are in language/page
# pathToRoot = '../../'
pathToRoot = URL_ROOT
templateText = open(fromPath).read()
data = {
'pathToRoot':pathToRoot,
'lang':lang,
'progLang':progLang,
'langName':self.getLangName(lang),
'page':page
}
try:
compiledHtml = SimpleTemplate(templateText).render(data)
self.makePath(toPath)
fileName, fileExtension = os.path.splitext(fromPath)
compiledHtml = compiledHtml.encode('utf8')
open(toPath, 'wb').write(compiledHtml)
except Exception as err:
print('failed to compile: ', fromPath)
print('error: {}'.format(err))
def getLangName(self, lang):
return LANGUAGES[lang]
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):
return URL_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 += '../'
print(relativePath, 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 getWebpageFilePaths(self, progLang, lang):
paths = []
templateDirPath = os.path.join(TEMPLATES_DIR, progLang, lang, PAGES_DIR)
if not os.path.exists(templateDirPath):
return paths
for fileName in os.listdir(templateDirPath):
if self.isTemplateFile(fileName):
paths.append(fileName)
return paths
if __name__ == '__main__':
Compiler().run()