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'])) # events events = list(utils.get_files_in_subdir(conf['content'], 'event', 'md').values()) upcoming_events = [] previous_events = [] for e in events: p = frontmatter.load(e) summary = f"{p['title']} - {p['date']}" content = markdown.markdown(p.content) detail = f"When: {p['date']}
Where: {p['location']}
{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) # reading bibliography reading = "" bibliography_dir = pathlib.Path(conf['content']) / "bibliography" 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)