-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.py
executable file
·126 lines (108 loc) · 3.46 KB
/
pages.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
import glob
import json
import os
import re
import shutil
import sys
HEADER_TEMPLATE = '''
<!DOCTYPE html>
<meta charset=utf-8>
<title>Project Euler - Solutions in JavaScript</title>
<link rel=stylesheet href=s/pe.min.css>
<link rel=stylesheet href=s/desert.min.css>
<h1>Project Euler</h1>
<h2>Solutions in JavaScript</h2>
<div id=main>
<div id=pe>
<h3><div id=p>Problem <div id=pn>{0}</div></div><div id=pt>{1}</div></h3>
<p id=pd>{2}
<h3 id=s>Solution</h3>
'''.lstrip()
CODE_TEMPLATE = '''
<pre id=sc class=prettyprint>
<code class=language-javascript>
{0}
</code>
</pre>
'''
FOOTER_TEMPLATE = '''
</div>
</div>
'''
RUNNER_TEMPLATE = '''
<div>
<input id=r type=button value=Run></td>
<table id=tr>
<tr>
<td id=text></td>
<td id=solution></td>
<td id=time></td>
<td id=icon></td>
</tr>
</table>
</div>
'''
PREV_NAV_TEMPLATE = '''
<a href={0}.html rel=prev title="back to Problem {0}"><img src=i/p.png alt=<></a>
'''.strip()
PROBLEM_NAV_TEMPLATE = '''
<a href={0}.html title="go to Problem {0}"><span>{0}</span></a>
'''.strip()
SELECTED_NAV_TEMPLATE = '''
<span class=s>{0}</span>
'''.strip()
NEXT_NAV_TEMPLATE = '''
<a href={0}.html rel=next title="onward to Problem {0}"><img src=i/n.png alt=>></a>
'''.strip()
SCRIPT_TEMPLATE = '''
<p class=c>© 2012 <a href=http://github.com/gpiancastelli>Giulio Piancastelli</a><br>
Suggestions? Issues? Comments? <a href=https://github.com/gpiancastelli/project-euler-js/issues>I’d love to hear you!</a>
<script src=j/jquery.min.js></script>
<script src=j/prettify.min.js></script>
'''
DEST_DIR = sys.argv[1] if len(sys.argv) > 0 else '.'
def navigation(p, np):
navigation = []
if p - 1:
navigation.append(PREV_NAV_TEMPLATE.format(p - 1))
for i in range(1, np + 1):
if i == p:
navigation.append(SELECTED_NAV_TEMPLATE.format(i))
else:
navigation.append(PROBLEM_NAV_TEMPLATE.format(i))
if p < np:
navigation.append(NEXT_NAV_TEMPLATE.format(p + 1))
return '<p class=v>' + ' '.join(navigation)
SOLUTION_TEMPLATE = r'''
var problem = {{
solution: \1,
args: [\2],
result: {0}
}};'''.lstrip()
def solution(n, js, result):
filename = os.path.join('j', str(n) + '.js')
with open(os.path.join(DEST_DIR, filename), 'w') as f:
pattern = r'console\.log\((.*)\((.*)\)\);'
replacement = SOLUTION_TEMPLATE.format(result)
f.write(re.sub(pattern, replacement, js))
# return the new JavaScript file name for the solution
return os.path.basename(filename)
with open('problems.json') as p:
problems = json.load(p)
solutions = glob.glob('*.js')
for filename in solutions:
n = int(os.path.splitext(filename)[0]) # cut leading zeros
pagename = os.path.join(DEST_DIR, str(n) + '.html')
problem = problems[n - 1]
with open(filename) as input, open(pagename, 'w') as output:
output.write(HEADER_TEMPLATE.format(n, problem['title'], problem['desc']))
js = input.read()
output.write(CODE_TEMPLATE.format(js))
output.write(FOOTER_TEMPLATE)
output.write(RUNNER_TEMPLATE)
output.write(navigation(n, len(solutions)))
output.write(SCRIPT_TEMPLATE)
output.write('<script src=j/' + solution(n, js, problem['result']) + '></script>')
# first problem page is also home page
shutil.copy(os.path.join(DEST_DIR, '1.html'),
os.path.join(DEST_DIR, 'index.html'))