#!/usr/bin/env python # Turn a website into an HTML5 application with an application cache # manifest. # # http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html # https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache import os import re import shutil 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(" ])", '