From 910df833d986dc1915fb40e784ec2e143b915c22 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Mon, 5 Mar 2018 02:34:47 +0000 Subject: md_tw.py: Add TWMarkdown.output_footnote. * md_tw.py (TWMarkdown)[output_footnote]: New method. --- md_tw.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/md_tw.py b/md_tw.py index ba85ea0..458e6c0 100644 --- a/md_tw.py +++ b/md_tw.py @@ -467,6 +467,53 @@ class TWMarkdown(mistune.Markdown): def output_def_link(self): return self.renderer.def_link(self.token['text']) + def output_footnote(self): + rm_i_indent = True + indent = ''.ljust(self.token['spaces']) + + def process(): + nonlocal rm_i_indent + + txt = self.tok() + if rm_i_indent: + txt = txt.lstrip() + + # Don't remove initial indent after processing first item. + rm_i_indent = False + + return txt + + # Take note of footnote key. + key = self.token['key'] + + # Add current initial indent + body = self.renderer.tw_get('initial_indent') + + # Set width + o_width = self.renderer.tw_get('width') + item_width = (o_width + - (len(self.renderer.tw_get('initial_indent')) + # Account for '[^key]: ' + + (len(key) + 5))) + self.renderer.tw_set(width=item_width) + + # Set prefix + prefix = self._add_prefix(indent) + + while self.pop()['type'] != 'footnote_end': + body += process() + body = body.rstrip() + '\n' + + rendered_fn = self.renderer.footnote_item(key, body) + + # Remove prefix + self._remove_prefix(len(indent)) + + # Revert width + self.renderer.tw_set(width=o_width) + + return rendered_fn + def main(): print('USAGE: md_tw 72 file.md file2.md [...]') -- cgit v1.2.3