-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.py
215 lines (164 loc) · 6.48 KB
/
build.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
"""Build mypy website (html and css).
Needs libsass (pip install libsass).
NOTES:
This is pretty crufty and was originally translated from an old script
written in Alore. Much of this doesn't make sense any more.
"""
import shutil
from re import sub, DOTALL, match, search
import re
import subprocess
from textwrap import dedent
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
news_items = load(open('news.yaml'), Loader=Loader)['items']
pages = ['index', 'examples', 'tutorial', 'about', 'news', 'faq', 'contact',
'roadmap', 'news',
'overview' # legacy
]
files = ['site.css', 'favicon.ico']
static_dirs = ['static']
out_dir = 'out'
default_title = 'mypy'
default_keywords = 'mypy, python static typing, python optional typing'
default_description = 'Mypy is an experimental Python variant with seamless dynamic and static typing.'
KW = ['class', 'for', 'in', 'return', 'if', 'else', 'void', 'pass',
'def', 'any', 'member', 'while', 'static', 'import', 'raise', 'not',
'yield', 'break', 'interface', 'from', 'with', 'as']
def build_page(p):
t = open('template.html').read()
content = open('%s.html' % p).read()
content, meta = format_content(content)
t = t.replace('$content$', content)
t = t.replace('$title$', meta.get('title', default_title))
t = t.replace('$keywords$', meta.get('keywords', default_keywords))
t = t.replace('$description$', meta.get('description',
default_description))
if '$contents' in t:
t = t.replace('$contents$', build_contents(t))
if '$latestnews$' in t:
t = t.replace('$latestnews$', build_latest_news())
if '$news$' in t:
t = t.replace('$news$', build_news())
f = open('%s/%s.html' % (out_dir, p), 'w')
f.write(t)
f.close()
def format_content(s):
meta = {}
s = sub(r'\$code!([^!]*)!', lambda m: render_code(m.group(1)),
s, flags=DOTALL)
s = sub(r'\$email\(([^)]*)\)\$', lambda m: obfuscate_email(m.group(1)), s)
s = sub(r'\$include\(([^)]*)\)\$', lambda m: include(m.group(1)), s)
s = sub(r'\$example\(([^)]*)\)\$', lambda m: example(m.group(1)), s)
for tag in 'title', 'keywords', 'description':
m = search(r'\$%s ([^$]*)\$' % tag, s)
if m:
meta[tag] = m.group(1)
s = s.replace(m.group(), '')
return s, meta
def render_code(s):
s = s.rstrip()
s = dedent(s)
s = s.replace('<', '<')
s = s.replace('>', '>')
for kw in KW:
s = sub(r'\b%s\b' % kw, r'<span class="kw">%s</span>' % kw, s)
for kw in ('list', 'str', 'print', 'int', 'float', 'tuple', 'bool',
'bytes', 'dict', 'object', 'Dict', 'List', 'Iterator', 'Any',
'Set', 'typevar', 'Generic', 'Iterable', 'Sequence'):
s = sub(r'\b%s\b' % kw, r'<span class="pr">%s</span>' % kw, s)
s = sub(r"('[^']*')", r'<span class="st">\1</span>', s)
s = sub(r'(""".*""")', r'<span class="st">\1</span>', s, flags=DOTALL)
s = sub(r'(#.*)', r'<span class="co">\1</span>', s)
s = sub(r'(""".*?""")', r'<span class="st">\1</span>', s)
s = sub(r'\|\|(.*?)\|\|', r'<span class="hili">\1</span>', s)
return '<pre class="ex">%s</pre>' % s
def include(fnam):
return open(fnam, 'r').read()
def example(fnam):
f1 = render_code(open('demo/%s.py' % fnam, 'r').read())
f2 = render_code(open('demo/%s-s.py' % fnam, 'r').read())
return '''
<div class="ex-left">
<div class="sample-header">Mypy with dynamic typing</div>
%s
</div>
<div class="ex-right">
<div class="sample-header">Mypy with static typing</div>
%s
</div>
<div class="clear"></div>
''' % (f1, f2)
def build_contents(t):
"""Collect all <h2> titles using a regxp and build <li> items."""
res = []
for id, text in re.findall(r'<h2(.*?)>(.*?)</h2>', t):
if text != 'Contents':
m = match(r' +id="(.*)"$', id)
assert m, "Missing id in {}".format(text)
res.append('<li><a href="#{}">{}</a>'.format(m.group(1), text))
return '<ul>\n' + '\n'.join(res) + '</ul>'
def main():
for p in pages:
build_page(p)
subprocess.check_call(['pysassc', 'site.scss', 'site.css'])
for f in files:
shutil.copy(f, out_dir + '/')
for d in static_dirs:
dn = out_dir + '/' + d
shutil.rmtree(dn, ignore_errors=True)
shutil.copytree(d, dn)
# Inline JavaScript code should encoded as CDATA for proper XHTML.
# Include the comments to work with old browsers.
JS_BEGIN = '/* <![CDATA[ */'
JS_END = '/* ]]> */'
# Obfuscate an email address by creating a JavaScript fragment that constructs
# the address + a fallback solution for browsers not supporting javascript.
#
# Expects the following CSS classes, with needed properties in parentheses:
# * adr (no wrapping)
# * hid (hide)
# * char (vertical image alignemnt)
def obfuscate_email(email):
html = ObfuscateEmailHTML(email)
js = ObfuscateEmailJS(email)
return ("<script type=""text/javascript"">" + JS_BEGIN + js + JS_END +
"</script>" + "<noscript>" + html + "</noscript>")
def ObfuscateEmailJS(email):
for ch in "xz-_#":
if not ch in email:
repl = ch
break
if len(email) < 5:
raise ValueError("Unsupported email address")
obfuscated = (email[:2] + repl + email[2:5] + repl + email[5:8] + repl +
email[8:])
res = (
"var addr = ('{}' + '{}').replace(/{}/g, '');".format(
obfuscated[:4], obfuscated[4:], repl) +
"document.write('<a href=\"mailto:'+addr+'\">'+addr+'<'+'/a>');")
return res
def ObfuscateEmailHTML(email):
user, domain = email.split("@")
return ('<span class="adr">\n' +
('{}<span class="x">{}</span><span class="hid">xxx</span>' +
'<img class="char" src="char.png" alt="" />{}').
format(user[:2], user[2:], domain) +
"\n</span>")
def build_news():
res = []
for item in news_items:
res.append('<h2>%s</h2>\n' % item['title'])
res.append('<p><b>%s</b>\n<p>%s<p>- %s' % (item['date'], item['text'], item['author']))
return '\n'.join(res)
def build_latest_news():
res = []
for item in news_items[:4]:
res.append('<p><b>%s</b>\n' % item['title'])
res.append('<p><b>%s</b>: %s -%s' % (item['date'], item['text'], item['author']))
return '\n'.join(res)
if __name__ == '__main__':
main()