29
29
30
30
FN_BACKLINK_TEXT = "zz1337820767766393qq"
31
31
NBSP_PLACEHOLDER = "qq3936677670287331zz"
32
- DEF_RE = re .compile (r'(\ ?\ ?\ ?) \[\^([^\]]*)\]:\s*(.*)' )
32
+ DEF_RE = re .compile (r'[ ]{0,3} \[\^([^\]]*)\]:\s*(.*)' )
33
33
TABBED_RE = re .compile (r'((\t)|( ))(.*)' )
34
34
35
35
class FootnoteExtension (markdown .Extension ):
@@ -152,54 +152,33 @@ def __init__ (self, footnotes):
152
152
self .footnotes = footnotes
153
153
154
154
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 ):
160
155
"""
161
- Recursively find all footnote definitions in lines .
156
+ Loop through lines and find, set, and remove footnote definitions .
162
157
163
158
Keywords:
164
159
165
160
* 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:
188
161
189
- * lines : A list of lines of text.
162
+ Return : A list of lines of text with footnote definitions removed .
190
163
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
-
195
164
"""
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 ])
199
170
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
203
182
204
183
def detectTabbed (self , lines ):
205
184
""" Find indented text and remove indent before further proccesing.
@@ -208,12 +187,11 @@ def detectTabbed(self, lines):
208
187
209
188
* lines: an array of strings
210
189
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.
213
191
214
192
"""
215
193
items = []
216
- item = - 1
194
+ blank_line = False # have we encountered a blank line yet?
217
195
i = 0 # to keep track of where we are
218
196
219
197
def detab (line ):
@@ -223,15 +201,21 @@ def detab(line):
223
201
224
202
for line in lines :
225
203
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.
228
211
items .append (line )
229
212
i += 1
230
213
continue
231
214
else :
232
- return items , lines [ i :]
215
+ return items , i + 1
233
216
234
217
else : # Blank line: _maybe_ we are done.
218
+ blank_line = True
235
219
i += 1 # advance
236
220
237
221
# Find the next non-blank line
@@ -250,7 +234,7 @@ def detab(line):
250
234
else :
251
235
i += 1
252
236
253
- return items , lines [ i :]
237
+ return items , i
254
238
255
239
256
240
class FootnotePattern (markdown .inlinepatterns .Pattern ):
0 commit comments