diff options
-rw-r--r-- | markdown/__init__.py | 14 | ||||
-rw-r--r-- | markdown/extensions/footnotes.py | 9 | ||||
-rw-r--r-- | tests/extensions/extra/footnote.html | 12 | ||||
-rw-r--r-- | tests/extensions/extra/footnote_placeholder.html | 4 | ||||
-rw-r--r-- | tests/extensions/extra/named_markers.html | 14 |
5 files changed, 32 insertions, 21 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index b637c88..58993dd 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -238,11 +238,17 @@ class Markdown: def set_output_format(self, format): """ Set the output format for the class instance. """ + self.output_format = format.lower() try: - self.serializer = self.output_formats[format.lower()] - except KeyError: - raise KeyError('Invalid Output Format: "%s". Use one of %s.' \ - % (format, self.output_formats.keys())) + self.serializer = self.output_formats[self.output_format] + except KeyError, e: + valid_formats = self.output_formats.keys() + valid_formats.sort() + message = 'Invalid Output Format: "%s". Use one of %s.' \ + % (self.output_format, + '"' + '", "'.join(valid_formats) + '"') + e.args = (message,) + e.args[1:] + raise return self def convert(self, source): diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index 3d83807..cfe41ed 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -61,6 +61,7 @@ class FootnoteExtension(markdown.Extension): """ Add pieces to Markdown. """ md.registerExtension(self) self.parser = md.parser + self.md = md # Insert a preprocessor before ReferencePreprocessor md.preprocessors.add("footnote", FootnotePreprocessor(self), "<reference") @@ -133,7 +134,9 @@ class FootnoteExtension(markdown.Extension): self.parser.parseChunk(li, self.footnotes[id]) backlink = etree.Element("a") backlink.set("href", "#" + self.makeFootnoteRefId(id)) - backlink.set("rev", "footnote") + if self.md.output_format not in ['html5', 'xhtml5']: + 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.text = FN_BACKLINK_TEXT @@ -255,7 +258,9 @@ class FootnotePattern(markdown.inlinepatterns.Pattern): a = etree.SubElement(sup, "a") sup.set('id', self.footnotes.makeFootnoteRefId(id)) a.set('href', '#' + self.footnotes.makeFootnoteId(id)) - a.set('rel', 'footnote') + if self.footnotes.md.output_format not in ['html5', 'xhtml5']: + a.set('rel', 'footnote') # invalid in HTML5 + a.set('class', 'footnote-ref') a.text = unicode(self.footnotes.footnotes.index(id) + 1) return sup else: diff --git a/tests/extensions/extra/footnote.html b/tests/extensions/extra/footnote.html index 02dcc02..e5b41a7 100644 --- a/tests/extensions/extra/footnote.html +++ b/tests/extensions/extra/footnote.html @@ -1,4 +1,4 @@ -<p>This is the body with a footnote<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> or two<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup> or more<sup id="fnref:3"><a href="#fn:3" rel="footnote">3</a></sup> <sup id="fnref:4"><a href="#fn:4" rel="footnote">4</a></sup> <sup id="fnref:5"><a href="#fn:5" rel="footnote">5</a></sup>.</p> +<p>This is the body with a footnote<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup> or two<sup id="fnref:2"><a class="footnote-ref" href="#fn:2" rel="footnote">2</a></sup> or more<sup id="fnref:3"><a class="footnote-ref" href="#fn:3" rel="footnote">3</a></sup> <sup id="fnref:4"><a class="footnote-ref" href="#fn:4" rel="footnote">4</a></sup> <sup id="fnref:5"><a class="footnote-ref" href="#fn:5" rel="footnote">5</a></sup>.</p> <p>Also a reference that does not exist[^6].</p> <div class="footnote"> <hr /> @@ -9,25 +9,25 @@ <li>item 1</li> <li>item 2</li> </ul> -<p><a href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> +<p><a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> </li> <li id="fn:2"> <blockquote> <p>This footnote is a blockquote.</p> </blockquote> -<p><a href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> +<p><a class="footnote-backref" href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> </li> <li id="fn:3"> -<p>A simple oneliner. <a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> +<p>A simple oneliner. <a class="footnote-backref" href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> </li> <li id="fn:4"> <p>A footnote with multiple paragraphs.</p> -<p>Paragraph two. <a href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> +<p>Paragraph two. <a class="footnote-backref" href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> </li> <li id="fn:5"> <p>First line of first paragraph. Second line of first paragraph is not intended. -Nor is third... <a href="#fnref:5" rev="footnote" title="Jump back to footnote 5 in the text">↩</a></p> +Nor is third... <a class="footnote-backref" href="#fnref:5" rev="footnote" title="Jump back to footnote 5 in the text">↩</a></p> </li> </ol> </div>
\ No newline at end of file diff --git a/tests/extensions/extra/footnote_placeholder.html b/tests/extensions/extra/footnote_placeholder.html index 9c89391..0143145 100644 --- a/tests/extensions/extra/footnote_placeholder.html +++ b/tests/extensions/extra/footnote_placeholder.html @@ -2,8 +2,8 @@ <hr /> <ol> <li id="fn:1"> -<p>A Footnote. <a href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> +<p>A Footnote. <a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> </li> </ol> </div> -<p>Some text with a footnote<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.</p>
\ No newline at end of file +<p>Some text with a footnote<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup>.</p>
\ No newline at end of file diff --git a/tests/extensions/extra/named_markers.html b/tests/extensions/extra/named_markers.html index f643b7b..55cea31 100644 --- a/tests/extensions/extra/named_markers.html +++ b/tests/extensions/extra/named_markers.html @@ -1,20 +1,20 @@ -<p>This is the body with footnotes<sup id="fnref:foo"><a href="#fn:foo" rel="footnote">1</a></sup> -that have named<sup id="fnref:bar"><a href="#fn:bar" rel="footnote">2</a></sup> markers and -oddly<sup id="fnref:56"><a href="#fn:56" rel="footnote">3</a></sup> numbered<sup id="fnref:99"><a href="#fn:99" rel="footnote">4</a></sup> markers.</p> +<p>This is the body with footnotes<sup id="fnref:foo"><a class="footnote-ref" href="#fn:foo" rel="footnote">1</a></sup> +that have named<sup id="fnref:bar"><a class="footnote-ref" href="#fn:bar" rel="footnote">2</a></sup> markers and +oddly<sup id="fnref:56"><a class="footnote-ref" href="#fn:56" rel="footnote">3</a></sup> numbered<sup id="fnref:99"><a class="footnote-ref" href="#fn:99" rel="footnote">4</a></sup> markers.</p> <div class="footnote"> <hr /> <ol> <li id="fn:foo"> -<p>Footnote marked <code>foo</code>. <a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> +<p>Footnote marked <code>foo</code>. <a class="footnote-backref" href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p> </li> <li id="fn:bar"> -<p>This one is marked <em>bar</em>. <a href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> +<p>This one is marked <em>bar</em>. <a class="footnote-backref" href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">↩</a></p> </li> <li id="fn:56"> -<p>A <strong>numbered</strong> footnote. <a href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> +<p>A <strong>numbered</strong> footnote. <a class="footnote-backref" href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">↩</a></p> </li> <li id="fn:99"> -<p>The last one. <a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> +<p>The last one. <a class="footnote-backref" href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">↩</a></p> </li> </ol> </div>
\ No newline at end of file |