-
Notifications
You must be signed in to change notification settings - Fork 31
/
build-menu.py
140 lines (116 loc) · 4.44 KB
/
build-menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
# coding=utf-8
from collections import OrderedDict
from translation_tools.translation_utils import LOCALES
import yaml
import sys
if '--publish' in sys.argv:
import publishconf as conf
else:
import pelicanconf as conf
ROOT = conf.SITEURL
SUFFIX = ''
def orderedLoad(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
return yaml.load(stream, OrderedLoader)
def isseparator(pagename):
for ch in pagename:
if ch != '_':
return False
return True
def build_menu(d, lvl=1):
l = []
for pagename, entry in d.items():
if pagename.startswith('<nodoc>'):
print(f'ignoring {pagename}')
continue
if pagename.startswith('__'):
pagename = pagename[2:]
if isseparator(pagename):
l.append('</ul></li>\n<li class="col-sm-3"><ul>')
continue
if entry is None:
l.append('%s<li class="dropdown-header dropdown-header-level-%d">%s</li>' \
% ('\t'*lvl, lvl, pagename))
continue
if isinstance(entry, dict):
if lvl == 1:
l.append(
('<li class="dropdown mega-dropdown">'
'<a href="#" class="dropdown-toggle level-%d" data-toggle="dropdown">'
'%s <span class="glyphicon glyphicon-menu-down"></span></a>') \
% (lvl+1, pagename))
l.append('<ul class="dropdown-menu mega-dropdown-menu row"><li class="col-sm-3"><ul>')
l.append(build_menu(entry, lvl+1))
l.append('</ul></li></ul></li>')
else:
l.append('%s<li class="dropdown-header dropdown-header-level-%d">%s</li>' \
% ('\t'*(lvl+1), lvl+1, pagename))
l.append(build_menu(entry, lvl+1))
continue
if entry.startswith('http'):
l.append('%s<li class="level-%d"><a href="%s">%s</a></li>' \
% ('\t'*lvl, lvl, entry, pagename))
else:
l.append('%s<li class="level-%d"><a href="%s/%s%s">%s</a></li>' \
% ('\t'*lvl, lvl, ROOT, entry, SUFFIX, pagename))
return '\n'.join(l)
def build_seo_sitemap(d):
sitemap = []
for pagename, entry in d.items():
if isseparator(pagename) or entry in [None, '']:
continue
if isinstance(entry, dict):
sitemap += build_seo_sitemap(entry)
continue
if not entry.startswith('http'):
sitemap.append(ROOT + '/' + entry + SUFFIX)
return sitemap
def build_live_sitemap(d):
sitemap = OrderedDict()
for pagename, entry in d.items():
if isinstance(entry, list):
cls = entry[1]
entry = entry[0]
else:
cls = ''
if isseparator(pagename) or entry in [None, '']:
continue
if isinstance(entry, dict):
sitemap[pagename] = build_live_sitemap(entry)
continue
if entry.startswith('http'):
sitemap[pagename] = entry
else:
sitemap[pagename] = '/' + conf.BRANCH + '/' + entry + SUFFIX
return sitemap
def main():
with open('sitemap/sitemap.yaml') as f:
d = orderedLoad(f)
with open('themes/cogsci/templates/mega-menu-content.html', 'w') as f:
f.write(build_menu(d))
print('Generated menu content')
sitemap = build_live_sitemap(d)
with open('static/sitemap.yml', 'w') as fd:
yaml.dump(sitemap, fd, default_flow_style=False)
print('Generated live sitemap')
with open('static/seo-sitemap.txt', 'w') as fd:
fd.write('\n'.join(build_seo_sitemap(d)) + '\n')
print('Generated seo sitemap')
for language, code in LOCALES:
print(f'Generated locale sitemap for {code}')
with open(f'sitemap/sitemap-{code}.yaml') as f:
d = orderedLoad(f)
with open(f'themes/cogsci/templates/mega-menu-content-{code}.html', 'w') as f:
f.write(build_menu(d))
with open(f'static/seo-sitemap-{code}.txt', 'w') as fd:
fd.write('\n'.join(build_seo_sitemap(d)) + '\n')
if __name__ == '__main__':
main()