-
Notifications
You must be signed in to change notification settings - Fork 10
/
compile.py
executable file
·118 lines (100 loc) · 3.95 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
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
from plugins.bottle.bottle import SimpleTemplate
import os.path
import sys
IGNORE_DIRS = [
'parts'
]
TEMPLATE_DIR = 'templates'
# Use the -t flag if you want to compile for local tests
DEPLOY = not '-t' in sys.argv
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):
templateFilePaths = self.getTemplateFilePaths('')
for templateFilePath in templateFilePaths:
self.compileTemplate(templateFilePath)
#####################
# Private Helpers
#####################
def includeFiles(self, compiledHtml):
# find all instances of
# '<!--#include virtual="'
# and then extract all the way until -->
# and keep the position
loc = 0
strToFind = '<!--#include virtual="'
while (True):
beginPos = compiledHtml.find(strToFind,loc)
if beginPos == -1:
break
# now find the end of the string
endPos = compiledHtml.find('"',beginPos+len(strToFind))
virtPath = compiledHtml[beginPos+len(strToFind):endPos]
insertLoc = compiledHtml.find('-->',endPos) + 3
# now load the included file, and stick it into the html
with open(virtPath,"r") as f:
incHtml = f.read()
compiledHtml = compiledHtml[0:insertLoc] + incHtml + compiledHtml[insertLoc+1:]
loc = endPos + len(incHtml)
return compiledHtml
def compileTemplate(self, relativePath):
print(relativePath)
pathToRoot = self.getPathToRoot(relativePath)
filePath = os.path.join(TEMPLATE_DIR, relativePath)
templateText = open(filePath).read()
compiledHtml = SimpleTemplate(templateText).render(pathToRoot = pathToRoot)
self.makePath(relativePath)
fileName, fileExtension = os.path.splitext(relativePath)
compiledHtml = compiledHtml.encode('utf8')
# shiv here to parse for included files
compiledHtml = self.includeFiles(compiledHtml)
open(relativePath, '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 '//web.stanford.edu/class/cs106x/'
return '//web.stanford.edu/class/archive/cs/cs106x/cs106x.1174/'
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 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
if __name__ == '__main__':
Compiler().run()