From 48cabe1df739d1452e7ecc5164122a067157b057 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Tue, 27 Dec 2022 02:59:59 -0600 Subject: add extra method for date conversions --- src/pyssg/page.py | 27 +++++++++++++++++++++------ src/pyssg/plt/index.html | 8 ++++---- src/pyssg/plt/page.html | 4 ++-- src/pyssg/plt/tag.html | 8 ++++---- 4 files changed, 31 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/pyssg/page.py b/src/pyssg/page.py index 0161f25..85a6a47 100644 --- a/src/pyssg/page.py +++ b/src/pyssg/page.py @@ -59,6 +59,23 @@ class Page: log.debug('getting metadata "%s" failed, using optional value "%s"', var, or_else) return or_else + def cdate(self, format: str) -> str: + if format in self.config['fmt']: + return self.cdatetime.strftime(self.config['fmt'][format]) + else: + log.warning('format "%s" not found in config["fmt"], returning empty string', format) + return '' + + def mdate(self, format: str) -> str: + if self.mdatetime is None: + log.warning('no mdatetime found, can\'t return a formatted string') + return '' + if format in self.config['fmt']: + return self.mdatetime.strftime(self.config['fmt'][format]) # type: ignore + else: + log.warning('format "%s" not found in config["fmt"], returning empty string', format) + return '' + # parses meta from self.meta, for og, it prioritizes, # the actual og meta def parse_metadata(self): @@ -70,16 +87,14 @@ class Page: log.debug('parsing timestamp') self.cdatetime = datetime.fromtimestamp(self.ctimestamp, tz=timezone.utc) - cdate = lambda x : self.cdatetime.strftime(self.config['fmt'][x]) - self.cdate_rss = cdate('rss_date') - self.cdate_sitemap = cdate('sitemap_date') + 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 = datetime.fromtimestamp(self.mtimestamp, tz=timezone.utc) - mdate = lambda x : self.mdatetime.strftime(self.config['fmt'][x]) # type: ignore - self.mdate_rss = mdate('rss_date') - self.mdate_sitemap = mdate('sitemap_date') + 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') diff --git a/src/pyssg/plt/index.html b/src/pyssg/plt/index.html index c33fd37..7c034bb 100644 --- a/src/pyssg/plt/index.html +++ b/src/pyssg/plt/index.html @@ -19,13 +19,13 @@ diff --git a/src/pyssg/plt/page.html b/src/pyssg/plt/page.html index 08e92f1..93d4da9 100644 --- a/src/pyssg/plt/page.html +++ b/src/pyssg/plt/page.html @@ -8,9 +8,9 @@

{{page.title}}

By {{page.author}}

-

Created: {{page.cdatetime.strftime(config['fmt']['date'])}}

+

Created: {{page.cdate('date')}}

{%if page.mdatetime is not none%} -

Modified: {{page.mdatetime.strftime(config['fmt']['date'])}}

+

Modified: {{page.mdate('date')}}

{%endif%} {{page.content}} diff --git a/src/pyssg/plt/tag.html b/src/pyssg/plt/tag.html index 2dd7177..aff7982 100644 --- a/src/pyssg/plt/tag.html +++ b/src/pyssg/plt/tag.html @@ -13,13 +13,13 @@ -- cgit v1.2.3-70-g09d2