From f8b8fbe6d17f92872e128c41af704dc7857eb02d Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Mon, 6 Feb 2023 03:02:02 +0000 Subject: [PATCH] Initialize Default Page Variables 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 | 1 + sigma | 38 ++++++++++++++++++++++++-------- templates/scripts/build_posts.py | 16 ++++++++++---- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/data.toml b/data.toml index 2ce5b4d..076926d 100644 --- 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 d17f8b4..c92db15 100755 --- 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) diff --git a/templates/scripts/build_posts.py b/templates/scripts/build_posts.py index 0345712..5b372cd 100644 --- a/templates/scripts/build_posts.py +++ b/templates/scripts/build_posts.py @@ -1,14 +1,23 @@ import os +import time _value = "" -- 2.30.2