]> git.phdru.name Git - phdru.name/phdru.name.git/blobdiff - gen-sitemap.py
gen-sitemap: refactoring
[phdru.name/phdru.name.git] / gen-sitemap.py
index b6ab57d8be254160c17af11d82e1fe3282e26498..aaff97a7c3001f77707b32876da324d3bc20b73a 100755 (executable)
@@ -3,6 +3,7 @@
 import argparse
 from fnmatch import fnmatch
 import os
+from news import write_if_changed
 
 parser = argparse.ArgumentParser(description='Generate sitemap')
 parser.add_argument('-x', '--exclude', action='append',
@@ -46,3 +47,41 @@ for dirpath, dirs, files in sorted(os.walk(fullpath)):
         save_level = level
     except StopIteration:
         pass
+
+def _tree2html(tree, level=0):
+    subparts = []
+    indent = "  " * (level + 1)
+    for title, subtree in tree:
+        if subtree:
+            subparts.append(indent + "<li>%s" % title)
+            subparts.append(_tree2html(subtree, level+2))
+            subparts.append(indent + "</li>")
+        else:
+            subparts.append(indent + "<li>%s</li>" % title)
+    s = "\n".join(subparts)
+
+    parts = []
+    indent = "  " * level
+    parts.append(indent + "<ul>")
+    parts.append(s)
+    parts.append(indent + "</ul>")
+
+    return "\n".join(parts)
+
+sitemap_tmpl = ["""\
+#extends phd_site
+#implements respond
+#attr $Title = 'Sitemap'
+#attr $Copyright = 2015
+##
+#def body_html
+"""]
+
+sitemap_tmpl.append(_tree2html(tree[1]))
+
+sitemap_tmpl.append("""
+#end def
+$phd_site.respond(self)
+""")
+
+write_if_changed("sitemap.tmpl", ''.join(sitemap_tmpl))