From 1b008e4b4d11def9b13dc0a800b0d068624d43ae Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 27 Feb 2019 23:48:35 +0100 Subject: half of a footnote implementation --- megapixels/app/site/parser.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'megapixels/app/site') diff --git a/megapixels/app/site/parser.py b/megapixels/app/site/parser.py index 98d9f284..ef83b655 100644 --- a/megapixels/app/site/parser.py +++ b/megapixels/app/site/parser.py @@ -18,6 +18,7 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): current_group = [] footnotes = [] in_stats = False + in_footnotes = False ignoring = False if 'desc' in metadata and 'subdesc' in metadata: @@ -33,6 +34,7 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): continue elif section.strip().startswith('### Footnotes'): groups.append(format_section(current_group, s3_path)) + current_group = [] footnotes = [] in_footnotes = True elif in_footnotes: @@ -82,10 +84,18 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): current_group.append(section) groups.append(format_section(current_group, s3_path)) + footnote_txt = '' + footnote_lookup = {} + if len(footnotes): - groups.append(format_footnotes(footnotes, s3_path)) + footnote_txt, footnote_lookup = format_footnotes(footnotes, s3_path) content = "".join(groups) + + if footnote_lookup: + for key, index in footnote_lookup.items(): + content = content.replace(key, '{}'.format(key, index, index)) + content += footnote_txt return content @@ -153,8 +163,10 @@ def format_section(lines, s3_path, type='', tag='section'): return "<{}>{}".format(tag, markdown(lines), tag) return "" - def fix_meta(lines): + """ + Format metadata sections before passing to markdown + """ new_lines = [] for line in lines: if line.startswith('+ '): @@ -162,7 +174,6 @@ def fix_meta(lines): new_lines.append(line) return new_lines - def format_metadata(section): """ format a metadata section (+ key: value pairs) @@ -173,12 +184,24 @@ def format_metadata(section): meta.append("
{}
{}
".format(key, value)) return "
{}
".format(''.join(meta)) -def format_footnotes(footnotes): +def format_footnotes(footnotes, s3_path): + """ + Format the footnotes section separately and produce a lookup we can use to update the main site + """ footnotes = '\n'.join(footnotes).split('\n') + index = 1 + footnote_index_lookup = {} + footnote_list = [] for footnote in footnotes: if not len(footnote) or '[^' not in footnote: continue - key, footnote = footnotes.split(': ') + key, note = footnote.split(': ', 1) + footnote_index_lookup[key] = index + footnote_list.append('^'.format(key) + markdown(note)) + index += 1 + + footnote_txt = '
' + return footnote_txt, footnote_index_lookup def format_applet(section, s3_path): """ @@ -189,7 +212,7 @@ def format_applet(section, s3_path): applet = {} # print(payload) if ': ' in payload[0]: - command, opt = payload[0].split(': ') + command, opt = payload[0].split(': ', 1) else: command = payload[0] opt = None -- cgit v1.2.3-70-g09d2