Skip to content

Commit

Permalink
Fixed #129. Footnotes now output valid HTML5.
Browse files Browse the repository at this point in the history
As HTML5 has depreciated use of `rev=anything` and `rel=footnotes`, they are no
longer inlcuded in the output when the output_format is set to HTML5. Note that
if someone successful registers a spec for `rel=footnotes` in the future (as
a microformat), then that could be considered valid. But until that happens,
it is invlaid to use in HTML5. Therefore, we remove it from the output (when
outputing HTML% only).

As an alternative, two new classes are set (in all output_formats). On the link
to the footnote (where `rel=footnotes` was used), we set `class=footnote-ref`
and on the backlink (where `rev=footnote` was used), we set
`class=footnote-backref`.

Also updated the tests to reflect to the new classes in the output.
  • Loading branch information
Waylan Limberg committed Aug 9, 2012
1 parent b2939d1 commit 2fe5b5b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
9 changes: 7 additions & 2 deletions markdown/extensions/footnotes.py
Expand Up @@ -61,6 +61,7 @@ def extendMarkdown(self, md, md_globals):
""" 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")
Expand Down Expand Up @@ -133,7 +134,9 @@ def makeFootnotesDiv(self, root):
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
Expand Down Expand Up @@ -255,7 +258,9 @@ def handleMatch(self, m):
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:
Expand Down
12 changes: 6 additions & 6 deletions 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 />
Expand All @@ -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">&#8617;</a></p>
<p><a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</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">&#8617;</a></p>
<p><a class="footnote-backref" href="#fnref:2" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
<li id="fn:3">
<p>A simple oneliner.&#160;<a href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
<p>A simple oneliner.&#160;<a class="footnote-backref" href="#fnref:3" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
</li>
<li id="fn:4">
<p>A footnote with multiple paragraphs.</p>
<p>Paragraph two.&#160;<a href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
<p>Paragraph two.&#160;<a class="footnote-backref" href="#fnref:4" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
</li>
<li id="fn:5">
<p>First line of first paragraph.
Second line of first paragraph is not intended.
Nor is third...&#160;<a href="#fnref:5" rev="footnote" title="Jump back to footnote 5 in the text">&#8617;</a></p>
Nor is third...&#160;<a class="footnote-backref" href="#fnref:5" rev="footnote" title="Jump back to footnote 5 in the text">&#8617;</a></p>
</li>
</ol>
</div>
4 changes: 2 additions & 2 deletions tests/extensions/extra/footnote_placeholder.html
Expand Up @@ -2,8 +2,8 @@
<hr />
<ol>
<li id="fn:1">
<p>A Footnote.&#160;<a href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
<p>A Footnote.&#160;<a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</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>
<p>Some text with a footnote<sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup>.</p>
14 changes: 7 additions & 7 deletions 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>.&#160;<a href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
<p>Footnote marked <code>foo</code>.&#160;<a class="footnote-backref" href="#fnref:foo" rev="footnote" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
<li id="fn:bar">
<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>
<p>This one is marked <em>bar</em>.&#160;<a class="footnote-backref" href="#fnref:bar" rev="footnote" title="Jump back to footnote 2 in the text">&#8617;</a></p>
</li>
<li id="fn:56">
<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>
<p>A <strong>numbered</strong> footnote.&#160;<a class="footnote-backref" href="#fnref:56" rev="footnote" title="Jump back to footnote 3 in the text">&#8617;</a></p>
</li>
<li id="fn:99">
<p>The last one.&#160;<a href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
<p>The last one.&#160;<a class="footnote-backref" href="#fnref:99" rev="footnote" title="Jump back to footnote 4 in the text">&#8617;</a></p>
</li>
</ol>
</div>

0 comments on commit 2fe5b5b

Please sign in to comment.