+
+def _tree2html(tree, path='', level=0):
+ subparts = []
+ indent = " " * (level + 1)
+ for title, subtree in tree:
+ subpath = "%s/%s" % (path, title)
+ if subpath.startswith('/'):
+ subpath = subpath[1:]
+ href = '<a href="%s/">%s</a>' % (subpath, title)
+ if subtree:
+ subparts.append(indent + "<li>%s" % href)
+ subparts.append(_tree2html(subtree, subpath, level+2))
+ subparts.append(indent + "</li>")
+ else:
+ subparts.append(indent + "<li>%s</li>" % href)
+ 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))