summaryrefslogtreecommitdiff
path: root/src/pyssg/sitemap.py
blob: 410033feb8bd1e216fbd7975733b1fcd19c8d4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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")}'
            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