diff options
-rw-r--r-- | markdown_extensions/footnotes.py | 59 | ||||
-rw-r--r-- | tests/extensions-x-footnotes/footnote.html | 6 | ||||
-rw-r--r-- | tests/extensions-x-footnotes/named_markers.html | 12 |
3 files changed, 66 insertions, 11 deletions
diff --git a/markdown_extensions/footnotes.py b/markdown_extensions/footnotes.py index d653c6e..f65a79d 100644 --- a/markdown_extensions/footnotes.py +++ b/markdown_extensions/footnotes.py @@ -29,7 +29,7 @@ from markdown import etree FN_BACKLINK_TEXT = "zz1337820767766393qq" NBSP_PLACEHOLDER = "qq3936677670287331zz" DEF_RE = re.compile(r'(\ ?\ ?\ ?)\[\^([^\]]*)\]:\s*(.*)') - +TABBED_RE = re.compile(r'((\t)|( ))(.*)') class FootnoteExtension(markdown.Extension): """ Footnote Extension. """ @@ -111,9 +111,7 @@ class FootnoteExtension(markdown.Extension): for id in self.footnotes.keys(): li = etree.SubElement(ol, "li") li.set("id", self.makeFootnoteId(id)) - self.parser.parseChunk(li, self.footnotes[id].split("\n"), - looseList=1) - + self.parser.parseChunk(li, self.footnotes[id]) backlink = etree.Element("a") backlink.set("href", "#" + self.makeFootnoteRefId(id)) backlink.set("rev", "footnote") @@ -158,7 +156,7 @@ class FootnotePreprocessor(markdown.Preprocessor): if id : plain = lines[:i] - detabbed, theRest = self.footnotes.parser.detectTabbed(lines[i+1:]) + detabbed, theRest = self.detectTabbed(lines[i+1:]) self.footnotes.setFootnote(id, footnote + "\n" + "\n".join(detabbed)) @@ -188,6 +186,57 @@ class FootnotePreprocessor(markdown.Preprocessor): counter += 1 return counter, None, None + def detectTabbed(self, lines): + """ Find indented text and remove indent before further proccesing. + + Keyword arguments: + + * lines: an array of strings + + Returns: a list of post processed items and the unused + remainder of the original list + + """ + items = [] + item = -1 + i = 0 # to keep track of where we are + + def detab(line): + match = TABBED_RE.match(line) + if match: + return match.group(4) + + for line in lines: + if line.strip(): # Non-blank line + line = detab(line) + if line: + items.append(line) + i += 1 + continue + else: + return items, lines[i:] + + else: # Blank line: _maybe_ we are done. + i += 1 # advance + + # Find the next non-blank line + for j in range(i, len(lines)): + if lines[j].strip(): + next_line = lines[j]; break + else: + break # There is no more text; we are done. + + # Check if the next non-blank line is tabbed + if detab(next_line): # Yes, more work to do. + items.append("") + continue + else: + break # No, we are done. + else: + i += 1 + + return items, lines[i:] + class FootnotePattern(markdown.Pattern): """ InlinePattern for footnote markers in a document's body text. """ diff --git a/tests/extensions-x-footnotes/footnote.html b/tests/extensions-x-footnotes/footnote.html index b949c57..6556dab 100644 --- a/tests/extensions-x-footnotes/footnote.html +++ b/tests/extensions-x-footnotes/footnote.html @@ -12,12 +12,14 @@ </li> <li id="fn:2"> <blockquote> -<p>This footnote is a blockquote.</p> +<p>This footnote is a blockquote. +</p> </blockquote> <p><a href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> </li> <li id="fn:3"> -<p>A simple oneliner. <a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> +<p>A simple oneliner. + <a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> </li> <li id="fn:4"> <p>A footnote with multiple paragraphs.</p> diff --git a/tests/extensions-x-footnotes/named_markers.html b/tests/extensions-x-footnotes/named_markers.html index f643b7b..6996b5f 100644 --- a/tests/extensions-x-footnotes/named_markers.html +++ b/tests/extensions-x-footnotes/named_markers.html @@ -5,16 +5,20 @@ oddly<sup id="fnref:56"><a href="#fn:56" rel="footnote">3</a></sup> numbered<su <hr /> <ol> <li id="fn:foo"> -<p>Footnote marked <code>foo</code>. <a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> +<p>Footnote marked <code>foo</code>. + <a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> </li> <li id="fn:bar"> -<p>This one is marked <em>bar</em>. <a href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> +<p>This one is marked <em>bar</em>. + <a href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> </li> <li id="fn:56"> -<p>A <strong>numbered</strong> footnote. <a href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> +<p>A <strong>numbered</strong> footnote. + <a href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> </li> <li id="fn:99"> -<p>The last one. <a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> +<p>The last one. + <a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> </li> </ol> </div>
\ No newline at end of file |