diff options
author | rsiddharth <s@ricketyspace.net> | 2018-01-02 00:51:19 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2018-01-02 00:51:19 +0000 |
commit | 673c0ad9bbce77a121353757a93c18c808a2655e (patch) | |
tree | 06552e72205c3667a4622b7ab6fe1e84ec44d42d /md_tw.py | |
parent | ad8fa809c55e2bf2a6bf2e977f6683e90ad7200d (diff) |
md_tw.py: Add `TWBlockLexer.parse_def_footnotes`.
* md_tw.py
(TWBlockLexer.parse_def_footnotes): New method.
* tests/test_md_tw.py
(TestTWBlockLexer.test_parse_def_footnotes): New test.
* tests/data/blexer-footnotes.md: New file.
Diffstat (limited to 'md_tw.py')
-rw-r--r-- | md_tw.py | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -159,6 +159,48 @@ class TWBlockLexer(mistune.BlockLexer): 'text': m.group(0) }) + def parse_def_footnotes(self, m): + key = self._keyify(m.group(1)) + if key in self.def_footnotes: + # footnote is already defined + return + + self.def_footnotes[key] = 0 + + text = m.group(2) + multiline = False + spaces = 0 + if '\n' in text: + multiline = True + lines = text.split('\n') + whitespace = None + for line in lines[1:]: + space = len(line) - len(line.lstrip()) + if space and (not whitespace or space < whitespace): + whitespace = space + newlines = [lines[0]] + for line in lines[1:]: + newlines.append(line[whitespace:]) + text = '\n'.join(newlines) + + if whitespace: + spaces = whitespace + + self.tokens.append({ + 'type': 'footnote_start', + 'key': key, + 'multiline': multiline, + 'spaces': spaces + }) + + self.parse(text, self.footnote_rules) + + self.tokens.append({ + 'type': 'footnote_end', + 'key': key, + 'spaces': spaces + }) + class TWInlineLexer(mistune.InlineLexer): """Text Wrap Inline level lexer for inline gramars.""" |