9a03981fc5cfe9f27464029cc13969890796d41e
3 """Turn a website into an HTML5 application
6 This turns a directory into the simplest possible offline web
7 application, by making a manifest containing every file in the
8 directory, then inserting it into each HTML document.
10 For more information about the HTML5 application cache, see:
12 - https://html.spec.whatwg.org/multipage/browsers.html#offline
13 - https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache
22 def is_html(filename
):
23 return filename
.lower().endswith(".html")
27 if not os
.path
.isdir(appdir
):
28 raise StandardError("input (%r) is not a directory" % appdir
)
30 for root
, dirnames
, filenames
in os
.walk(appdir
):
31 root
= os
.path
.relpath(root
, appdir
)
32 for filename
in filenames
:
33 all_files
.append(os
.path
.join(root
, filename
))
35 appcache
= os
.path
.join(appdir
, "manifest.appcache")
36 with
open(appcache
, "w") as fobj
:
37 fobj
.write("CACHE MANIFEST\n")
38 fobj
.write("# Generated on %s\n" % time
.strftime("%Y %T %z"))
39 for filename
in all_files
:
40 fobj
.write(filename
+ "\n")
42 for filename
in filter(is_html
, all_files
):
43 filename
= os
.path
.join(appdir
, filename
)
44 # This call to relpath is the entire reason this tool is in
45 # Python and not a shell script.
46 relpath
= os
.path
.relpath(appcache
, os
.path
.dirname(filename
))
47 html
= open(filename
).read()
48 html
= re
.sub("<html([> ])", '<html manifest="%s"\\1' % relpath
, html
)
49 with
open(filename
, "w") as fobj
:
52 if __name__
== "__main__":
57 raise SystemExit("Usage: %s appdir" % sys
.argv
[0])