Using jinja2
with pip3 install jinja2
https://pypi.org/project/Jinja2/
With list of links generated from Pelican's folder with Markdown files. Could be done on the back of the output folder with HTML files too.
####################
# Generate sitemap for notes.nicolasdeville.com
from pathlib import Path
test = False
v = True
## Generate links
links = ['https://notes.nicolasdeville.com/', ]
for root, dirs, files in os.walk("/path/to/my/articles"):
for file in files:
if file.endswith((".md")):
count += 1
full_path = f"{root}/{file}"
parent_folder = Path(full_path).parts[-2:-1][0]
if ' ' in parent_folder:
parent_folder = parent_folder.replace(' ', '-')
article_path = f"{file[:-3]}"
if v:
print(f"{root=}")
print(f"{dirs=}")
print(f"{files=}")
print(f"{file=}")
print(f"{full_path=}")
print(f"{parent_folder=}")
print(f"{article_path=}")
link = f"https://notes.nicolasdeville.com/{parent_folder}/{article_path}"
if not any(ele in link for ele in blacklist):
links.append(link)
print(f"{link=}")
## Generate sitemap.xml
sitemap_template='''<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for link in links %}
<url>
<loc>{{ link['link'] }}</loc>
<lastmod>{{ link['lastmod'] }}</lastmod>
<priority>{{ link['priority'] }}</priority>
</url>
{% endfor %}
</urlset>'''
template_sitemap = Template(sitemap_template)
lastmod_date = datetime.now().strftime('%Y-%m-%d')
list_meta_urls = []
for link in links:
list_meta_urls.append({'link': link, 'lastmod': lastmod_date, 'priority': '0.8'})
# Render each row / column in the sitemap
sitemap_output = template_sitemap.render(links=list_meta_urls)
sitemap_filename = f"/path/to/folder/sitemap.xml"
# Write the File to Your Working Folder
with open(sitemap_filename, 'wt') as f:
f.write(sitemap_output)
print(f"\nSitemap generated at {sitemap_filename}")