From 95cb13208dac4dcc32f4d32c987da6834cb6ba75 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Mon, 28 Aug 2023 19:01:52 -0600
Subject: refactor: simplify parser and page code, remove unnecessary debu logs

---
 src/pyssg/builder.py     | 23 ++++++---------
 src/pyssg/md_parser.py   | 26 +++++-----------
 src/pyssg/page.py        | 77 +++++++++++-------------------------------------
 src/pyssg/plt/index.html |  8 ++---
 src/pyssg/plt/page.html  |  6 ++--
 src/pyssg/plt/tag.html   |  8 ++---
 6 files changed, 46 insertions(+), 102 deletions(-)

diff --git a/src/pyssg/builder.py b/src/pyssg/builder.py
index dc7bdda..1eec345 100644
--- a/src/pyssg/builder.py
+++ b/src/pyssg/builder.py
@@ -54,7 +54,7 @@ class Builder:
 
         # files and pages are synoyms
         self.all_files: list[Page]
-        self.all_tags: list[tuple[str, str]]
+        self.all_tags: list[str]
         self.common_vars: dict
 
     def build(self) -> None:
@@ -100,12 +100,12 @@ class Builder:
         self.__render_pages(self.dir_cfg['plt'])
 
         if self.dir_cfg['tags']:
-            log.debug('rendering tags for dir "%s"', self.dir_cfg['dir'])
-            create_dir(os.path.join(self.dir_cfg['dst'], 'tag'), True)
+            create_dir(os.path.join(self.dir_cfg['dst'], 'tags'), True)
             if isinstance(self.dir_cfg['tags'], str):
                 self.__render_tags(self.dir_cfg['tags'])
             else:
                 self.__render_tags('tag.html')
+            log.debug('rendered tags for dir "%s"', self.dir_cfg['dir'])
 
         default_plts: dict[str, str] = {'index': 'index.html',
                                         'rss': 'rss.xml',
@@ -158,32 +158,27 @@ class Builder:
         tag_vars: dict = deepcopy(self.common_vars)
         tag_pages: list[Page]
         for t in self.all_tags:
-            log.debug('rendering tag "%s"', t[0])
             # clean tag_pages
             tag_pages = []
-            log.debug('adding all pages that contain current tag')
             for p in self.all_files:
-                if p.tags is not None and t[0] in list(map(itemgetter(0),
-                                                           p.tags)):
-                    log.debug('adding page "%s" as it contains tag "%s"',
-                              p.name, t[0])
+                if p.tags is not None and t in p.tags:
                     tag_pages.append(p)
-            log.debug('adding tag and tag_pages to exposed vars for jinja')
+                    log.debug('added page "%s" to tag "%s"', p.name, t)
             tag_vars['tag'] = t
             tag_vars['tag_pages'] = tag_pages
-            t_fname: str = f'tag/@{t[0]}.html'
+            t_fname: str = f'tags/{t}.html'
             # actually render tag page
             self.__render_template(template_name, t_fname, **tag_vars)
+            log.debug('rendered tag "%s"', t)
 
     def __render_template(self, template_name: str,
                           file_name: str,
                           **template_vars) -> None:
-        log.debug('rendering html "%s" with template "%s"',
-                  file_name, template_name)
         template: Template = self.env.get_template(template_name)
         content: str = template.render(**template_vars)
         dst_path: str = os.path.join(self.dir_cfg['dst'], file_name)
 
-        log.debug('writing html file to path "%s"', dst_path)
         with open(dst_path, 'w') as f:
             f.write(content)
+        log.debug('wrote html at "%s"', dst_path)
+
diff --git a/src/pyssg/md_parser.py b/src/pyssg/md_parser.py
index 7056a5b..0c0dd5b 100644
--- a/src/pyssg/md_parser.py
+++ b/src/pyssg/md_parser.py
@@ -75,7 +75,7 @@ class MDParser:
         self.md: Markdown = get_md_obj(self.pymdvar_vars, self.pymdvar_enable_env)
 
         self.all_files: list[Page] = []
-        self.all_tags: list[tuple[str, str]] = []
+        self.all_tags: list[str] = []
 
     def parse_files(self) -> None:
         for i, f in enumerate(self.files):
@@ -98,7 +98,6 @@ class MDParser:
                 else:
                     entry = oentry
             
-            log.debug('parsing md into html')
             # ignoring md.Meta type as it is not yet defined
             #   (because it is from an extension)
             page: Page = Page(f,
@@ -108,35 +107,26 @@ class MDParser:
                               self.md.toc,  # type: ignore
                               self.md.toc_tokens,  # type: ignore
                               self.md.Meta,  # type: ignore
-                              self.config,
-                              self.dir_config)
+                              self.config)
             page.parse_metadata()
-
-            log.debug('adding to file list')
             self.all_files.append(page)
 
             if self.dir_config['tags']:
