Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompile.py
More file actions
Latest commit
executable file
·96 lines (79 loc) · 2.95 KB
/
Copy pathcompile.py
File metadata and controls
executable file
·96 lines (79 loc) · 2.95 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
#!/usr/bin/env python
fromplugins.bottle.bottleimportSimpleTemplate
importos.path
importsys
IGNORE_DIRS= [
'parts'
]
TEMPLATE_DIR='templates'
ROOT='pythonreader'
OUT_DIR='en'
# Use the -t flag if you want to compile for local tests
DEPLOY=False
classCompiler(object):
# Function: Run
# -------------
# This function compiles all the html files (recursively)
# from the templates dir into the current folder. Folder
# hierarchy is preserved
defrun(self):
templateFilePaths=self.getTemplateFilePaths('')
fortemplateFilePathintemplateFilePaths:
self.compileTemplate(templateFilePath)
#####################
# Private Helpers
#####################
defcompileTemplate(self, relativePath):
print(relativePath)
pathToLangRoot=self.getPathToRoot(relativePath)
filePath=os.path.join(TEMPLATE_DIR, relativePath)
templateText=open(filePath).read()
compiledHtml=SimpleTemplate(templateText).render(pathToRoot='../'+pathToLangRoot, pathToLang=pathToLangRoot)
fileName, fileExtension=os.path.splitext(relativePath)
compiledHtml=compiledHtml.encode('utf8')
path=OUT_DIR+'/'+relativePath
self.makePath(path)
open(path, 'wb').write(compiledHtml)
defmakePath(self, path):
dirPath=os.path.dirname(path)
ifdirPath=='': return
ifnotos.path.exists(dirPath):
os.makedirs(dirPath)
defgetPathToRoot(self, relativePath):
ifDEPLOY:
returnROOT
returnself.getRelPathToRoot(relativePath)
defgetRelPathToRoot(self, relativePath):
dirs=self.splitDirs(relativePath)
depth=len(dirs) -1
pathToRoot=''
foriinrange(depth, 0, -1):
curr=dirs[i]
pathToRoot+='../'
print(relativePath, pathToRoot)
returnpathToRoot
defsplitDirs(self, filePath):
iffilePath=='': return []
rootPath, last=os.path.split(filePath)
rootDirs=self.splitDirs(rootPath)
rootDirs.append(last)
returnrootDirs
defisTemplateFile(self, fileName):
extension=os.path.splitext(fileName)[1]
returnextension=='.html'
defgetTemplateFilePaths(self, root):
ifrootinIGNORE_DIRS: return []
paths= []
templateDirPath=os.path.join(TEMPLATE_DIR, root)
forfileNameinos.listdir(templateDirPath):
filePath=os.path.join(root, fileName)
templateFilePath=os.path.join(TEMPLATE_DIR, filePath)
ifos.path.isdir(templateFilePath):
childPaths=self.getTemplateFilePaths(filePath)
forchildPathinchildPaths:
paths.append(childPath)
elifself.isTemplateFile(fileName):
paths.append(filePath)
returnpaths
if__name__=='__main__':
Compiler().run()