-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjst.py
110 lines (92 loc) · 4.31 KB
/
jst.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
## Compiler for the jst html templating system
## - Made to meet the specific needs of this site and may not be readily applicable elsewhere.
## Author: Jon Janelle
import re, os, os.path
##
def createCssTag(file_name, root_path, css_paths):
for path in css_paths:
if file_name in os.listdir("{0}/{1}".format(root_path, path)):
print("Added " + file_name)
#return "<link rel='stylesheet' href='{0}/{1}/{2}'>".format(root_path,path, file_name)
return "<link rel='stylesheet' href='{0}/{1}'>".format(path, file_name)
##
def createJsTag(file_name, root_path, js_paths):
for path in js_paths:
if file_name in os.listdir("{0}/{1}".format(root_path, path)):
print("Added " + file_name)
#return "<script type='text/javascript' src='{0}/{1}/{2}'></script>".format(root_path, path, file_name)
return "<script type='text/javascript' src='{0}/{1}'></script>".format(path, file_name)
##
def addFileToStr(fileName, resultString, root_path, css_paths, js_paths, ext):
print("Import " + fileName)
fh = open(fileName, 'r')
for line in fh:
search = re.search('{{.*}}', line)
if search != None:
match = search.group(0).replace('{{','').replace('}}','').split(':')
if match[0] == 'css':
css_include = createCssTag(match[1], root_path, css_paths)
if css_include != None:
resultString += css_include
else:
print("Warning: css file {0} could not be found".format(match[1]))
elif match[0] == 'js':
js_include = createJsTag(match[1], root_path, js_paths)
if js_include != None:
resultString += js_include
else:
print("Warning: JavaScript file {0} could not be found".format(match[1]))
elif match[0] == 'import':
resultString = addFileToStr("{0}{1}".format(match[1],ext), resultString, root_path, css_paths, js_paths, ext)
else:
resultString += line
fh.close()
return resultString
##
def compileFile(fileName, current_dir, ext, outExt, root_path, css_paths, js_paths, output_directory):
original_dir = os.getcwd()
os.chdir(current_dir)
fh = open("{0}{1}".format(fileName, ext),'r')
outFileName = "{0}{1}".format(fileName, outExt)
try:
os.remove("{0}/{1}".format(output_directory,outFileName))
except OSError:
pass
outText = str()
outText = addFileToStr("{0}{1}".format(fileName, ext), outText, root_path, css_paths, js_paths, ext)
fh.close()
os.chdir(output_directory)
outFile = open(outFileName,'x')
outFile.write(outText)
outFile.close()
print(outFileName + " created successfully")
os.chdir(original_dir)
# Get array of all file names in a directory with a given extension
# dir: directory path string
# ext: file extension
def getFilesInDir(dir_path, ext=None):
if ext:
if ext[0] != '.':
ext = "." + ext
return [f for f in os.listdir(dir_path) if (os.path.isfile("{0}/{1}".format(dir_path, f)) and f.endswith(ext))]
else:
return [f for f in os.listdir(dir_path) if os.path.isfile("{0}/{1}".format(dir_path, f))]
## Run script from root directory of project (i.e. where index.html is located)
## Update css_paths and source_dirs when new stylesheet and view directories added
if __name__ == "__main__":
print("Beginning compilation...")
# source_dirs and css_paths are relative to the root directory of the project
source_dirs = ['.', 'circles', 'fractals']
css_paths = ['css', 'vendor/bootstrap/css']
js_paths = ['js', 'vendor/jquery','vendor/bootstrap/js']
output_directory = 'C:\dev\FractalExplorer\dist'
ext = ".jst" # Compile all files with extension jst
outExt = ".html" # Output as html
root_path = os.getcwd() # This compiler assumed that compilation begins in root directory
for current_dir in source_dirs:
jstFiles = getFilesInDir(current_dir, ext)
for fn in jstFiles:
f_tmp = fn.split(".")
fileName = str('.').join(f_tmp[0:len(f_tmp)-1])
compileFile(fileName, current_dir, ext, outExt, root_path, css_paths, js_paths, output_directory)
print("Compilation completed")