aboutsummaryrefslogtreecommitdiffstats
path: root/markdown/extensions
diff options
context:
space:
mode:
authorDmitry Shachnev <mitya57@gmail.com>2013-09-25 11:56:40 +0400
committerDmitry Shachnev <mitya57@gmail.com>2013-09-25 14:33:05 +0400
commited399ac3da4dddfd676bedada42a3aa4d4d80e1e (patch)
tree9d802df5c4cc744ac6c4915ea9c68faf3a9dabec /markdown/extensions
parent85e1a0c29c3f659fa1f36e94788a00b3e864b9d3 (diff)
downloadmarkdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.tar.gz
markdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.tar.bz2
markdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.zip
Add new "permalink" option to toc extension
and use it in our docs
Diffstat (limited to 'markdown/extensions')
-rw-r--r--markdown/extensions/toc.py44
1 files changed, 31 insertions, 13 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 516f2c5..eba0e69 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -89,16 +89,24 @@ class TocTreeprocessor(Treeprocessor):
yield parent, child
def add_anchor(self, c, elem_id): #@ReservedAssignment
- if self.use_anchors:
- anchor = etree.Element("a")
- anchor.text = c.text
- anchor.attrib["href"] = "#" + elem_id
- anchor.attrib["class"] = "toclink"
- c.text = ""
- for elem in c.getchildren():
- anchor.append(elem)
- c.remove(elem)
- c.append(anchor)
+ anchor = etree.Element("a")
+ anchor.text = c.text
+ anchor.attrib["href"] = "#" + elem_id
+ anchor.attrib["class"] = "toclink"
+ c.text = ""
+ for elem in c.getchildren():
+ anchor.append(elem)
+ c.remove(elem)
+ c.append(anchor)
+
+ def add_permalink(self, c, elem_id):
+ permalink = etree.Element("a")
+ permalink.text = ("\xb6" if self.use_permalinks is True
+ else self.use_permalinks)
+ permalink.attrib["href"] = "#" + elem_id
+ permalink.attrib["class"] = "headerlink"
+ permalink.attrib["title"] = "Permanent link"
+ c.append(permalink)
def build_toc_etree(self, div, toc_list):
# Add title to the div
@@ -128,6 +136,9 @@ class TocTreeprocessor(Treeprocessor):
header_rgx = re.compile("[Hh][123456]")
self.use_anchors = parseBoolValue(self.config["anchorlink"])
+ self.use_permalinks = parseBoolValue(self.config["permalink"], False)
+ if self.use_permalinks is None:
+ self.use_permalinks = self.config["permalink"]
# Get a list of id attributes
used_ids = set()
@@ -171,8 +182,11 @@ class TocTreeprocessor(Treeprocessor):
toc_list.append({'level': tag_level,
'id': elem_id,
'name': text})
-
- self.add_anchor(c, elem_id)
+
+ if self.use_anchors:
+ self.add_anchor(c, elem_id)
+ if self.use_permalinks:
+ self.add_permalink(c, elem_id)
toc_list_nested = order_toc_list(toc_list)
self.build_toc_etree(div, toc_list_nested)
@@ -202,7 +216,11 @@ class TocExtension(Extension):
"Defaults to None"],
"anchorlink" : [0,
"1 if header should be a self link"
- "Defaults to 0"]}
+ "Defaults to 0"],
+ "permalink" : [0,
+ "1 or link text if a Sphinx-style permalink should be added",
+ "Defaults to 0"]
+ }
for key, value in configs:
self.setConfig(key, value)