import os
import re
import tomllib
+import time
import markdown
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):
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
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"))
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)
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)
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"
% {
}
)
-_value += posts
_value += "</ul>"