Skip to content

Commit

Permalink
sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
linesinalandscape committed Dec 5, 2024
1 parent 64cd16b commit ad8bcf5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
57 changes: 43 additions & 14 deletions ssg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'''
TODO
- all date related stuff
- 404
- production permalinks
- remove jekyll from footer
Expand All @@ -18,11 +19,15 @@

from shutil import copytree, rmtree
from pathlib import Path
import datetime as dt
import markdown

SOURCE_DIR = Path('content')
BUILD_DIR = Path('build')
TEMPLATE_FILE = Path('templates/default.html')
SITEMAP = Path('build/sitemap.xml')
TEMPLATE_HTML = Path('templates/default.html')
TEMPLATE_SITEMAP = Path('templates/sitemap.xml')
TEMPLATE_SITEMAP_ITEM = Path('templates/sitemap_item.xml')

# sitewide metadata for use in the template
SITE_META = {
Expand All @@ -35,21 +40,18 @@
rmtree(BUILD_DIR, ignore_errors=True)
copytree(SOURCE_DIR, BUILD_DIR)

# Load the template
template = TEMPLATE_FILE.read_text(encoding='utf-8')
# Load the layout templates
layout_html = TEMPLATE_HTML.read_text(encoding='utf-8')
layout_sitemap = TEMPLATE_SITEMAP.read_text(encoding='utf-8')
layout_sitemap_item = TEMPLATE_SITEMAP_ITEM.read_text(encoding='utf-8')

# make a list of files with .md extension in the build directory and subfolders
md_files = list(BUILD_DIR.rglob('*.md'))
sitemap_items = ''

print('Writing HTML files converted from markdown...')

for md_file in md_files:

# set the final url for the page
permalink = (SITE_META.get('site_url')
+ str(md_file.relative_to(BUILD_DIR).parent) + '/')
permalink = permalink.replace('\\', '/')
permalink = permalink.replace('./', '') # for root index page

# read the markdown file
content = md_file.read_text(encoding='utf-8')
Expand All @@ -58,20 +60,39 @@
md = markdown.Markdown(extensions=["meta"])
html = md.convert(content)
page_meta = md.Meta

# add permalink to page:meta
page_meta['permalink'] = [permalink]

# Insert the HTML content into the template
output = template.replace('{{ content }}', html)
output = layout_html.replace('{{ content }}', html)

# various metadata fields not already in the markdown file
# set the final url for the page
permalink = (SITE_META.get('site_url')
+ str(md_file.relative_to(BUILD_DIR).parent) + '/')
permalink = permalink.replace('\\', '/')
permalink = permalink.replace('./', '') # for root index page
page_meta['permalink'] = permalink

# set date for the page in priority order:
# date updated in metadata, date in metadata, file modified date
#date_file = dt.datetime.fromtimestamp(md_file.stat().st_mtime)
# convert to human readable date in format YYYY-MM-DD
# date_final = dt.datetime.strptime(date_file, '%Y-%m-%d')
# date_final = ''
# if page_meta.get('date_updated'):
# date_final = page_meta.get('date_updated')[0]
# elif page_meta.get('date'):
# date_final = page_meta.get('date')[0]
# page_meta['date_final'] = date_final

# insert the site metadata into the template
for key in SITE_META:
output = output.replace('{{ ' + key + ' }}', SITE_META.get(key))

# insert the page metadata into the template
# insert the page metadata into the templates
for key in md.Meta:
output = output.replace('{{ ' + key + ' }}', page_meta.get(key)[0])
sitemap_item = (layout_sitemap_item.replace('{{ ' + key + ' }}',
page_meta.get(key)[0]))

# reformat internal links
output = output.replace('\index.md', '/')
Expand All @@ -87,4 +108,12 @@

print(output_file)

# Add the page to the sitemap
sitemap_items += sitemap_item

# Generate the sitemap
sitemap_output = layout_sitemap.replace('{{ content }}', sitemap_items)
SITEMAP.write_text(sitemap_output, encoding='utf-8')
print()
print('Sitemap written to', SITEMAP)
print('Site generation finished')
4 changes: 4 additions & 0 deletions templates/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset 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" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ content }}
</urlset>
4 changes: 4 additions & 0 deletions templates/sitemap_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ date_final }}</lastmod>
</url>

0 comments on commit ad8bcf5

Please sign in to comment.