]> git.phdru.name Git - phdru.name/phdru.name.git/blob - gen-sitemap.py
Fix(phd.py): Change URL for Lurk
[phdru.name/phdru.name.git] / gen-sitemap.py
1 #! /usr/bin/env python
2
3 import argparse
4 from fnmatch import fnmatch
5 import os
6 from news import write_if_changed
7
8 parser = argparse.ArgumentParser(description='Generate sitemap')
9 parser.add_argument('-x', '--exclude', action='append',
10                     help='exclude directories (pattern)')
11 parser.add_argument('root_dir', help='Root dicrectory')
12 args = parser.parse_args()
13
14 exclude = []
15 if args.exclude:
16     for pat in args.exclude:
17         exclude.append(pat)
18         if not pat.endswith('/*'):
19             exclude.append(pat + '/*')
20
21 os.chdir(args.root_dir)
22 fullpath = os.getcwd()
23 fp_len = len(fullpath)
24
25 tree = ('', [])
26 current_subtree = tree[1]
27 save_level = 0
28
29 for dirpath, dirs, files in sorted(os.walk(fullpath)):
30     try:
31         dirpath = dirpath[fp_len:]
32         if not dirpath:
33             continue
34         for pat in exclude:
35             if fnmatch(dirpath, pat):
36                 raise StopIteration
37         parts = dirpath.split('/')
38         level = len(parts) - 2
39         if level < save_level:
40             current_subtree = tree[1]
41             for i in range(level):
42                 current_subtree = current_subtree[-1][1]
43         elif level > save_level:
44             assert level - save_level == 1
45             current_subtree = current_subtree[-1][1]
46         #else: # level == save_level:
47         current_subtree.append((parts[-1], []))
48         save_level = level
49     except StopIteration:
50         pass
51
52 def _tree2html(tree, path='', level=0):
53     subparts = []
54     indent = "  " * (level + 1)
55     for title, subtree in tree:
56         subpath = "%s/%s" % (path, title)
57         if subpath.startswith('/'):
58             subpath = subpath[1:]
59         href = '<a href="%s/">%s</a>' % (subpath, title)
60         if subtree:
61             subparts.append(indent + "<li>%s" % href)
62             subparts.append(_tree2html(subtree, subpath, level+2))
63             subparts.append(indent + "</li>")
64         else:
65             subparts.append(indent + "<li>%s</li>" % href)
66     s = "\n".join(subparts)
67
68     parts = []
69     indent = "  " * level
70     parts.append(indent + "<ul>")
71     parts.append(s)
72     parts.append(indent + "</ul>")
73
74     return "\n".join(parts)
75
76 sitemap_tmpl = ["""\
77 #extends phd_site
78 #implements respond
79 #attr $Title = 'Sitemap'
80 #attr $Copyright = 2015
81 ##
82 #def body_html
83 """]
84
85 sitemap_tmpl.append(_tree2html(tree[1]))
86
87 sitemap_tmpl.append("""
88 #end def
89 $phd_site.respond(self)
90 """)
91
92 write_if_changed("sitemap.tmpl", ''.join(sitemap_tmpl))