summaryrefslogtreecommitdiff
path: root/megapixels/app/site
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-03-09 12:39:30 +0100
committerJules Laplace <julescarbon@gmail.com>2019-03-09 12:39:30 +0100
commit194564985418192f73627c97dbb46c0d748db96f (patch)
treea706b57bcb429cb357c4ad40c208505491280bf6 /megapixels/app/site
parenta952926378fbdac594c80e6f956b44a8fd87a1cf (diff)
using jinja to parse the inclues
Diffstat (limited to 'megapixels/app/site')
-rw-r--r--megapixels/app/site/loader.py2
-rw-r--r--megapixels/app/site/parser.py32
2 files changed, 22 insertions, 12 deletions
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 = '<section><ul class="footnotes"><li>' + '</li><li>'.join(footnote_list) + '</li></ul></section>'
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):
"""