3 # Turn a website into an HTML5 application with an application cache
6 # http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html
7 # https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache
14 def is_html(filename
):
15 return filename
.lower().endswith(".html")
18 if not os
.path
.isdir(appdir
):
19 raise StandardError("input (%r) is not a directory" % appdir
)
21 for root
, dirnames
, filenames
in os
.walk(appdir
):
22 root
= os
.path
.relpath(root
, appdir
)
23 for filename
in filenames
:
24 all_files
.append(os
.path
.join(root
, filename
))
26 appcache
= os
.path
.join(appdir
, "manifest.appcache")
27 with
open(appcache
, "w") as fobj
:
28 fobj
.write("CACHE MANIFEST\n")
29 fobj
.write("# Generated on %s\n" % time
.strftime("%Y %T %z"))
30 for filename
in all_files
:
31 fobj
.write(filename
+ "\n")
33 for filename
in filter(is_html
, all_files
):
34 filename
= os
.path
.join(appdir
, filename
)
35 # This call to relpath is the entire reason this tool is in
36 # Python and not a shell script.
37 relpath
= os
.path
.relpath(appcache
, os
.path
.dirname(filename
))
38 html
= open(filename
).read()
39 html
= re
.sub("<html([> ])", '<html manifest="%s"\\1' % relpath
, html
)
40 with
open(filename
, "w") as fobj
:
43 if __name__
== "__main__":
48 raise SystemExit("Usage: %s appdir" %(