roundabout,
created on Saturday, 20 April 2024, 08:10:19 (1713600619),
received on Tuesday, 23 April 2024, 11:54:32 (1713873272)
Author identity: vlad <vlad.muntoiu@gmail.com>
aacbad7428d26f4b3e935f4eb51087d81c4c614b
ampoule_ssg/__init__.py
@@ -1,16 +1,24 @@
#!/usr/bin/env python3 import os import jinja2from ruamel.yaml import YAMLimport reimport shutil import contextlib from datetime import datetime import jinja2 from ruamel.yaml import YAMLimport colorama colorama.init() # Disable YAML date constructor def no_date_constructor(loader, node): value = loader.construct_scalar(node) return value @contextlib.contextmanager def in_directory(directory): cwd = os.getcwd()
@@ -29,13 +37,95 @@ def delete_directory_contents(directory):
shutil.rmtree(os.path.join(root, dir)) def parse_date_string(date_string): def split_date_and_time(date_string): if ":" not in date_string: return date_string, "00:00:00" elements = date_string.partition(":") partition_character = " " if " " not in date_string: partition_character = "-" if "-" not in date_string: partition_character = "T" date = elements[0].rpartition(partition_character)[0].strip() time = elements[0].rpartition(partition_character)[2].strip() + elements[1] + elements[2].strip() time = time.removeprefix("T").removesuffix("Z") return date, time time_formats = [ # 24-hour ISO "%H:%M:%S", "%H:%M", "%H", # Single digit hour "-%H:%M:%S", "-%H:%M", "-%H", # 12-hour (AM/PM) "%I:%M:%S %p", "%I:%M %p", "%I %p", # Single digit 12-hour "-%I:%M:%S %p", "-%I:%M %p", "-%I %p", ] date_formats = [ # ISO formats "%Y-%m-%d", "%y-%m-%d", # European formats "%d.%m.%Y", "%d.%m.%y", # American formats "%m/%d/%Y", "%m/%d/%y", # Text-based European formats "%d %B %Y", "%d %b %Y", "%d %B, %Y", "%d %b, %Y", # Text-based American formats "%B %d %Y", "%b %d %Y", "%B %d, %Y", "%b %d, %Y", # ISO weekly calendar "%G-W%V-%u", ] date, time = split_date_and_time(date_string) time_object = datetime.min.time() date_object = datetime.min.date() for time_format in time_formats: try: time_object = datetime.strptime(time, time_format) except ValueError: pass for date_format in date_formats: try: date_object = datetime.strptime(date, date_format) except ValueError: pass return datetime.combine(date_object, time_object.time()) class Document: def __init__(self, file_name, url_transform=lambda x: x): self.file_name = file_name self.encoding = "utf-8" # If the file is text, read it. self.front_matter = YAML() self.front_matter.Constructor.add_constructor("tag:yaml.org,2002:timestamp", no_date_constructor)self.content = "" self.date = datetime.fromtimestamp(os.path.getmtime(file_name))try: with open(file_name, "r", encoding=self.encoding) as f: print(colorama.Style.RESET_ALL, colorama.Style.BRIGHT, colorama.Fore.LIGHTWHITE_EX, f"Loading document {file_name}".ljust(shutil.get_terminal_size().columns), sep="")
@@ -51,10 +141,14 @@ class Document:
if line != "---\n": front_matter += line print(colorama.Style.RESET_ALL, colorama.Fore.GREEN, "Front matter loaded", sep="") print(front_matter)if front_matter: self.front_matter = self.front_matter.load(front_matter) print(self.front_matter, type(self.front_matter)) if "DATE" in self.front_matter: self.date = parse_date_string(self.front_matter["DATE"])else: # put it back self.content = initial_line
@@ -88,15 +182,20 @@ class Document:
class Index: def __init__(self, directory, recursive=False, url_transform=lambda x: x):def __init__(self, directory, recursive=False, url_transform=lambda x: x, sort_by=lambda x: x.file_name, exclude=None):self.directory = directory # Temporarily move to the specified directory in order to read the files. if exclude: regex = re.compile(exclude) else: regex = re.compile("(?!)")with in_directory(directory): if recursive: self.file_names = [os.path.join(dir_path, f) for dir_path, dir_name, filenames in os.walk(".") for f in filenames]self.file_names = [os.path.join(dir_path, f) for dir_path, dir_name, filenames in os.walk(".") for f in filenames if not regex.search(f)]else: self.file_names = [i for i in os.listdir() if os.path.isfile(i)]self.documents = [Document(i, url_transform) for i in self.file_names]self.file_names = [i for i in os.listdir() if os.path.isfile(i) and not regex.search(i)] self.documents = sorted([Document(i, url_transform) for i in self.file_names], key=sort_by)self.__current_index = 0 def __iter__(self):