diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2019-02-28 15:56:04 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2019-02-28 15:56:04 +0100 |
| commit | 6711fb0c58e969284e3fcf94bb163c77445e2e13 (patch) | |
| tree | 9fe398601ca07919e546829d223b0f4be4649a11 /megapixels/app | |
| parent | eaf8a163ed3dc15b624188dcf8ae7216b801d73e (diff) | |
footnote back and forth navigation
Diffstat (limited to 'megapixels/app')
| -rw-r--r-- | megapixels/app/site/parser.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/megapixels/app/site/parser.py b/megapixels/app/site/parser.py index ef83b655..9e904e00 100644 --- a/megapixels/app/site/parser.py +++ b/megapixels/app/site/parser.py @@ -10,6 +10,8 @@ import app.site.s3 as s3 renderer = mistune.Renderer(escape=False) markdown = mistune.Markdown(renderer=renderer) +footnote_count = 0 + def parse_markdown(metadata, sections, s3_path, skip_h1=False): """ parse page into sections, preprocess the markdown to handle our modifications @@ -94,7 +96,18 @@ def parse_markdown(metadata, sections, s3_path, skip_h1=False): if footnote_lookup: for key, index in footnote_lookup.items(): - content = content.replace(key, '<a href="#{}" class="footnote" title="Footnote {}">{}</a>'.format(key, index, index)) + global footnote_count + footnote_count = 0 + letters = "abcdefghijklmnopqrstuvwxyz" + footnote_backlinks = [] + def footnote_tag(match): + global footnote_count + footnote_count += 1 + footnote_backlinks.append('<a href="#{}_{}">{}</a>'.format(key, footnote_count, letters[footnote_count-1])) + return '<a class="footnote_shim" name="{}_{}"> </a><a href="#{}" class="footnote" title="Footnote {}">{}</a>'.format(key, footnote_count, key, index, index) + key_regex = re.compile(key.replace('[', '\\[').replace('^', '\\^').replace(']', '\\]')) + content = key_regex.sub(footnote_tag, content) + footnote_txt = footnote_txt.replace("{}_BACKLINKS".format(index), "".join(footnote_backlinks)) content += footnote_txt return content @@ -197,7 +210,7 @@ def format_footnotes(footnotes, s3_path): continue key, note = footnote.split(': ', 1) footnote_index_lookup[key] = index - footnote_list.append('<a name="{}" class="footnote_anchor">^</a>'.format(key) + markdown(note)) + footnote_list.append('<a name="{}" class="footnote_shim"></a><span class="backlinks">{}_BACKLINKS</span>'.format(key, index) + markdown(note)) index += 1 footnote_txt = '<section><ul class="footnotes"><li>' + '</li><li>'.join(footnote_list) + '</li></ul></section>' |
