Skip to content

Instantly share code, notes, and snippets.

@jhancock532
Created March 17, 2022 16:27
Show Gist options
  • Save jhancock532/bee65514f964032f223a3d8635e41ad5 to your computer and use it in GitHub Desktop.
Save jhancock532/bee65514f964032f223a3d8635e41ad5 to your computer and use it in GitHub Desktop.
Sitemap Generator for Django Pattern Library Pages
# Please adapt the file paths according to your project conventions.
import glob
import re
base_url = "http://localhost:8000/pattern-library/render-pattern/patterns"
# /render-pattern/ loads the pattern in full page view without the pattern library UI
pattern_libray_pages = glob.glob('**/patterns/pages/**/*.html', recursive=True)
sitemap = """<?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">
"""
for html_file in pattern_libray_pages:
# removes all characters in the file path up to and including "patterns"
url = base_url + re.sub(r'^.*?patterns', '', html_file)
sitemap += " <url>\n <loc>"
sitemap += url
sitemap += "</loc>\n </url>\n\n"
sitemap += "</urlset>"
f = open("sitemap.xml", "w")
f.write(sitemap)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment