aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/extensions/toc.py44
-rw-r--r--markdown/util.py2
-rwxr-xr-xsetup.py3
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
diff --git a/setup.py b/setup.py
index 826de9d..0702f6b 100755
--- a/setup.py
+++ b/setup.py
@@ -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':