Skip to content

Commit d6e2b4c

Browse files
committed
add sitemap generation templates
1 parent 4cbd837 commit d6e2b4c

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

scripts/sitemap/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3+
{% for page in pages %}
4+
<url>
5+
<loc>{{root_url}}/{{page['url']}}</loc>
6+
<lastmod>{{page['lastmod']}}</lastmod>
7+
<changefreq>{{freq}}</changefreq>
8+
</url>
9+
{% endfor %}
10+
</urlset>

scripts/sitemap_gen.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import os
55
from pathlib import Path
6-
from jinja2 import Template
6+
from jinja2 import Environment, FileSystemLoader
77
from subprocess import check_output
88

99
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
@@ -31,13 +31,17 @@ def sitemap(suffix='.md'):
3131
p = p.with_suffix('.html')
3232
fn = p.as_posix().lower()
3333
page = {}
34-
page['date'] = date
34+
page['lastmod'] = date
3535
page['url'] = fn
3636
pages.append(page)
3737
root_url = 'http://algorithm.yuanbin.me'
38-
template = Template(os.path.join(BASE_DIR, 'sitemap.xml'))
39-
template.render(root_url=root_url, pages=pages)
40-
# print(sitemap_xml)
38+
templates = os.path.join(BASE_DIR, 'sitemap' + os.sep + 'templates')
39+
env = Environment(loader=FileSystemLoader(templates))
40+
template = env.get_template('sitemap.xml')
41+
sitemap_xml = template.render(root_url=root_url, pages=pages, freq='daily')
42+
sitemap_fn = os.path.join(ROOT_DIR, 'sitemap.xml')
43+
with open(sitemap_fn, 'w') as sf:
44+
sf.write(sitemap_xml)
4145

4246

4347
if __name__ == "__main__":

0 commit comments

Comments
 (0)