diff options
author | Dmitry Shachnev <mitya57@gmail.com> | 2013-09-25 11:56:40 +0400 |
---|---|---|
committer | Dmitry Shachnev <mitya57@gmail.com> | 2013-09-25 14:33:05 +0400 |
commit | ed399ac3da4dddfd676bedada42a3aa4d4d80e1e (patch) | |
tree | 9d802df5c4cc744ac6c4915ea9c68faf3a9dabec | |
parent | 85e1a0c29c3f659fa1f36e94788a00b3e864b9d3 (diff) | |
download | markdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.tar.gz markdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.tar.bz2 markdown-ed399ac3da4dddfd676bedada42a3aa4d4d80e1e.zip |
Add new "permalink" option to toc extension
and use it in our docs
-rw-r--r-- | markdown/extensions/toc.py | 44 | ||||
-rw-r--r-- | markdown/util.py | 2 | ||||
-rwxr-xr-x | setup.py | 3 |
3 files changed, 34 insertions, 15 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) diff --git a/markdown/util.py b/markdown/util.py index 0027176..d0ef8a3 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -88,7 +88,7 @@ def parseBoolValue(value, fail_on_errors=True): """Parses a string representing bool value. If parsing was successful, returns True or False. If parsing was not successful, raises ValueError, or, if fail_on_errors=False, returns None.""" - if not isinstance(value, str): + if not isinstance(value, string_type): return bool(value) elif value.lower() in ('true', 'yes', 'y', 'on', '1'): return True @@ -144,7 +144,8 @@ class build_docs(Command): else: with codecs.open('docs/_template.html', encoding='utf-8') as f: template = f.read() - self.md = markdown.Markdown(extensions=['extra', 'toc', 'meta', 'admonition', 'smarty']) + self.md = markdown.Markdown( + extensions=['extra', 'toc(permalink=true)', 'meta', 'admonition', 'smarty']) for infile in self.docs: outfile, ext = os.path.splitext(infile) if ext == '.txt': |