2024-11-25 15:01:56 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import shutil
|
|
|
|
import csv
|
2024-12-02 22:40:59 +00:00
|
|
|
import re
|
2025-01-07 14:59:31 +00:00
|
|
|
from datetime import datetime
|
2024-11-25 15:01:56 +00:00
|
|
|
|
|
|
|
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
|
|
import frontmatter
|
|
|
|
import markdown
|
|
|
|
from slugify import slugify
|
|
|
|
|
|
|
|
env = Environment(
|
|
|
|
loader=PackageLoader("src"),
|
|
|
|
autoescape=select_autoescape()
|
|
|
|
)
|
|
|
|
|
|
|
|
CONTENT_D = os.path.abspath("src/content")
|
|
|
|
OUTPUT_D = "dist"
|
|
|
|
documents = {}
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2025-01-07 14:59:31 +00:00
|
|
|
def listDocuments(params):
|
2025-01-07 10:21:48 +00:00
|
|
|
param = params.split(" ")
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2025-01-07 10:21:48 +00:00
|
|
|
template = env.select_template([f"snippets/list-documents.jinja"])
|
|
|
|
html = template.render(documents=documents, layout=param[0], type=param[1])
|
2024-12-18 17:24:39 +00:00
|
|
|
|
|
|
|
return html
|
|
|
|
|
2025-01-07 19:40:12 +00:00
|
|
|
def detail(params):
|
|
|
|
param = params.split(" ")
|
|
|
|
modifier = False
|
|
|
|
|
|
|
|
if len(param)> 2:
|
|
|
|
modifier = param[2]
|
|
|
|
|
|
|
|
for doc in documents[param[0]]:
|
|
|
|
if doc.filename == param[1]:
|
|
|
|
template = env.select_template([f"snippets/detail.jinja"])
|
|
|
|
html = template.render(document=doc, modifier = modifier)
|
|
|
|
return html
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2025-01-07 14:59:31 +00:00
|
|
|
def listEvents(params):
|
|
|
|
param = params.split(" ")
|
|
|
|
|
|
|
|
if "events" not in documents:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
template = env.select_template([f"snippets/list-events.jinja"])
|
|
|
|
html = template.render(events=documents["events"], filter=param[0])
|
|
|
|
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
def slugify_filter(value):
|
|
|
|
return slugify(value)
|
|
|
|
|
|
|
|
|
2025-01-07 14:59:31 +00:00
|
|
|
def prettydate(value, format='%d/%m/%Y'):
|
|
|
|
return datetime.fromtimestamp(int(value)).strftime(format)
|
|
|
|
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
def shortcode_filter(value):
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
shortcode_callbacks = {
|
2025-01-07 14:59:31 +00:00
|
|
|
"show": listDocuments,
|
2025-01-07 19:40:12 +00:00
|
|
|
"events": listEvents,
|
|
|
|
"detail": detail
|
2024-12-02 22:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def shortcode_replacer(match):
|
|
|
|
shortcode_name = match.group(1).strip()
|
|
|
|
param = match.group(2).strip()
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
if shortcode_name in shortcode_callbacks:
|
|
|
|
return shortcode_callbacks[shortcode_name](param)
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
return match.group(0)
|
|
|
|
|
|
|
|
pattern = re.compile(r"{{\s*(\w+)\s+([^{}]+?)\s*}}")
|
|
|
|
return pattern.sub(shortcode_replacer, value)
|
|
|
|
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
env.filters["shortcode"] = shortcode_filter
|
|
|
|
env.filters["slugify"] = slugify_filter
|
2025-01-07 14:59:31 +00:00
|
|
|
env.filters["prettydate"] = prettydate
|
2024-11-25 15:01:56 +00:00
|
|
|
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
def render_single_file(template, page, path, dist):
|
|
|
|
html = template.render(documents=documents, page=page)
|
|
|
|
name = Path(path).stem
|
|
|
|
|
|
|
|
if not os.path.exists(dist):
|
|
|
|
os.makedirs(dist)
|
|
|
|
|
|
|
|
with open(f"{dist}/{name}.html", "w", encoding="utf-8") as output_file:
|
|
|
|
output_file.write(html)
|
|
|
|
|
|
|
|
|
|
|
|
def get_page_data(path):
|
|
|
|
filename = Path(path).stem
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
page = frontmatter.load(path)
|
|
|
|
page['slug'] = slugify(filename)
|
2024-12-02 22:40:59 +00:00
|
|
|
page.filename = filename
|
2025-01-07 10:21:48 +00:00
|
|
|
page.folder = os.path.basename(os.path.dirname(path))
|
|
|
|
|
2025-01-07 14:59:31 +00:00
|
|
|
if "start_datetime" in page:
|
|
|
|
now = datetime.now()
|
|
|
|
page["has_passed"] = datetime.fromtimestamp(page["start_datetime"]) < now
|
|
|
|
|
|
|
|
|
2025-01-07 10:21:48 +00:00
|
|
|
page.body = markdown.markdown(
|
|
|
|
page.content,
|
|
|
|
extensions=[
|
|
|
|
'def_list',
|
2025-01-07 19:40:12 +00:00
|
|
|
'footnotes'])
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
return page
|
|
|
|
|
|
|
|
|
|
|
|
def render_posts(path):
|
|
|
|
name = Path(path).stem
|
|
|
|
print(f"looking for {name}.jinja")
|
|
|
|
template = env.select_template([f"{name}.jinja", "post.jinja"])
|
|
|
|
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
if filename.endswith(".md"):
|
|
|
|
post_path = os.path.join(path, filename)
|
|
|
|
page = get_page_data(post_path)
|
|
|
|
render_single_file(template, page, post_path, f"{OUTPUT_D}/{name}")
|
|
|
|
|
|
|
|
|
|
|
|
def preload_documents():
|
|
|
|
print("preload any needed data")
|
|
|
|
|
|
|
|
for subdir in os.listdir(CONTENT_D):
|
|
|
|
path = os.path.join(CONTENT_D, subdir)
|
|
|
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
name = Path(path).stem
|
|
|
|
if name not in documents:
|
|
|
|
documents[name] = []
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
if filename.endswith(".md"):
|
|
|
|
post_path = os.path.join(path, filename)
|
|
|
|
|
|
|
|
documents[name].append(get_page_data(post_path))
|
|
|
|
|
|
|
|
elif Path(path).suffix == '.md':
|
|
|
|
documents[Path(path).stem] = get_page_data(path)
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
def copy_assets():
|
|
|
|
if os.path.exists("dist/assets"):
|
|
|
|
shutil.rmtree("dist/assets")
|
|
|
|
|
|
|
|
shutil.copytree("src/assets", "dist/assets")
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
def get_inventory():
|
|
|
|
|
|
|
|
with open("src/content/component-inventory.csv") as f:
|
|
|
|
documents['inventory'] = []
|
2025-01-07 10:21:48 +00:00
|
|
|
for line in csv.DictReader(
|
|
|
|
f,
|
|
|
|
fieldnames=(
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Value',
|
|
|
|
'type',
|
|
|
|
'Date',
|
|
|
|
'Where',
|
|
|
|
'Mounting type')):
|
2024-11-25 15:01:56 +00:00
|
|
|
documents['inventory'].append(line)
|
|
|
|
|
2024-12-02 22:40:59 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
def main():
|
|
|
|
get_inventory()
|
|
|
|
preload_documents()
|
|
|
|
|
|
|
|
print("render the content")
|
|
|
|
for subdir in os.listdir(CONTENT_D):
|
|
|
|
path = os.path.join(CONTENT_D, subdir)
|
|
|
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
render_posts(path)
|
|
|
|
elif Path(path).suffix == '.md':
|
|
|
|
template = env.select_template(
|
|
|
|
[f"{Path(path).stem}.jinja", "post.jinja"])
|
|
|
|
page = get_page_data(path)
|
|
|
|
render_single_file(template, page, path, OUTPUT_D)
|
2025-01-07 10:51:20 +00:00
|
|
|
elif Path(path).suffix in [".csv"]:
|
|
|
|
print("not compiling this file!")
|
2024-11-25 15:01:56 +00:00
|
|
|
else:
|
2025-01-07 10:51:20 +00:00
|
|
|
print("i cannot handle this file yet: ", path)
|
2024-11-25 15:01:56 +00:00
|
|
|
|
|
|
|
copy_assets()
|
|
|
|
|
2025-01-07 10:21:48 +00:00
|
|
|
|
2024-11-25 15:01:56 +00:00
|
|
|
main()
|