Find all footnote references and store for later use.
| 145 | |
| 146 | |
| 147 | class FootnotePreprocessor(markdown.preprocessors.Preprocessor): |
| 148 | """ Find all footnote references and store for later use. """ |
| 149 | |
| 150 | def __init__ (self, footnotes): |
| 151 | self.footnotes = footnotes |
| 152 | |
| 153 | def run(self, lines): |
| 154 | lines = self._handleFootnoteDefinitions(lines) |
| 155 | text = "\n".join(lines) |
| 156 | return text.split("\n") |
| 157 | |
| 158 | def _handleFootnoteDefinitions(self, lines): |
| 159 | """ |
| 160 | Recursively find all footnote definitions in lines. |
| 161 | |
| 162 | Keywords: |
| 163 | |
| 164 | * lines: A list of lines of text |
| 165 | |
| 166 | Return: A list of lines with footnote definitions removed. |
| 167 | |
| 168 | """ |
| 169 | i, id, footnote = self._findFootnoteDefinition(lines) |
| 170 | |
| 171 | if id : |
| 172 | plain = lines[:i] |
| 173 | detabbed, theRest = self.detectTabbed(lines[i+1:]) |
| 174 | self.footnotes.setFootnote(id, |
| 175 | footnote + "\n" |
| 176 | + "\n".join(detabbed)) |
| 177 | more_plain = self._handleFootnoteDefinitions(theRest) |
| 178 | return plain + [""] + more_plain |
| 179 | else : |
| 180 | return lines |
| 181 | |
| 182 | def _findFootnoteDefinition(self, lines): |
| 183 | """ |
| 184 | Find the parts of a footnote definition. |
| 185 | |
| 186 | Keywords: |
| 187 | |
| 188 | * lines: A list of lines of text. |
| 189 | |
| 190 | Return: A three item tuple containing the index of the first line of a |
| 191 | footnote definition, the id of the definition and the body of the |
| 192 | definition. |
| 193 | |
| 194 | """ |
| 195 | counter = 0 |
| 196 | for line in lines: |
| 197 | m = DEF_RE.match(line) |
| 198 | if m: |
| 199 | return counter, m.group(2), m.group(3) |
| 200 | counter += 1 |
| 201 | return counter, None, None |
| 202 | |
| 203 | def detectTabbed(self, lines): |
| 204 | """ Find indented text and remove indent before further proccesing. |