-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_embed.py
More file actions
executable file
·51 lines (45 loc) · 1.55 KB
/
Copy path_embed.py
File metadata and controls
executable file
·51 lines (45 loc) · 1.55 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Project:
# Module:
import cgi, sys
from xml.dom.minidom import parseString
def log(msg): sys.stderr.write(str(msg) + "\n")
class TemplateProcessor(object):
def process(self, filename):
js_files = []
buf = ""
chk = 0
fh = open(filename, "r")
for line in fh:
if line.strip().startswith("<script ") and line.strip().endswith("</script>"):
el = parseString(line)
if el.documentElement.getAttribute("embed"):
js_files.append(el.documentElement.getAttribute("src"))
if not chk:
chk = line.find("<script ")
buf += " " * chk + '<script type="text/javascript">\n'
buf += self._load_external(js_files[-1])
continue
if chk:
buf += " " * chk + "</script>\n"
chk = 0
buf += line
return buf
def _load_external(self, filename):
fh = open(filename, "r")
buf = ""
for line in fh: buf += "\t" + line
fh.close()
return buf
def application(environ, start_response):
status = "200 OK"
headers = [("Content-type", "text/html; charset=utf-8")]
start_response(status, headers)
form = cgi.FieldStorage()
tmpl = form["html"].value + ".html"
processor = TemplateProcessor()
return [processor.process(tmpl)]
if __name__ == "__main__":
from wsgiref import handlers
handlers.CGIHandler().run(application)