]> jfr.im git - irc/quakenet/qwebirc.git/blob - bin/pagegen.py
Merge.
[irc/quakenet/qwebirc.git] / bin / pagegen.py
1 import os, sys, pages, subprocess, re, optionsgen, config
2
3 class HGException(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 hgid = ""
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 hgid = "-" + gethgid()
15
16 return list(y if y.startswith("//") else "js/%s%s.js" % (y, hgid) for y in pages.flatten(x))
17
18 def csslist(name, debug, gen=False):
19 ui = pages.UIs[name]
20 nocss = ui.get("nocss")
21 if not debug:
22 return ["css/%s-%s.css" % (name, gethgid())]
23 css = pages.flatten([ui.get("extracss", []), "colours", "dialogs"])
24 if not nocss:
25 css = list(css) + [name]
26 return list("css/%s%s.css" % ("debug/" if gen else "", x) for x in css)
27
28 def _gethgid():
29 try:
30 p = subprocess.Popen(["hg", "id"], stdout=subprocess.PIPE, shell=os.name == "nt")
31 except Exception, e:
32 if hasattr(e, "errno") and e.errno == 2:
33 raise HGException, "unable to execute"
34 raise HGException, "unknown exception running hg: %s" % repr(e)
35
36 data = p.communicate()[0]
37 if p.wait() != 0:
38 raise HGException, "unable to get id"
39 return re.match("^([0-9a-f]+).*", data).group(1)
40
41 HGID = None
42 def gethgid():
43 global HGID
44 if HGID is None:
45 try:
46 HGID = _gethgid()
47 except HGException, e:
48 print >>sys.stderr, "warning: hg: %s (using a random id)." % e
49 HGID = os.urandom(10).encode("hex")
50 return HGID
51
52 def producehtml(name, debug):
53 ui = pages.UIs[name]
54 js = jslist(name, debug)
55 css = csslist(name, debug, gen=True)
56 csshtml = "\n".join(" <link rel=\"stylesheet\" href=\"%s%s\" type=\"text/css\"/>" % (config.STATIC_BASE_URL, x) for x in css)
57 jshtml = "\n".join(" <script type=\"text/javascript\" src=\"%s%s\"></script>" % ("" if x.startswith("//") else config.STATIC_BASE_URL, x) for x in js)
58
59 if hasattr(config, "ANALYTICS_HTML"):
60 jshtml+="\n" + config.ANALYTICS_HTML
61
62 div = ui.get("div", "")
63 customjs = ui.get("customjs", "")
64
65 return """%s
66 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
67 <head>
68 <base />
69 <title>%s (qwebirc)</title>
70 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
71 <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
72 <meta name="mobile-web-app-capable" content="yes" />
73 <link rel="icon" sizes="192x192" href="%simages/highresicon.png"/>
74 <link rel="shortcut icon" type="image/png" href="%simages/favicon.png"/>
75 %s<script type="text/javascript">QWEBIRC_DEBUG=%s;</script>%s
76 %s
77 <script type="text/javascript">
78 var ui = new qwebirc.ui.Interface("ircui", qwebirc.ui.%s, %s);
79 </script>
80 </head>
81 <body>
82 <div id="ircui">
83 <noscript>
84 <div id="noscript">Javascript is required to use IRC.</div>
85 </noscript>%s
86 </div>
87 </body>
88 </html>
89 """ % (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)
90
91 def main(outputdir=".", produce_debug=True):
92 p = os.path.join(outputdir, "static")
93 for x in pages.UIs:
94 if produce_debug:
95 f = open(os.path.join(p, "%sdebug.html" % x), "wb")
96 try:
97 f.write(producehtml(x, debug=True))
98 finally:
99 f.close()
100
101 f = open(os.path.join(p, "%s.html" % x), "wb")
102 try:
103 f.write(producehtml(x, debug=False))
104 finally:
105 f.close()
106
107 if __name__ == "__main__":
108 main()
109