Skip to content

Commit 2208121

Browse files
author
Waylan Limberg
committed
Fixed #51. Removed recussion from footnote preproccessor. Also refactors the code to provide a few other minor improvements s that output more closely matches php's output. Thus the changes in the tests.
1 parent ef9a229 commit 2208121

File tree

4 files changed

+38
-61
lines changed

4 files changed

+38
-61
lines changed

markdown/extensions/footnotes.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
FN_BACKLINK_TEXT = "zz1337820767766393qq"
3131
NBSP_PLACEHOLDER = "qq3936677670287331zz"
32-
DEF_RE = re.compile(r'(\ ?\ ?\ ?)\[\^([^\]]*)\]:\s*(.*)')
32+
DEF_RE = re.compile(r'[ ]{0,3}\[\^([^\]]*)\]:\s*(.*)')
3333
TABBED_RE = re.compile(r'((\t)|( ))(.*)')
3434

3535
class FootnoteExtension(markdown.Extension):
@@ -152,54 +152,33 @@ def __init__ (self, footnotes):
152152
self.footnotes = footnotes
153153

154154
def run(self, lines):
155-
lines = self._handleFootnoteDefinitions(lines)
156-
text = "\n".join(lines)
157-
return text.split("\n")
158-
159-
def _handleFootnoteDefinitions(self, lines):
160155
"""
161-
Recursively find all footnote definitions in lines.
156+
Loop through lines and find, set, and remove footnote definitions.
162157
163158
Keywords:
164159
165160
* lines: A list of lines of text
166-
167-
Return: A list of lines with footnote definitions removed.
168-
169-
"""
170-
i, id, footnote = self._findFootnoteDefinition(lines)
171-
172-
if id :
173-
plain = lines[:i]
174-
detabbed, theRest = self.detectTabbed(lines[i+1:])
175-
self.footnotes.setFootnote(id,
176-
footnote + "\n"
177-
+ "\n".join(detabbed))
178-
more_plain = self._handleFootnoteDefinitions(theRest)
179-
return plain + [""] + more_plain
180-
else :
181-
return lines
182-
183-
def _findFootnoteDefinition(self, lines):
184-
"""
185-
Find the parts of a footnote definition.
186-
187-
Keywords:
188161
189-
* lines: A list of lines of text.
162+
Return: A list of lines of text with footnote definitions removed.
190163
191-
Return: A three item tuple containing the index of the first line of a
192-
footnote definition, the id of the definition and the body of the
193-
definition.
194-
195164
"""
196-
counter = 0
197-
for line in lines:
198-
m = DEF_RE.match(line)
165+
newlines = []
166+
i = 0
167+
#import pdb; pdb.set_trace() #for i, line in enumerate(lines):
168+
while True:
169+
m = DEF_RE.match(lines[i])
199170
if m:
200-
return counter, m.group(2), m.group(3)
201-
counter += 1
202-
return counter, None, None
171+
fn, _i = self.detectTabbed(lines[i+1:])
172+
fn.insert(0, m.group(2))
173+
i += _i-1 # skip past footnote
174+
self.footnotes.setFootnote(m.group(1), "\n".join(fn))
175+
else:
176+
newlines.append(lines[i])
177+
if len(lines) > i+1:
178+
i += 1
179+
else:
180+
break
181+
return newlines
203182

204183
def detectTabbed(self, lines):
205184
""" Find indented text and remove indent before further proccesing.
@@ -208,12 +187,11 @@ def detectTabbed(self, lines):
208187
209188
* lines: an array of strings
210189
211-
Returns: a list of post processed items and the unused
212-
remainder of the original list
190+
Returns: a list of post processed items and the index of last line.
213191
214192
"""
215193
items = []
216-
item = -1
194+
blank_line = False # have we encountered a blank line yet?
217195
i = 0 # to keep track of where we are
218196

219197
def detab(line):
@@ -223,15 +201,21 @@ def detab(line):
223201

224202
for line in lines:
225203
if line.strip(): # Non-blank line
226-
line = detab(line)
227-
if line:
204+
detabbed_line = detab(line)
205+
if detabbed_line:
206+
items.append(detabbed_line)
207+
i += 1
208+
continue
209+
elif not blank_line and not DEF_RE.match(line):
210+
# not tabbed but still part of first par.
228211
items.append(line)
229212
i += 1
230213
continue
231214
else:
232-
return items, lines[i:]
215+
return items, i+1
233216

234217
else: # Blank line: _maybe_ we are done.
218+
blank_line = True
235219
i += 1 # advance
236220

237221
# Find the next non-blank line
@@ -250,7 +234,7 @@ def detab(line):
250234
else:
251235
i += 1
252236

253-
return items, lines[i:]
237+
return items, i
254238

255239

256240
class FootnotePattern(markdown.inlinepatterns.Pattern):

tests/extensions/extra/footnote.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
</li>
1414
<li id="fn:2">
1515
<blockquote>
16-
<p>This footnote is a blockquote.
17-
</p>
16+
<p>This footnote is a blockquote.</p>
1817
</blockquote>
1918
<p><a href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
2019
</li>
2120
<li id="fn:3">
22-
<p>A simple oneliner.
23-
&#160;<a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
21+
<p>A simple oneliner.&#160;<a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
2422
</li>
2523
<li id="fn:4">
2624
<p>A footnote with multiple paragraphs.</p>

tests/extensions/extra/footnote_placeholder.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
<hr />
33
<ol>
44
<li id="fn:1">
5-
<p>A Footnote.
6-
&#160;<a href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
5+
<p>A Footnote.&#160;<a href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
76
</li>
87
</ol>
98
</div>

tests/extensions/extra/named_markers.html

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
<hr />
66
<ol>
77
<li id="fn:foo">
8-
<p>Footnote marked <code>foo</code>.
9-
&#160;<a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
8+
<p>Footnote marked <code>foo</code>.&#160;<a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
109
</li>
1110
<li id="fn:bar">
12-
<p>This one is marked <em>bar</em>.
13-
&#160;<a href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
11+
<p>This one is marked <em>bar</em>.&#160;<a href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
1412
</li>
1513
<li id="fn:56">
16-
<p>A <strong>numbered</strong> footnote.
17-
&#160;<a href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
14+
<p>A <strong>numbered</strong> footnote.&#160;<a href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
1815
</li>
1916
<li id="fn:99">
20-
<p>The last one.
21-
&#160;<a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
17+
<p>The last one.&#160;<a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
2218
</li>
2319
</ol>
2420
</div>

0 commit comments

Comments
 (0)