diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2021-05-25 21:21:20 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2021-05-25 21:21:20 -0600 |
commit | 2c4afde1c93bbf3990514c45b06b63e648c46f63 (patch) | |
tree | f7497bf7885543b4d59ac7cafeb5012e8c8930c8 /src/pyssg/sitemap.py | |
parent | a9d5d971961bd28f2d6275aabfa78f2fd268b851 (diff) |
add sitemap support
Diffstat (limited to 'src/pyssg/sitemap.py')
-rw-r--r-- | src/pyssg/sitemap.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/pyssg/sitemap.py b/src/pyssg/sitemap.py new file mode 100644 index 0000000..e8338cb --- /dev/null +++ b/src/pyssg/sitemap.py @@ -0,0 +1,57 @@ +import os +from datetime import datetime, timezone + +from .page import Page +from .configuration import Configuration + + +DFORMAT = '%Y-%m-%d' + + +class SitemapBuilder: + def __init__(self, config: Configuration, + template: str, + pages: list[Page], + tags: list[str]): + self.config: Configuration = config + self.sitemap: str = template + 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) + + + 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")}' + date: str = p.m_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 |