-                if page.tags is None:
-                    self.db.update_tags(f)
-                else:
-                    tags: tuple = tuple(set(map(itemgetter(0), page.tags)))
-                    if tags != entry[4]:
-                        self.db.update_tags(f, tags)
+                if entry[4] is not None:
+                    if set(page.tags) != set(entry[4]):
+                        self.db.update_tags(f, page.tags)
 
-                log.debug('add all tags to tag list')
                 for t in page.tags:
-                    if t[0] not in list(map(itemgetter(0), self.all_tags)):
+                    if t not in self.all_tags:
                         self.all_tags.append(t)
-                        log.debug('added tag "%s"', t[0])
+                        log.debug('added tag "%s" to all tags', t)
 
-        log.debug('sorting all lists for consistency')
         self.all_files.sort(reverse=True)
-        self.all_tags.sort(key=itemgetter(0))
+        self.all_tags.sort()
 
         pages_amount: int = len(self.all_files)
         # note that prev and next are switched because of the
         # reverse ordering of all_pages
-        log.debug('update next and prev attributes')
         for i, p in enumerate(self.all_files):
             if i != 0:
                 next_page: Page = self.all_files[i - 1]
diff --git a/src/pyssg/page.py b/src/pyssg/page.py
index 26d2655..d39d8ef 100644
--- a/src/pyssg/page.py
+++ b/src/pyssg/page.py
@@ -7,37 +7,31 @@ log: Logger = getLogger(__name__)
 
 class Page:
     def __init__(self, name: str,
-                 ctime: float,
-                 mtime: float,
+                 cts: float,
+                 mts: float,
                  html: str,
                  toc: str,
                  toc_tokens: list[str],
                  meta: dict[str, Any],
-                 config: dict[str, Any],
-                 dir_config: dict[str, Any]) -> None:
-        log.debug('initializing a page object with name "%s"', name)
+                 config: dict[str, Any]) -> None:
         # initial data
-        self.name: str = name
-        self.ctimestamp: float = ctime
-        self.mtimestamp: float = mtime
+        self.name: str = name.replace(".md", ".html")
+        self.cts: float = cts
+        self.mts: float = mts
         self.content: str = html
         self.toc: str = toc
         self.toc_tokens: list[str] = toc_tokens
         self.meta: dict[str, Any] = meta
         self.config: dict[str, Any] = config
-        self.dir_config: dict[str, Any] = dir_config
 
         # data from self.meta
         self.title: str
         self.author: list[str]
         self.summary: str
         self.lang: str
-        self.cdatetime: datetime
-        self.mdatetime: datetime | None = None
-        self.tags: list[tuple[str, str]] = []
+        self.tags: tuple[str]
 
         # constructed
-        self.url: str
         self.cdate_rss: str
         self.cdate_sitemap: str
         self.mdate_rss: str | None = None
@@ -52,7 +46,6 @@ class Page:
     def __get_meta(self, var: str,
                    or_else: str | list[str] = ['']) -> str | list[str] | Any:
         if var in self.meta:
-            log.debug('getting metadata "%s"', var)
             return self.meta[var]
         else:
             log.debug('getting metadata "%s" failed, using optional value "%s"',
@@ -61,7 +54,9 @@ class Page:
 
     # these date/cdate/mdate might be a bit overcomplicated
 
-    def __date(self, dt: datetime, format: str) -> str:
+    def date(self, ts: float, format: str) -> str:
+        dt: datetime = datetime.fromtimestamp(ts, tz=timezone.utc)
+
         if format in self.config['fmt']:
             return dt.strftime(self.config['fmt'][format])
         else:
@@ -69,56 +64,20 @@ class Page:
                         'empty string', format)
             return ''
 
-    def cdate(self, format: str) -> str:
-        return self.__date(self.cdatetime, format)
-
-    def mdate(self, format: str) -> str:
-        if self.mdatetime is None:
-            log.warning('no mdatetime found, can\'t return a formatted string')
-            return ''
-        return self.__date(self.mdatetime, format)
-
-    def from_timestamp(self, timestamp: float) -> datetime:
-        return datetime.fromtimestamp(timestamp, tz=timezone.utc)
-
     # parses meta from self.meta
     def parse_metadata(self):
-        log.debug('parsing metadata for file "%s"', self.name)
         self.title = str(self.__get_meta('title')[0])
         self.author = list(self.__get_meta('author'))
         self.summary = str(self.__get_meta('summary')[0])
         self.lang = str(self.__get_meta('lang', ['en'])[0])
 
