forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_head.py
executable file
·85 lines (65 loc) · 1.83 KB
/
html_head.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
#!/usr/bin/env python2
"""
html_head.py: Emit <html><head> boilerplate.
And make sure it works on mobile!
"""
from __future__ import print_function
import sys
import cgi
import cStringIO
import optparse
from doctools import doc_html
# Python library. Also see doctools/doc_html.py.
def Write(f, title, css_urls=None, js_urls=None):
css_urls = css_urls or []
js_urls = js_urls or []
# Note: do lang=en and charset=utf8 matter? I guess if goes to a different
# web server?
f.write('''\
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>%s</title>
''' % cgi.escape(title))
# Write CSS files first I guess?
for url in css_urls:
f.write(doc_html.CSS_FMT % cgi.escape(url))
for url in js_urls:
f.write(doc_html.JS_FMT % cgi.escape(url))
f.write('''\
</head>
''')
# Not used now
def HtmlHead(title, css_urls=None, js_urls=None):
f = cStringIO.StringIO()
Write(f, title, css_urls=css_urls, js_urls=js_urls)
return f.getvalue()
def main(argv):
p = optparse.OptionParser('html_head.py FLAGS? CSS_JS*')
p.add_option(
'-t', '--title', dest='title', default='',
help='The title of the web page')
opts, argv = p.parse_args(argv[1:])
# Make it easier to use from shell scripts
css_urls = []
js_urls = []
for arg in argv:
i = arg.rfind('?') # account for query param
if i != -1:
url = arg[:i]
else:
url = arg
if url.endswith('.js'):
js_urls.append(arg)
elif url.endswith('.css'):
css_urls.append(arg)
else:
raise RuntimeError("Expected URL with .js or .css, got %r" % arg)
Write(sys.stdout, opts.title, css_urls=css_urls, js_urls=js_urls)
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)