2025-09-21 07:32:21 +02:00

130 lines
3.8 KiB
Python

import yaml, pathlib, shutil, markdown, frontmatter, arrow, jinja2
from fetch_ics import read_date
import utils
if __name__ == "__main__":
with open('conf.yml') as fp:
conf = yaml.safe_load(fp.read())
content = pathlib.Path(conf['content'])
template = pathlib.Path(conf['template'])
## description
desc = utils.read_file(content / 'description.md')
if not desc:
print("not description.md in content/")
exit(1)
# template
index = utils.read_file(template / 'index.html')
if not index:
print("not index.md in template/")
exit(1)
index_template = jinja2.Template(index)
# partials templates
partials = utils.get_files_in_subdir(template, 'partials', 'html')
# head
head_template = jinja2.Template(utils.read_file(partials['head']))
res_js = [p.relative_to('resources') for p in list(utils.get_files_in_subdir('resources/', 'js', 'js').values())]
res_css = [p.relative_to('resources') for p in list(utils.get_files_in_subdir('resources/', 'style', 'css').values())]
head = head_template.render(title=conf['site'], js_items=res_js, css_items=res_css)
# header
header_template = jinja2.Template(utils.read_file(partials['header']))
header = header_template.render(title=conf['site'])
# cases
cases_template = jinja2.Template(utils.read_file(partials['cases']))
cases = cases_template.render()
# description
description_template = jinja2.Template(utils.read_file(partials['description']))
desc = markdown.markdown(frontmatter.loads(desc).content)
description = description_template.render(desc=desc)
# section {events + readings}
section_template = jinja2.Template(utils.read_file(partials['section']))
content_dir = pathlib.Path(conf['content'])
# events
events_dir = content_dir / 'events'
upcoming_events = []
previous_events = []
if not events_dir.exists():
print("path content/events does not exist.")
else:
events = list(utils.get_files_in_subdir(conf['content'], 'events', 'md').values())
for e in events:
p = frontmatter.load(e)
summary = f"{p['title']} - {p['date']}"
content = markdown.markdown(p.content)
detail = f"<i>When:</i> {p['date']}<br><i>Where:</i> {p['location']}<br>{content}"
el = {'summary': summary, 'detail': detail}
if read_date(p) > arrow.utcnow():
upcoming_events.append(el)
else:
previous_events.append(el)
upcoming = section_template.render(section_id='events', section_title="Upcoming session(s)", section_items=upcoming_events)
previous = section_template.render(section_id='archive', section_title="Previous sessions", section_items=previous_events)
# bibliography
reading = ""
bibliography_dir = content_dir / "bibliography"
if not bibliography_dir.exists():
print("path content/bibliography does not exist.")
else:
bibliography = utils.ls_dir(bibliography_dir)
for b in bibliography:
reading_list = utils.get_files_in_subdir(bibliography_dir, b.stem, 'md')
if not reading_list:
continue
reading_list = list(reading_list.values())
readings = []
for r in sorted(reading_list):
p = frontmatter.load(r)
summary = p['title']
detail = markdown.markdown(p.content)
readings.append({'summary': summary, 'detail': detail})
reading += section_template.render(section_id='readings', section_title=b.stem, section_items=readings) + "\n"
# footer
if 'footer' in partials and 'footer_img' in conf:
footer_template = jinja2.Template(utils.read_file(partials['footer']))
footer = footer_template.render(footer_img=conf['footer_img'])
# index_html
index_html = index_template.render(head=head, header=header, cases=cases, description=description, upcoming=upcoming, reading=reading, previous=previous, footer=footer)
# save
output_path = pathlib.Path(f"{conf['output']}")
index_path = output_path / "index.html"
utils.save_file(index_path, index_html, overwrite=True, mkdirs=True)
shutil.copytree('resources', output_path, dirs_exist_ok=True)