aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2011-05-04 13:46:39 -0400
committerWaylan Limberg <waylan@gmail.com>2011-05-04 13:46:39 -0400
commitfd54a3e34b2958d0690c1413e17232c26fa56302 (patch)
tree7b7499ffa6bce6c19449163769a08aa6c10a7e0e
parent1aedcafa01acede9a1822f5dcfd0b380d13dfaa5 (diff)
downloadmarkdown-fd54a3e34b2958d0690c1413e17232c26fa56302.tar.gz
markdown-fd54a3e34b2958d0690c1413e17232c26fa56302.tar.bz2
markdown-fd54a3e34b2958d0690c1413e17232c26fa56302.zip
Fix #4. Links in headers no longer munge up table of contents in TOC extension.
-rw-r--r--markdown/extensions/toc.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 2aeab4c..dac1eec 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -10,6 +10,8 @@ Dependencies:
"""
import markdown
from markdown.util import etree
+from markdown.inlinepatterns import LINK_RE, REFERENCE_RE, SHORT_REF_RE, \
+ AUTOLINK_RE, AUTOMAIL_RE
import re
class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
@@ -75,10 +77,15 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
level = tag_level
else:
level += 1
+
+ # Sanitize text. Replace all links with link lables (group 1).
+ text = c.text
+ for RE in [LINK_RE, REFERENCE_RE, SHORT_REF_RE, AUTOLINK_RE, AUTOMAIL_RE]:
+ text = re.sub(RE, '\g<1>', text)
# Do not override pre-existing ids
if not "id" in c.attrib:
- id = self.config["slugify"](c.text)
+ id = self.config["slugify"](text)
if id in used_ids:
ctr = 1
while "%s_%d" % (id, ctr) in used_ids:
@@ -92,12 +99,12 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
# List item link, to be inserted into the toc div
last_li = etree.Element("li")
link = etree.SubElement(last_li, "a")
- link.text = c.text
+ link.text = text
link.attrib["href"] = '#' + id
if int(self.config["anchorlink"]):
anchor = etree.SubElement(c, "a")
- anchor.text = c.text
+ anchor.text = text
anchor.attrib["href"] = "#" + id
anchor.attrib["class"] = "toclink"
c.text = ""