From 59d860ec5dc701a0e2335c24f43e8960fee040dd Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 13 Nov 2008 23:06:59 -0500 Subject: Fixed footnote extension to work with new core BlockParser and updated tests which had a few insignificant differances in whitespace. --- markdown_extensions/footnotes.py | 59 ++++++++++++++++++++++--- tests/extensions-x-footnotes/footnote.html | 6 ++- 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 @@
  • -

    This footnote is a blockquote.

    +

    This footnote is a blockquote. +

  • -

    A simple oneliner. 

    +

    A simple oneliner. + 

  • A footnote with multiple paragraphs.

    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 @@ oddly3 numbered
    1. -

      Footnote marked foo

      +

      Footnote marked foo. + 

    2. -

      This one is marked bar

      +

      This one is marked bar. + 

    3. -

      A numbered footnote. 

      +

      A numbered footnote. + 

    4. -

      The last one. 

      +

      The last one. + 

    \ No newline at end of file -- cgit v1.2.3