Skip to content

Instantly share code, notes, and snippets.

@zaibacu
Last active August 29, 2015 13:57
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 zaibacu/9412574 to your computer and use it in GitHub Desktop.
Save zaibacu/9412574 to your computer and use it in GitHub Desktop.
Compiles single html file with all script, stylesheet includes to a single encrypted file
import re
import codecs
import base64
def load_js(src):
stream = ""
with codecs.open(src, encoding="utf-8", mode="r") as f:
stream = f.read()
return "<script>" + stream + "</script>"
def load_css(src):
stream = ""
with codecs.open(src, encoding="utf-8", mode="r") as f:
stream = f.read()
return "<style>\n"+ stream + "\n</style>"
def dependency_walk(root_file):
with open(root_file) as f:
stream = f.read()
#Handle scripts
while True:
scripts = re.compile("(<script (type=\"text/javascript\")? src=\")(?P<url>[a-z./]+)\"></script>")
result = scripts.search(stream)
if not result:
break
stream = stream.replace(result.group(0), load_js(result.group("url")))
#Handle style
while True:
styles = re.compile("<link href=\"(?P<url>[a-z./]+)\" rel=\"stylesheet\">")
result = styles.search(stream)
if not result:
break
stream = stream.replace(result.group(0), load_css(result.group("url")))
return stream
def encrypter(stream):
return base64.b64encode(stream.encode("utf-8")).decode("utf-8")
print("reading")
html = dependency_walk("index.html")
print("encrypting")
html = encrypter(html)
print("writing")
with codecs.open("data.bin", encoding="utf-8", mode="w") as f:
f.write(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment