From 421adbea75c5a4282630a7399f8b1018c4f0dd90 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 27 Feb 2019 23:02:17 +0100 Subject: parser --- megapixels/app/site/parser.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/megapixels/app/site/parser.py b/megapixels/app/site/parser.py index dc53177b..98d9f284 100644 --- a/megapixels/app/site/parser.py +++ b/megapixels/app/site/parser.py @@ -16,7 +16,9 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): """ groups = [] current_group = [] + footnotes = [] in_stats = False + ignoring = False if 'desc' in metadata and 'subdesc' in metadata: groups.append(intro_section(metadata, s3_path)) @@ -27,7 +29,16 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): elif section.strip().startswith('---'): continue elif section.lower().strip().startswith('ignore text'): - break + ignoring = True + continue + elif section.strip().startswith('### Footnotes'): + groups.append(format_section(current_group, s3_path)) + footnotes = [] + in_footnotes = True + elif in_footnotes: + footnotes.append(section) + elif ignoring: + continue elif '### Statistics' in section: if len(current_group): groups.append(format_section(current_group, s3_path)) @@ -70,9 +81,14 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): else: current_group.append(section) groups.append(format_section(current_group, s3_path)) + + if len(footnotes): + groups.append(format_footnotes(footnotes, s3_path)) + content = "".join(groups) return content + def intro_section(metadata, s3_path): """ Build the intro section for datasets @@ -100,6 +116,7 @@ def intro_section(metadata, s3_path): return section + def fix_images(lines, s3_path): """ do our own tranformation of the markdown around images to handle wide images etc @@ -122,6 +139,7 @@ def fix_images(lines, s3_path): real_lines.append(line) return "\n".join(real_lines) + def format_section(lines, s3_path, type='', tag='section'): """ format a normal markdown section @@ -135,6 +153,7 @@ def format_section(lines, s3_path, type='', tag='section'): return "<{}>{}".format(tag, markdown(lines), tag) return "" + def fix_meta(lines): new_lines = [] for line in lines: @@ -143,6 +162,7 @@ def fix_meta(lines): new_lines.append(line) return new_lines + def format_metadata(section): """ format a metadata section (+ key: value pairs) @@ -153,6 +173,13 @@ def format_metadata(section): meta.append("
{}
{}
".format(key, value)) return "
{}
".format(''.join(meta)) +def format_footnotes(footnotes): + footnotes = '\n'.join(footnotes).split('\n') + for footnote in footnotes: + if not len(footnote) or '[^' not in footnote: + continue + key, footnote = footnotes.split(': ') + def format_applet(section, s3_path): """ Format the applets, which load javascript modules like the map and CSVs -- cgit v1.2.3-70-g09d2