#!/usr/bin/env python """Turn a website into an HTML5 application This turns a directory into the simplest possible offline web application, by making a manifest containing every file in the directory, then inserting it into each HTML document. For more information about the HTML5 application cache, see: - https://html.spec.whatwg.org/multipage/browsers.html#offline - https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache """ import os import re import time def is_html(filename): return filename.lower().endswith(".html") def main(appdir): if not os.path.isdir(appdir): raise StandardError("input (%r) is not a directory" % appdir) all_files = [] for root, dirnames, filenames in os.walk(appdir): root = os.path.relpath(root, appdir) for filename in filenames: all_files.append(os.path.join(root, filename)) all_files.sort() appcache = os.path.join(appdir, "manifest.appcache") with open(appcache, "w") as fobj: fobj.write("CACHE MANIFEST\n") fobj.write("# Generated on %s\n" % time.strftime("%Y %T %z")) for filename in all_files: fobj.write(filename + "\n") for filename in filter(is_html, all_files): filename = os.path.join(appdir, filename) # This call to relpath is the entire reason this tool is in # Python and not a shell script. relpath = os.path.relpath(appcache, os.path.dirname(filename)) html = open(filename).read() html = re.sub(" ])", '