Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created June 4, 2010 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/425746 to your computer and use it in GitHub Desktop.
Save mahemoff/425746 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Quick edit of FND's http://github.com/FND/tiddlywebadmin/blob/master/spa.py
# mahemoff - added support for remote resources and uglied up RegExps to make them a # bit more general
"""
single-page app compiler
uses references to local JavaScript and CSS files and bakes their code into an
HTML5 document
Usage:
spa.py <filename>
"""
import sys
import os
import re
import urllib
from base64 import b64encode
def main(args):
args = [unicode(arg, "utf-8") for arg in args]
filename = args[1]
patterns = { # TODO: support arbitrary attributes (e.g. img's alt)
"js": re.compile(r'<script[^>]*src=[\'"](.*?)[\'"][^>]*></script>'),
"css": re.compile(r'<link[^>]*rel=[\'"]stylesheet[\'"][^>]*href="(.*?)[\'"][^>]*>'),
"img": re.compile(r'<img[^>]*src=[\'"](.*?)[\'"][^>]*>')
}
original = _readfile(filename)
references = []
for type in patterns.keys():
refs = patterns[type].findall(original)
references.extend(refs)
resources = {}
for uri in references:
if uri.startswith("http://") and not uri.startswith("https://"):
resources[uri] = urllib.urlopen(uri).read()
else:
filepath = os.sep.join(uri.split("/"))
binary = not (uri.endswith(".js") or uri.endswith(".css")) # XXX: special-casing
resources[uri] = _readfile(filepath, binary)
spa = []
for line in original.splitlines():
for uri, code in resources.items():
if uri in line:
if uri.endswith(".js"):
template = "<script>\n%s\n</script>"
elif uri.endswith(".css"):
template = "<style>\n%s\n</style>"
else: # image
ext = uri.rsplit(".", 1)[1]
template = '<img src="data:image/%s;base64,%s">' % (ext, "%s")
code = b64encode(code)
line = template % code
spa.append(line)
filename = filename.replace(".html", ".spa.html")
f = open(filename, "w")
f.write("\n".join(spa))
f.close()
print "converted %s resources: %s" % (len(resources), filename)
return True
def _readfile(filepath, binary=False):
mode = "rb" if binary else "r"
f = open(filepath, mode)
content = f.read()
f.close()
return content
def _readhttp(filepath, binary=False):
mode = "rb" if binary else "r"
f = open(filepath, mode)
content = f.read()
f.close()
return content
if __name__ == "__main__":
status = not main(sys.argv)
sys.exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment