-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-scales.py
executable file
·245 lines (208 loc) · 8.35 KB
/
go-scales.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/python
# converted to python3 19/Aug/2022
# Moved to EC2 5/Sep/2020
# A script for creating the static HTML pages for the scales
# Finds the list of Javascript scales from the js/scales directory
# Create HTML pages for them
# Then it creates Javascript files for the guidelines using org-mode encoded sources in the js/guidelines folder.
# It then offers to copy files to Android Studio Project assets folder
# created Dean Jenkins 8/Oct/2017
import os, shutil, datetime
#from distutils.dir_util import copy_tree
import sys, html, re
import Orgnode
#### set up some functions ####
# method for getting filenames from a directory
# top directory os.walk trick adapted from https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory#3207973
def filenamesbyextension(path,extension):
extension = "." + extension.lstrip(".") # in case this function is called with a dot or not
fullfilenames = []
for (dirpath, dirnames, filenames) in os.walk(path):
fullfilenames.extend(filenames)
break
filenames = []
for name in fullfilenames:
if name.endswith(extension): # return only those with the extension
filenames.append(re.sub(extension+'$', '', name))
return filenames
# command line colour codes from https://stackoverflow.com/a/27265453/4066963
def red(string):
W = '\033[0m' # white (normal)
R = '\033[31m' # red
return R + string + W
def green(string):
W = '\033[0m' # white (normal)
G = '\033[32m' # green
return G + string + W
###############################
print("Making HTML files for the scales")
# scales to be included
scales = filenamesbyextension('js/scales','js')
# guidelines to be included
guidelines = filenamesbyextension('js/guidelines','txt')
# HTML template
scale_template = 'go-scales_template.html'
scalecounter = 0
scale_skipped = 0
guideline_template = 'go-guidelines_template.html'
guideline_template_js = 'go-guidelines_template.js'
guidelinecounter = 0
guideline_skipped = 0
js_swap_string = "XXXXXXXXXX.js"
#### do the scales ####
# read scale template
print("Using template '" + scale_template + "'")
f = open(scale_template)
template = f.read()
f.close()
# check scale template is OK
if template.find(js_swap_string) == -1:
print(red("Fatal error: template swap string '" + js_swap_string + "' not found."))
sys.exit()
# create the scales files
for x in scales:
msg = " " + x + " "
if os.path.isfile('js/scales/'+x+'.js'):
scale = open("scale_" + x + ".html","w")
scale.write(template.replace(js_swap_string,x + ".js",1))
scale.close()
msg = msg + green("... done 'scale_" + x + ".html'")
scalecounter = scalecounter + 1
else:
msg = msg + red(" ... scale skipped as no 'js/scales/"+x+".js'")
scale_skipped = scale_skipped + 1
print(msg)
# generate a report
report = "Finished. "
if scale_skipped == 0:
report = report + ""
elif scale_skipped == 1:
report = report + "1 scale skipped. "
else:
report = report + str(scale_skipped) + " scales skipped. "
if scalecounter == 1:
report = report + "1 scale created. "
else:
report = report + str(scalecounter) + " scales done. "
print(report)
#### do the guidelines ####
# read guideline template HTML
print("Using template '" + guideline_template + "'")
f = open(guideline_template)
template = f.read()
f.close()
# check guideline template HTML is OK
if template.find(js_swap_string) == -1:
print(red("Fatal error: template swap string '" + js_swap_string + "' not found."))
sys.exit()
# read guideline template JS
print("Using template '" + guideline_template_js + "'")
f = open(guideline_template_js)
template_js = f.read()
f.close()
# check guideline template JS is OK
if template_js.find(js_swap_string) == -1:
print(red("Fatal error: template swap string '" + js_swap_string + "' not found."))
sys.exit()
def htmlentities(string):
string = html.escape(string) # HTML entities
string = re.sub(r"'","\\'",string)
string = re.sub(r'\[\[(.*?)\]\[(.*?)\]\]',r'<a href="\1">\2</a>',string) # find and code org-mod encoded hyperlinks
return string
# create the guidelines files
for x in guidelines:
msg = " " + x + " "
if os.path.isfile('js/guidelines/'+x+'.txt'):
# get the guideline text file in org-mode format
nodelist = Orgnode.makelist('js/guidelines/'+x+'.txt')
guidelinehtml = ""
headernumber = 0
for n in nodelist:
# make HTML headings tags
guidelinehtml += '<h' + str(n.Level()) + ' data-target="#glb_' + str(headernumber) + '" data-toggle="collapse" style="cursor: pointer;">' # HTML Heading tag open. The inline style required for forcing iOS to behave.
guidelinehtml += htmlentities(n.Heading())
guidelinehtml += "</h" + str(n.Level()) + ">\n" # HTML Heading tag close
# if there is a body below a heading then make it in to HTML too
bodylist = []
# collect the list items
for line in n.Body().split('\n'):
if len(line) > 0:
bodylist.append("<p>"+htmlentities(line)+"</p>\n")
# only if there are list items make a HTML unordered list
# (this forces ignoring of blank lines in the guideline file)
if len(bodylist) > 0:
guidelinehtml += '<div id="glb_' + str(headernumber) + '" class="collapse">'
#guidelinehtml += "<ul>" # not using lists at the moment (looks ugly)
for item in bodylist:
guidelinehtml += item
#guidelinehtml += "</ul>" # not using lists at the moment (looks ugly)
guidelinehtml += '</div>'
headernumber = headernumber + 1
# make guidelinehtml safe for javascript
guidelinehtml = guidelinehtml.replace("'","\'")
guidelinehtmllines = guidelinehtml.split("\n")
# wrap the html into a Javascript document.write() lines
jsinsert = ""
for line in guidelinehtmllines:
if line != "":
jsinsert += "document.write('" + line + "');\n"
# put the Javascript link into the HTML page
guideline = open("guideline_" + x + ".html","w")
guideline.write(template.replace(js_swap_string,"guideline_" + x + ".js",1))
guideline.close()
# create the Javascript source file
guideline_js = open("js/guidelines/guideline_" + x + ".js","w")
guideline_js.write(template_js.replace(js_swap_string,jsinsert,1))
guideline_js.close()
msg = msg + green("... done 'guideline_" + x + ".html'")
guidelinecounter = guidelinecounter + 1
else:
msg = msg + red(" ... guideline skipped as no 'js/guidelines/"+x+" .txt'")
guideline_skipped = guideline_skipped + 1
print(msg)
# generate a report
report = "Finished. "
if guideline_skipped == 0:
report = report + ""
elif guideline_skipped == 1:
report = report + "1 guideline skipped. "
else:
report = report + str(guideline_skipped) + " guideline skipped. "
if guidelinecounter == 1:
report = report + "1 guideline created. "
else:
report = report + str(guidelinecounter) + " guidelines done. "
print(report)
print("Updating version date")
# read about file
f = open('about.html')
aboutfile = f.read()
f.close()
# check about file has detectable swap string
if aboutfile.find('Version: ') == -1:
print(red("Fatal error: about.html does not contain 'Version: ' not found."))
sys.exit()
else:
today = datetime.datetime.utcnow()
aboutfile = re.sub(r'Version: .*?</p>','Version: ' + today.strftime('%d %B %Y (%H:%M:%S UTC)') + '</p>',aboutfile)
about_new = open("about.html","w")
about_new.write(aboutfile)
about_new.close()
print("Updating service worker cache name")
# read service worker file
f = open('sw.js')
swfile = f.read()
f.close()
# check about file has detectable swap string
if swfile.find('cacheName = "cache_') == -1:
print(red("Fatal error: sw.js does not contain 'cacheName = \"cache_' not found."))
sys.exit()
else:
thisversion = datetime.datetime.utcnow()
swfile = re.sub(r'cacheName = \"cache_.*?\";','cacheName = \"cache_' + thisversion.strftime('%Y%m%d%H%M%S') + '\";',swfile)
sw_new = open("sw.js","w")
sw_new.write(swfile)
sw_new.close()
# print reminder
print("remember to add menu items if new scales")
input("Hit ENTER to exit ") # python 2