diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2019-03-09 12:39:30 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2019-03-09 12:39:30 +0100 |
| commit | 194564985418192f73627c97dbb46c0d748db96f (patch) | |
| tree | a706b57bcb429cb357c4ad40c208505491280bf6 /megapixels/app/site/parser.py | |
| parent | a952926378fbdac594c80e6f956b44a8fd87a1cf (diff) | |
using jinja to parse the inclues
Diffstat (limited to 'megapixels/app/site/parser.py')
| -rw-r--r-- | megapixels/app/site/parser.py | 32 |
1 files changed, 20 insertions, 12 deletions
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): """ |
