def write_text_to_file( filepath: Path, ctx: dict, text: str, out_dir: str, ext: Optional[str] = None, relative_to: Optional[str] = None, ): if relative_to is not None: filepath = filepath.relative_to(relative_to) new_filepath = Path(out_dir) / filepath new_filepath.parent.mkdir(parents=True, exist_ok=True) if ext is not None: new_filepath = new_filepath.with_suffix(ext) with open(new_filepath, "w") as f: f.write(text) return ctx
def parse_markdown_with_template(filepath: Path, ctx: dict, template_dir: str): metadata, content = parse_markdown_file_with_fm(filepath) env = Environment( loader=FileSystemLoader(template_dir), trim_blocks=True, lstrip_blocks=True ) env.globals = ctx.get("__globals", {}) try: template_name = metadata.pop("__template") except KeyError as e: logging.error(f"File {filepath} does not specify a template.") raise e ctx["template"] = template_name try: template = env.get_template(template_name) except TemplateNotFound as e: logging.error(f"Could not locate template {template_name}.") raise e special_keys = [k[2:] for k in metadata.keys() if k[0:2] == "__"] for key in special_keys: ctx[key] = metadata.pop(key) output = template.render({**metadata, "content": content}) filepath = filepath.with_suffix(".html") ctx.update({"output": output}) return ctx