summaryrefslogtreecommitdiff
path: root/src/pyssg/sitemap.py
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2021-06-04 22:13:44 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2021-06-04 22:13:44 -0600
commit11b9e8505b8a92555cce6615f19268a620e9ce53 (patch)
treee39ce9da68045d7aa6c5cde7f09e2b5185262d18 /src/pyssg/sitemap.py
parent30607ae87ed36289d860a41f8027900bebeec1e8 (diff)
refactor and add support for jinija
Diffstat (limited to 'src/pyssg/sitemap.py')
-rw-r--r--src/pyssg/sitemap.py54
1 files changed, 16 insertions, 38 deletions
diff --git a/src/pyssg/sitemap.py b/src/pyssg/sitemap.py
index 410033f..f63a7e6 100644
--- a/src/pyssg/sitemap.py
+++ b/src/pyssg/sitemap.py
@@ -1,60 +1,38 @@
import os
+from jinja2 import Environment, Template
+from importlib.metadata import version
from datetime import datetime, timezone
from .page import Page
from .configuration import Configuration
-
+VERSION = version('pyssg')
+# specific format for sitemap (not as strict)
DFORMAT = '%Y-%m-%d'
+RUN_DATE = datetime.now(tz=timezone.utc).strftime(DFORMAT)
class SitemapBuilder:
def __init__(self, config: Configuration,
- template: str,
+ env: Environment,
pages: list[Page],
tags: list[str]):
self.config: Configuration = config
- self.sitemap: str = template
+ self.env: Environment = env
self.pages: list[Page] = pages
self.tags: list[str] = tags
def build(self):
- # initial base replacements
- urls_formatted: str = self.__get_urls_formatted()
- self.sitemap = self.sitemap.replace('$$URLS', urls_formatted)
+ template: Template = self.env.get_template("sitemap.xml")
+ content: str = template.render(site_title=self.config.title,
+ site_base_url=self.config.base_url,
+ site_base_static_url=self.config.base_static_url,
+ pyssg_version=VERSION,
+ run_date=RUN_DATE,
+ all_pages=self.pages,
+ all_tags=self.tags)
with open(os.path.join(self.config.dst, 'sitemap.xml'), 'w') as f:
- f.write(self.sitemap)
-
-
- def __get_urls_formatted(self) -> str:
- # u_f=items formatted for short
- u_f: str = ''
- for p in self.pages:
- url: str = f'{self.config.base_url}/{p.name.replace(".md", ".html")}'
- if p.m_datetime is not None:
- date: str = p.m_datetime.strftime(DFORMAT)
- else:
- date: str = p.c_datetime.strftime(DFORMAT)
-
- u_f = f'{u_f} <url>\n'
- u_f = f'{u_f} <loc>{url}</loc>\n'
- u_f = f'{u_f} <lastmod>{date}</lastmod>\n'
- u_f = f'{u_f} <changefreq>weekly</changefreq>\n'
- u_f = f'{u_f} <priority>1.0</priority>\n'
- u_f = f'{u_f} </url>\n'
-
- for t in self.tags:
- url: str = f'{self.config.base_url}/tag/@{t}.html'
- date: str = datetime.now(tz=timezone.utc).strftime(DFORMAT)
-
- u_f = f'{u_f} <url>\n'
- u_f = f'{u_f} <loc>{url}</loc>\n'
- u_f = f'{u_f} <lastmod>{date}</lastmod>\n'
- u_f = f'{u_f} <changefreq>daily</changefreq>\n'
- u_f = f'{u_f} <priority>0.5</priority>\n'
- u_f = f'{u_f} </url>\n'
-
- return u_f
+ f.write(content)