Add site map
This commit is contained in:
6
src/.buildinfo.json
Executable file
6
src/.buildinfo.json
Executable file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"tag": "jc-ng-localtest:",
|
||||
"date": "2025-06-15",
|
||||
"host": "jake-e580",
|
||||
"user": "jake"
|
||||
}
|
@@ -12,6 +12,7 @@ app = Flask(__name__)
|
||||
# These imports need to come after our app is defined as they add routes to it.
|
||||
import projects # pylint: disable=wrong-import-position,unused-import
|
||||
import contact # pylint: disable=wrong-import-position,unused-import
|
||||
import sitemap # pylint: disable=wrong-import-position,unused-import
|
||||
|
||||
class DiscordLogger(logging.Handler):
|
||||
''' Simple logging handler to send a message to Discord '''
|
||||
|
@@ -45,7 +45,7 @@ def to_html(content: str) -> str:
|
||||
''' Jninja filter to wrap markdown '''
|
||||
return markdown(content)
|
||||
|
||||
def get_all_posts(directory: str) -> list:
|
||||
def get_all_posts(directory: str = md_directory) -> list:
|
||||
''' Get all posts in the posts directory '''
|
||||
abs_paths = [path.join(directory, x) for x in glob(f'{directory}/*.md')]
|
||||
return [frontmatter.load(x) for x in abs_paths]
|
||||
|
@@ -4,3 +4,6 @@ import sys
|
||||
sys.path.append('/var/www/jc')
|
||||
|
||||
from index import app as application
|
||||
|
||||
if __name__ == '__main__':
|
||||
application.run(debug=True)
|
42
src/sitemap.py
Executable file
42
src/sitemap.py
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
import json
|
||||
from flask import url_for, request, Response
|
||||
from re import match
|
||||
from index import app
|
||||
from projects import get_all_posts
|
||||
|
||||
def get_routes() -> list:
|
||||
routes = []
|
||||
for rule in app.url_map.iter_rules():
|
||||
if 0 >= len(rule.arguments):
|
||||
url = url_for(rule.endpoint, **(rule.defaults or {}))
|
||||
routes.append(url)
|
||||
return routes
|
||||
|
||||
def get_build_date():
|
||||
try:
|
||||
with open('/var/www/jc/.buildinfo.json', encoding='utf8') as build:
|
||||
build_json = json.load(build)
|
||||
return build_json['date']
|
||||
except:
|
||||
return '1970-01-01'
|
||||
|
||||
@app.route('/sitemap.xml')
|
||||
def sitemap():
|
||||
date = get_build_date()
|
||||
root = ET.Element('urlset', xmlns='http://www.sitemaps.org/schemas/sitemap/0.9')
|
||||
base_url = match(r'^https?:\/\/.+:?\d*(?=\/)', request.base_url).group()
|
||||
for route in get_routes():
|
||||
url = ET.SubElement(root, 'url')
|
||||
ET.SubElement(url, 'loc').text = base_url + route
|
||||
ET.SubElement(url, 'lastmod').text = date
|
||||
for article in get_all_posts():
|
||||
if 'link' in article.metadata:
|
||||
continue
|
||||
url = ET.SubElement(root, 'url')
|
||||
ET.SubElement(url, 'loc').text = f'{base_url}/projects/{article.metadata['id']}'
|
||||
ET.SubElement(url, 'lastmod').text = article.metadata['date'].strftime('%Y-%m-%d')
|
||||
|
||||
return Response(ET.tostring(root, encoding='utf-8'), 200, {'content-type': 'application/xml'})
|
4
src/static/robots.txt
Executable file
4
src/static/robots.txt
Executable file
@@ -0,0 +1,4 @@
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
Sitemap: https://jakecharman.co.uk/sitemap.xml
|
Reference in New Issue
Block a user