diff options
Diffstat (limited to 'markdown/extensions/toc.py')
-rw-r--r-- | markdown/extensions/toc.py | 44 |
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) |