Initialize Default Page Variables
authorGeorgios Atheridis <georgios@atheridis.org>
Mon, 6 Feb 2023 03:02:02 +0000 (03:02 +0000)
committerGeorgios Atheridis <georgios@atheridis.org>
Mon, 6 Feb 2023 03:02:02 +0000 (03:02 +0000)
Default page variables are initialized if not defined. Page name is the
name of the file without the extension and the date of the page is the
mtime of the file.

Implement custom date format which can be defined in the data.toml file.

Posts are now built in chronological order.

data.toml
sigma
templates/scripts/build_posts.py

index 2ce5b4d8a6d40cc71ece216767d3b5ed98d4ffd2..076926d367ed185b3fbccd69ae0ca195c8066436 100644 (file)
--- a/data.toml
+++ b/data.toml
@@ -4,6 +4,7 @@
 _PAGE_ROOT = "pages"
 _TEMPLATES = "templates"
 _OUT = "out"
+_DATE_FORMAT = "%Y-%m-%d"
 
 ##########################
 #              Variables #
diff --git a/sigma b/sigma
index d17f8b4d03ef862746e69e7303dca321c8162840..c92db1502fc5742e99a6f9cff698b15775305611 100755 (executable)
--- a/sigma
+++ b/sigma
@@ -28,6 +28,7 @@ import argparse
 import os
 import re
 import tomllib
+import time
 
 import markdown
 
@@ -42,7 +43,21 @@ def md_to_html(md: str) -> str:
 
 
 def initialize_values(data: dict):
-    pass
+    for dir in os.walk(data["_PAGE_ROOT"]):
+        for file in dir[2]:
+            path = os.path.join(dir[0], file)
+            rel_path = os.path.relpath(path, data["_PAGE_ROOT"])
+            mtime = os.path.getmtime(path)
+            rel_path_no_type, _ = os.path.splitext(rel_path)
+            namespace = split_path(rel_path_no_type)
+            update_value(data, namespace, "_name", namespace[-1], False)
+            update_value(
+                data,
+                namespace,
+                "_date",
+                time.strftime(data["_DATE_FORMAT"], time.localtime(mtime)),
+                False,
+            )
 
 
 def get_value(data: dict, namespace: tuple, key: str):
@@ -50,13 +65,12 @@ def get_value(data: dict, namespace: tuple, key: str):
     for namespace_item in namespace:
         data = data.get(namespace_item, data)
         value = data.get(key, value)
-    if value:
-        return value
-    if key == "_name":
-        return namespace[-1]
+    return value
 
 
-def update_value(data: dict, namespace: tuple, key: str, value):
+def update_value(data: dict, namespace: tuple, key: str, value, replace=True):
+    if not replace and get_value(data, namespace, key):
+        return
     for namespace_item in namespace:
         data = data.setdefault(namespace_item, {})
     data[key] = value
@@ -113,9 +127,7 @@ def generate_output(file: str, data: dict, namespace: tuple) -> str:
 
     if _extend := get_value(data, namespace, "_extend"):
         update_value(data, namespace, "_extend", "")
-        generate_output(
-            os.path.join(data["_TEMPLATES"], _extend), data, namespace
-        )
+        generate_output(os.path.join(data["_TEMPLATES"], _extend), data, namespace)
     return str(get_value(data, namespace, "_value"))
 
 
@@ -140,6 +152,13 @@ def main(args):
     elif not data.get("_OUT"):
         data["_OUT"] = "out"
 
+    if args.date_format:
+        data["_DATE_FORMAT"] = args.date_format
+    elif not data.get("_DATE_FORMAT"):
+        data["_DATE_FORMAT"] = "%Y-%m-%d"
+
+    initialize_values(data)
+
     rel_path = os.path.relpath(args.page, data["_PAGE_ROOT"])
     rel_path_no_type, file_type = os.path.splitext(rel_path)
     namespace = split_path(rel_path_no_type)
@@ -157,5 +176,6 @@ if __name__ == "__main__":
     parser.add_argument("--templates", type=str)
     parser.add_argument("--data", default="data.toml", type=argparse.FileType("rb"))
     parser.add_argument("--out", type=str)
+    parser.add_argument("--date-format", type=str)
     args = parser.parse_args()
     main(args)
index 034571278c94092e3d01cec5c29f428141bea64a..5b372cda81f353bfa674045022b612c72c9bd068 100644 (file)
@@ -1,14 +1,23 @@
 import os
+import time
 
 _value = "<ul>\n"
 
-posts = ""
+posts = data["posts"]
+post_timestamp = []
+for post in posts:
+    if post.startswith("_"):
+        continue
+    post_timestamp.append(
+        (time.strptime(posts[post]["_date"], data["_DATE_FORMAT"]), post)
+    )
+post_timestamp = sorted(post_timestamp, reverse=True)
 
-for post in os.listdir(os.path.join(data["_PAGE_ROOT"], "posts")):
+for timestamp, post in post_timestamp:
     path = "/posts/" + os.path.splitext(post)[0] + ".html"
     post = os.path.splitext(post)[0]
 
-    posts += (
+    _value += (
         '<li> <span>{{ posts.%(post)s._date }}</span> <a href="%(path)s">'
         "{{ posts.%(post)s._title }}</a> </li>\n"
         % {
@@ -17,5 +26,4 @@ for post in os.listdir(os.path.join(data["_PAGE_ROOT"], "posts")):
         }
     )
 
-_value += posts
 _value += "</ul>"