-        log.debug('parsing timestamp')
-        self.cdatetime = self.from_timestamp(self.ctimestamp)
-        self.cdate_rss = self.cdate('rss_date')
-        self.cdate_sitemap = self.cdate('sitemap_date')
-
-        if self.mtimestamp != 0.0:
-            log.debug('parsing modified timestamp')
-            self.mdatetime = self.from_timestamp(self.mtimestamp)
-            self.mdate_rss = self.mdate('rss_date')
-            self.mdate_sitemap = self.mdate('sitemap_date')
-        else:
-            log.debug('not parsing modified timestamp, hasn\'t been modified')
-
-        if self.dir_config['tags']:
-            log.debug('parsing tags')
-            tags_only: list[str] = list(self.__get_meta('tags', []))
-            if tags_only:
-                tags_only.sort()
+        # probably add a way to sanitize tags
+        self.tags = tuple(set(sorted(self.__get_meta('tags', []))))
+        log.debug('parsed tags %s', self.tags)
 
-                for t in tags_only:
-                    # need to specify dir_config['url'] as it is
-                    #   a hardcoded tag url
-                    tag_url: str = f'{self.dir_config["url"]}/tag/@{t}.html'
-                    self.tags.append((t, tag_url))
-            else:
-                log.debug('no tags to parse')
+        self.cdate_rss = self.date(self.cts, 'rss_date')
+        self.cdate_sitemap = self.date(self.cts, 'sitemap_date')
+        if self.mts != 0.0:
+            self.mdate_rss = self.date(self.mts,  'rss_date')
+            self.mdate_sitemap = self.date(self.mts, 'sitemap_date')
 
-        log.debug('parsing page url')
-        # no need to specify dir_config['url'] as self.name already
-        #   contains the relative url
-        name_html: str = self.name.replace(".md", ".html")
-        self.url = f'{self.config["url"]["base"]}/{name_html}'
-        log.debug('final url "%s"', self.url)
diff --git a/src/pyssg/plt/index.html b/src/pyssg/plt/index.html
index 7c034bb..23d890a 100644
--- a/src/pyssg/plt/index.html
+++ b/src/pyssg/plt/index.html
@@ -19,13 +19,13 @@
   <ul>
   {%for p in all_pages%}
     {%if loop.previtem%}
-      {%if loop.previtem.cdate('list_sep_date') != p.cdate('list_sep_date')%}
-        <h3>{{p.cdate('list_sep_date')}}</h3>
+      {%if loop.previtem.date(loop.previtem.cts, 'list_sep_date') != p.date(p.cts, 'list_sep_date')%}
+        <h3>{{p.date(p.cts, 'list_sep_date')}}</h3>
       {%endif%}
     {%else%}
-        <h3>{{p.cdate('list_sep_date')}}</h3>
+        <h3>{{p.date(p.cts, 'list_sep_date')}}</h3>
     {%endif%}
-    <li>{{p.cdate('list_date')}} - <a href="{{p.url}}">{{p.title}}</a></li>
+    <li>{{p.date(p.cts, 'list_date')}} - <a href="{{p.url}}">{{p.title}}</a></li>
   {%endfor%}
   </ul>
   </body>
diff --git a/src/pyssg/plt/page.html b/src/pyssg/plt/page.html
index 93d4da9..b61ad03 100644
--- a/src/pyssg/plt/page.html
+++ b/src/pyssg/plt/page.html
@@ -8,9 +8,9 @@
   <body>
     <h1>{{page.title}}</h1>
     <p>By {{page.author}}</p>
-    <p>Created: {{page.cdate('date')}}</p>
-    {%if page.mdatetime is not none%}
-      <p>Modified: {{page.mdate('date')}}</p>
+    <p>Created: {{page.date(page.cts, 'date')}}</p>
+    {%if page.mtimestamp != 0.0%}
+      <p>Modified: {{page.date(page.mts, 'date')}}</p>
     {%endif%}
 
     {{page.content}}
diff --git a/src/pyssg/plt/tag.html b/src/pyssg/plt/tag.html
index aff7982..bf15c0d 100644
--- a/src/pyssg/plt/tag.html
+++ b/src/pyssg/plt/tag.html
@@ -13,13 +13,13 @@
   <ul>
   {%for p in tag_pages%}
     {%if loop.previtem%}
-      {%if loop.previtem.cdate('list_sep_date') != p.cdate('list_sep_date')%}
-        <h3>{{p.cdate('list_sep_date')}}</h3>
+      {%if loop.previtem.date(loop.previtem.cts, 'list_sep_date') != p.date(p.cts, 'list_sep_date')%}
+        <h3>{{p.date(p.cts, 'list_sep_date')}}</h3>
       {%endif%}
     {%else%}
-        <h3>{{p.cdate('list_sep_date')}}</h3>
+        <h3>{{p.date(p.cts, 'list_sep_date')}}</h3>
     {%endif%}
-    <li>{{p.cdate('list_date')}} - <a href="{{p.url}}">{{p.title}}</a></li>
+    <li>{{p.date(p.cts, 'list_date')}} - <a href="{{p.url}}">{{p.title}}</a></li>
   {%endfor%}
   </ul>
   </body>
-- 
cgit v1.2.3-70-g09d2