From 194564985418192f73627c97dbb46c0d748db96f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 9 Mar 2019 12:39:30 +0100 Subject: using jinja to parse the inclues --- megapixels/app/site/loader.py | 2 ++ megapixels/app/site/parser.py | 32 ++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) (limited to 'megapixels') diff --git a/megapixels/app/site/loader.py b/megapixels/app/site/loader.py index 8fd7a2f8..a544333b 100644 --- a/megapixels/app/site/loader.py +++ b/megapixels/app/site/loader.py @@ -85,6 +85,8 @@ def parse_metadata(fn, sections): metadata['meta'] = load_json(dataset_path) if not metadata['meta']: print("Bad metadata? {}".format(dataset_path)) + if 'meta' not in metadata or not metadata['meta']: # dude + metadata['meta'] = {} return metadata, valid_sections diff --git a/megapixels/app/site/parser.py b/megapixels/app/site/parser.py index f6e308f3..79093bc7 100644 --- a/megapixels/app/site/parser.py +++ b/megapixels/app/site/parser.py @@ -4,6 +4,7 @@ import re import glob import simplejson as json import mistune +from jinja2 import Environment, FileSystemLoader, select_autoescape import app.settings.app_cfg as cfg import app.site.s3 as s3 @@ -11,6 +12,11 @@ import app.site.s3 as s3 renderer = mistune.Renderer(escape=False) markdown = mistune.Markdown(renderer=renderer) +includes_env = Environment( + loader=FileSystemLoader(cfg.DIR_SITE_INCLUDES), + autoescape=select_autoescape([]) +) + footnote_count = 0 def parse_markdown(metadata, sections, s3_path, skip_h1=False): @@ -63,7 +69,7 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): current_group = [] current_group.append(section) if section.strip().endswith(' %}'): - groups.append(format_include("\n\n".join(current_group))) + groups.append(format_include("\n\n".join(current_group), metadata)) current_group = [] elif section.strip().startswith('```'): groups.append(format_section(current_group, s3_path)) @@ -232,20 +238,22 @@ def format_footnotes(footnotes, s3_path): footnote_txt = '
' return footnote_txt, footnote_index_lookup -def format_include(section): +def format_include(section, metadata): """ Include html template """ - include_dir = cfg.DIR_SITE_INCLUDES - fp_html = section.strip().strip('\n').strip().strip('{%').strip().strip('%}').strip() - fp_html = fp_html.strip('include').strip().strip('"').strip().strip("'").strip() - try: - with open(join(include_dir, fp_html), 'r') as fp: - html = fp.read().replace('\n', '') - return html - except Exception as e: - print(f'Error parsing include: {e}') - return '' + include_fn = section.strip().strip('\n').strip().strip('{%').strip().strip('%}').strip() + include_fn = include_fn.strip('include').strip().strip('"').strip().strip("'").strip() + return includes_env.get_template(include_fn).render(metadata=metadata) + # include_dir = cfg.DIR_SITE_INCLUDES + # try: + # includes_env.get_template(fp_html) + # with open(join(include_dir, fp_html), 'r') as fp: + # html = fp.read().replace('\n', '') + # return html + # except Exception as e: + # print(f'Error parsing include: {e}') + # return '' def format_applet(section, s3_path): """ -- cgit v1.2.3-70-g09d2