-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.py
64 lines (48 loc) ยท 1.45 KB
/
sitemap.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
from pathlib import Path
import git
HOME = Path('./labml_nn')
REPO = git.Repo('.')
def collect(path: Path):
if path.is_file():
try:
commit = next(iter(REPO.iter_commits(paths=path)))
except StopIteration:
return []
html = path.relative_to(HOME)
if html.suffix not in {'.py'}:
return []
if html.stem == '__init__':
html = html.parent / 'index.html'
else:
html = html.parent / f'{html.stem}.html'
return [{'path': str(html), 'date': str(commit.committed_datetime.date())}]
urls = []
for f in path.iterdir():
urls += collect(f)
return urls
def main():
urls = []
for f in HOME.iterdir():
urls += collect(f)
urls = [f'''
<url>
<loc>https://nn.labml.ai/{u['path']}</loc>
<lastmod>{u['date']}T16:30:00+00:00</lastmod>
<priority>1.00</priority>
</url>
''' for u in urls]
urls = '\n'.join(urls)
xml = f'''
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
{urls}
</urlset>
'''
with open(str(HOME.parent / 'docs' / 'sitemap.xml'), 'w') as f:
f.write(xml)
if __name__ == '__main__':
main()