]> jfr.im git - irc/quakenet/qwebirc.git/blob - bin/pagegen.py
switch base64 routines out for MIT licensed version: base64-js
[irc/quakenet/qwebirc.git] / bin / pagegen.py
1 import os, sys, pages, subprocess, re, optionsgen, config
2
3 class GitException(Exception):
4 pass
5
6 def jslist(name, debug):
7 ui = pages.UIs[name]
8 if debug:
9 x = [pages.JS_DEBUG_BASE, ui.get("extra", []), pages.DEBUG, ["debug/ui/frontends/%s" % y for y in ui["uifiles"]]]
10 gitid = ""
11 else:
12 #x = [pages.JS_BASE, ui.get("buildextra", ui.get("extra", [])), pages.BUILD_BASE, name]
13 x = [pages.JS_RAW_BASE, name]
14 gitid = "-" + getgitid()
15
16 l = []
17 for url in pages.flatten(x):
18 if isinstance(url, tuple):
19 url, digest = url
20 else:
21 digest = None
22 l.append((url if url.startswith("//") else "js/%s%s.js" % (url, gitid), digest))
23 return l
24
25 def csslist(name, debug, gen=False):
26 ui = pages.UIs[name]
27 nocss = ui.get("nocss")
28 if not debug:
29 return ["css/%s-%s.css" % (name, getgitid())]
30 css = list(pages.flatten([ui.get("extracss", []), "colours", "dialogs"]))
31 if not nocss:
32 css+=[name]
33 css = ["%s.css" % x for x in css]
34 if hasattr(config, "CUSTOM_CSS"):
35 css+=[config.CUSTOM_CSS]
36 return list("css/%s%s" % ("debug/" if gen else "", x) for x in css)
37
38 def _getgitid():
39 try:
40 p = subprocess.Popen(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, shell=os.name == "nt")
41 except Exception, e:
42 if hasattr(e, "errno") and e.errno == 2:
43 raise GitException, "unable to execute"
44 raise GitException, "unknown exception running git: %s" % repr(e)
45
46 data = p.communicate()[0]
47 if p.wait() != 0:
48 raise GitException, "unable to get id"
49 return re.match("^([0-9a-f]+).*", data).group(1)
50
51 GITID = None
52 def getgitid():
53 global GITID
54 if GITID is None:
55 try:
56 GITID = _getgitid()
57 except GitException, e:
58 print >>sys.stderr, "warning: git: %s (using a random id)." % e
59 GITID = os.urandom(10).encode("hex")
60 return GITID
61
62 def producehtml(name, debug):
63 ui = pages.UIs[name]
64 js = jslist(name, debug)
65 css = csslist(name, debug, gen=True)
66 csshtml = "\n".join(" <link rel=\"stylesheet\" href=\"%s%s\" type=\"text/css\"/>" % (config.STATIC_BASE_URL, x) for x in css)
67
68 def toscript((url, digest)):
69 if digest:
70 subresource_int = " integrity=\"%s\" crossorigin=\"anonymous\"" % digest
71 else:
72 subresource_int = ""
73 return " <script type=\"text/javascript\" src=\"%s%s\"%s></script>" % ("" if url.startswith("//") else config.STATIC_BASE_URL, url, subresource_int)
74
75 jshtml = "\n".join(toscript(x) for x in js)
76
77 if hasattr(config, "ANALYTICS_HTML"):
78 jshtml+="\n" + config.ANALYTICS_HTML
79
80 div = ui.get("div", "")
81 customjs = ui.get("customjs", "")
82
83 return """%s
84 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
85 <head>
86 <base />
87 <title>%s (qwebirc)</title>
88 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
89 <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
90 <meta name="mobile-web-app-capable" content="yes" />
91 <link rel="icon" sizes="192x192" href="%simages/highresicon.png"/>
92 <link rel="shortcut icon" type="image/png" href="%simages/favicon.png"/>
93 %s<script type="text/javascript">QWEBIRC_DEBUG=%s;</script>%s
94 %s
95 <script type="text/javascript">
96 var ui = new qwebirc.ui.Interface("ircui", qwebirc.ui.%s, %s);
97 </script>
98 </head>
99 <body>
100 <div id="ircui">
101 <noscript>
102 <div id="noscript">Javascript is required to use IRC.</div>
103 </noscript>%s
104 </div>
105 </body>
106 </html>
107 """ % (ui["doctype"], config.APP_TITLE, config.STATIC_BASE_URL, config.STATIC_BASE_URL, csshtml, debug and "true" or "false", customjs, jshtml, ui["class"], optionsgen.get_options(), div)
108
109 def main(outputdir=".", produce_debug=True):
110 p = os.path.join(outputdir, "static")
111 for x in pages.UIs:
112 if produce_debug:
113 f = open(os.path.join(p, "%sdebug.html" % x), "wb")
114 try:
115 f.write(producehtml(x, debug=True))
116 finally:
117 f.close()
118
119 f = open(os.path.join(p, "%s.html" % x), "wb")
120 try:
121 f.write(producehtml(x, debug=False))
122 finally:
123 f.close()
124
125 if __name__ == "__main__":
126 main()
127