#!/usr/bin/python import os import glob from jinja2 import Environment, FileSystemLoader, select_autoescape import app.settings.app_cfg as cfg import app.site.s3 as s3 import app.site.parser as parser env = Environment( loader=FileSystemLoader(cfg.DIR_SITE_TEMPLATES), autoescape=select_autoescape([]) ) def build_page(fn, research_posts): """ build a single page from markdown into the appropriate template - writes it to site/public/ - syncs any assets with s3 - handles certain index pages... """ metadata, sections = parser.read_metadata(fn) if metadata is None: print("{} has no metadata".format(fn)) return print(metadata['url']) dirname = os.path.dirname(fn) output_path = cfg.DIR_SITE_PUBLIC + metadata['url'] output_fn = os.path.join(output_path, "index.html") skip_h1 = False if metadata['url'] == '/': template = env.get_template("home.html") elif 'research/' in fn: skip_h1 = True template = env.get_template("research.html") else: template = env.get_template("page.html") if 'datasets/' in fn: s3_dir = cfg.S3_DATASETS_PATH else: s3_dir = cfg.S3_SITE_PATH s3_path = s3.make_s3_path(s3_dir, metadata['path']) if 'index.md' in fn: s3.sync_directory(dirname, s3_dir, metadata) content = parser.parse_markdown(sections, s3_path, skip_h1=skip_h1) html = template.render( metadata=metadata, content=content, research_posts=research_posts, latest_research_post=research_posts[-1], ) os.makedirs(output_path, exist_ok=True) with open(output_fn, "w") as file: file.write(html) def build_research_index(research_posts): """ build the index of research (blog) posts """ metadata, sections = parser.read_metadata('../site/content/research/index.md') template = env.get_template("page.html") s3_path = s3.make_s3_path(cfg.S3_SITE_PATH, metadata['path']) content = parser.parse_markdown(sections, s3_path, skip_h1=False) content += parser.parse_research_index(research_posts) html = template.render( metadata=metadata, content=content, research_posts=research_posts, latest_research_post=research_posts[-1], ) output_fn = cfg.DIR_SITE_PUBLIC + '/research/index.html' with open(output_fn, "w") as file: file.write(html) def build_site(): """ build the site! =^) """ research_posts = parser.read_research_post_index() for fn in glob.iglob(os.path.join(cfg.DIR_SITE_CONTENT, "**/*.md"), recursive=True): build_page(fn, research_posts) build_research_index(research_posts) def build_file(fn): """ build just one page from a filename! =^) """ research_posts = parser.read_research_post_index() fn = os.path.join(cfg.DIR_SITE_CONTENT, fn) build_page(fn, research_posts)