diff options
author | Waylan Limberg <waylan.limberg@icloud.com> | 2014-11-20 16:07:03 -0500 |
---|---|---|
committer | Waylan Limberg <waylan.limberg@icloud.com> | 2014-11-20 16:07:03 -0500 |
commit | 8f66a94eab1389d97041944ed24afd2bf7c4389c (patch) | |
tree | 10b53664076650be951468cbbb163f3d637e5891 /markdown/extensions/footnotes.py | |
parent | 0c2143819ef7de53be52f7a4d47e027ff194a9b4 (diff) | |
download | markdown-8f66a94eab1389d97041944ed24afd2bf7c4389c.tar.gz markdown-8f66a94eab1389d97041944ed24afd2bf7c4389c.tar.bz2 markdown-8f66a94eab1389d97041944ed24afd2bf7c4389c.zip |
Flake8 cleanup (mostly whitespace).
Got all but a couple files in the tests (ran out of time today).
Apparently I have been using some bad form for years (although a few
things seemed to look better before the update). Anyway, conformant now.
Diffstat (limited to 'markdown/extensions/footnotes.py')
-rw-r--r-- | markdown/extensions/footnotes.py | 102 |
1 files changed, 58 insertions, 44 deletions
diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index a59de97..4779f0c 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -4,12 +4,12 @@ Footnotes Extension for Python-Markdown Adds footnote handling to Python-Markdown. -See <https://pythonhosted.org/Markdown/extensions/footnotes.html> +See <https://pythonhosted.org/Markdown/extensions/footnotes.html> for documentation. Copyright The Python Markdown Project -License: [BSD](http://www.opensource.org/licenses/bsd-license.php) +License: [BSD](http://www.opensource.org/licenses/bsd-license.php) """ @@ -25,30 +25,32 @@ from ..odict import OrderedDict import re FN_BACKLINK_TEXT = "zz1337820767766393qq" -NBSP_PLACEHOLDER = "qq3936677670287331zz" +NBSP_PLACEHOLDER = "qq3936677670287331zz" DEF_RE = re.compile(r'[ ]{0,3}\[\^([^\]]*)\]:\s*(.*)') TABBED_RE = re.compile(r'((\t)|( ))(.*)') + class FootnoteExtension(Extension): """ Footnote Extension. """ - def __init__ (self, *args, **kwargs): + def __init__(self, *args, **kwargs): """ Setup configs. """ self.config = { 'PLACE_MARKER': - ["///Footnotes Go Here///", - "The text string that marks where the footnotes go"], + ["///Footnotes Go Here///", + "The text string that marks where the footnotes go"], 'UNIQUE_IDS': - [False, - "Avoid name collisions across " - "multiple calls to reset()."], + [False, + "Avoid name collisions across " + "multiple calls to reset()."], "BACKLINK_TEXT": - ["↩", - "The text string that links from the footnote to the reader's place."] + ["↩", + "The text string that links from the footnote " + "to the reader's place."] } super(FootnoteExtension, self).__init__(*args, **kwargs) - + # In multiple invocations, emit links that don't get tangled. self.unique_prefix = 0 @@ -60,23 +62,27 @@ class FootnoteExtension(Extension): self.parser = md.parser self.md = md # Insert a preprocessor before ReferencePreprocessor - md.preprocessors.add("footnote", FootnotePreprocessor(self), - "<reference") + md.preprocessors.add( + "footnote", FootnotePreprocessor(self), "<reference" + ) # Insert an inline pattern before ImageReferencePattern - FOOTNOTE_RE = r'\[\^([^\]]*)\]' # blah blah [^1] blah - md.inlinePatterns.add("footnote", FootnotePattern(FOOTNOTE_RE, self), - "<reference") + FOOTNOTE_RE = r'\[\^([^\]]*)\]' # blah blah [^1] blah + md.inlinePatterns.add( + "footnote", FootnotePattern(FOOTNOTE_RE, self), "<reference" + ) # Insert a tree-processor that would actually add the footnote div - # This must be before all other treeprocessors (i.e., inline and + # This must be before all other treeprocessors (i.e., inline and # codehilite) so they can run on the the contents of the div. - md.treeprocessors.add("footnote", FootnoteTreeprocessor(self), - "_begin") + md.treeprocessors.add( + "footnote", FootnoteTreeprocessor(self), "_begin" + ) # Insert a postprocessor after amp_substitute oricessor - md.postprocessors.add("footnote", FootnotePostprocessor(self), - ">amp_substitute") + md.postprocessors.add( + "footnote", FootnotePostprocessor(self), ">amp_substitute" + ) def reset(self): - """ Clear the footnotes on reset, and prepare for a distinct document. """ + """ Clear footnotes on reset, and prepare for distinct document. """ self.footnotes = OrderedDict() self.unique_prefix += 1 @@ -92,7 +98,7 @@ class FootnoteExtension(Extension): return child, element, False finder(child) return None - + res = finder(root) return res @@ -115,7 +121,8 @@ class FootnoteExtension(Extension): def makeFootnoteRefId(self, id): """ Return footnote back-link id. """ if self.getConfig("UNIQUE_IDS"): - return 'fnref%s%d-%s' % (self.get_separator(), self.unique_prefix, id) + return 'fnref%s%d-%s' % (self.get_separator(), + self.unique_prefix, id) else: return 'fnref%s%s' % (self.get_separator(), id) @@ -137,10 +144,13 @@ class FootnoteExtension(Extension): backlink = etree.Element("a") backlink.set("href", "#" + self.makeFootnoteRefId(id)) if self.md.output_format not in ['html5', 'xhtml5']: - backlink.set("rev", "footnote") # Invalid in HTML5 + backlink.set("rev", "footnote") # Invalid in HTML5 backlink.set("class", "footnote-backref") - backlink.set("title", "Jump back to footnote %d in the text" % \ - (self.footnotes.index(id)+1)) + backlink.set( + "title", + "Jump back to footnote %d in the text" % + (self.footnotes.index(id)+1) + ) backlink.text = FN_BACKLINK_TEXT if li.getchildren(): @@ -157,7 +167,7 @@ class FootnoteExtension(Extension): class FootnotePreprocessor(Preprocessor): """ Find all footnote references and store for later use. """ - def __init__ (self, footnotes): + def __init__(self, footnotes): self.footnotes = footnotes def run(self, lines): @@ -178,7 +188,7 @@ class FootnotePreprocessor(Preprocessor): if m: fn, _i = self.detectTabbed(lines[i+1:]) fn.insert(0, m.group(2)) - i += _i-1 # skip past footnote + i += _i-1 # skip past footnote self.footnotes.setFootnote(m.group(1), "\n".join(fn)) else: newlines.append(lines[i]) @@ -199,16 +209,16 @@ class FootnotePreprocessor(Preprocessor): """ items = [] - blank_line = False # have we encountered a blank line yet? - i = 0 # to keep track of where we are + blank_line = False # have we encountered a blank line yet? + i = 0 # to keep track of where we are def detab(line): match = TABBED_RE.match(line) if match: - return match.group(4) + return match.group(4) for line in lines: - if line.strip(): # Non-blank line + if line.strip(): # Non-blank line detabbed_line = detab(line) if detabbed_line: items.append(detabbed_line) @@ -222,23 +232,24 @@ class FootnotePreprocessor(Preprocessor): else: return items, i+1 - else: # Blank line: _maybe_ we are done. + else: # Blank line: _maybe_ we are done. blank_line = True - i += 1 # advance + 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 + next_line = lines[j] + break else: - break # There is no more text; we are done. + 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. + if detab(next_line): # Yes, more work to do. items.append("") continue else: - break # No, we are done. + break # No, we are done. else: i += 1 @@ -260,7 +271,7 @@ class FootnotePattern(Pattern): sup.set('id', self.footnotes.makeFootnoteRefId(id)) a.set('href', '#' + self.footnotes.makeFootnoteId(id)) if self.footnotes.md.output_format not in ['html5', 'xhtml5']: - a.set('rel', 'footnote') # invalid in HTML5 + a.set('rel', 'footnote') # invalid in HTML5 a.set('class', 'footnote-ref') a.text = text_type(self.footnotes.footnotes.index(id) + 1) return sup @@ -271,7 +282,7 @@ class FootnotePattern(Pattern): class FootnoteTreeprocessor(Treeprocessor): """ Build and append footnote div to end of document. """ - def __init__ (self, footnotes): + def __init__(self, footnotes): self.footnotes = footnotes def run(self, root): @@ -290,16 +301,19 @@ class FootnoteTreeprocessor(Treeprocessor): else: root.append(footnotesDiv) + class FootnotePostprocessor(Postprocessor): """ Replace placeholders with html entities. """ def __init__(self, footnotes): self.footnotes = footnotes def run(self, text): - text = text.replace(FN_BACKLINK_TEXT, self.footnotes.getConfig("BACKLINK_TEXT")) + text = text.replace( + FN_BACKLINK_TEXT, self.footnotes.getConfig("BACKLINK_TEXT") + ) return text.replace(NBSP_PLACEHOLDER, " ") + def makeExtension(*args, **kwargs): """ Return an instance of the FootnoteExtension """ return FootnoteExtension(*args, **kwargs) - |