diff options
author | Waylan Limberg <waylan@gmail.com> | 2013-09-29 14:31:29 -0700 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2013-09-29 14:31:29 -0700 |
commit | 191d88b26c6bcb1cd9f66cb3a115e106366d1a55 (patch) | |
tree | 93804de49534fe52fd38f15e8cb0b39e08650a92 /markdown | |
parent | 62e5485c0f4292717f48b4c16cdd6894c891716f (diff) | |
parent | 635d2f71db191145d30cba4934ab7fa8f4d20509 (diff) | |
download | markdown-191d88b26c6bcb1cd9f66cb3a115e106366d1a55.tar.gz markdown-191d88b26c6bcb1cd9f66cb3a115e106366d1a55.tar.bz2 markdown-191d88b26c6bcb1cd9f66cb3a115e106366d1a55.zip |
Merge pull request #252 from mitya57/master
Enable anchorlinks in Python-Markdown documentation
Diffstat (limited to 'markdown')
-rw-r--r-- | markdown/extensions/headerid.py | 15 | ||||
-rw-r--r-- | markdown/extensions/smarty.py | 11 | ||||
-rw-r--r-- | markdown/extensions/toc.py | 48 | ||||
-rw-r--r-- | markdown/util.py | 13 |
4 files changed, 51 insertions, 36 deletions
diff --git a/markdown/extensions/headerid.py b/markdown/extensions/headerid.py index 0476f9e..8221fe1 100644 --- a/markdown/extensions/headerid.py +++ b/markdown/extensions/headerid.py @@ -78,7 +78,7 @@ from __future__ import absolute_import from __future__ import unicode_literals from . import Extension from ..treeprocessors import Treeprocessor -from ..util import HTML_PLACEHOLDER_RE +from ..util import HTML_PLACEHOLDER_RE, parseBoolValue import re import logging import unicodedata @@ -166,23 +166,14 @@ class HeaderIdTreeprocessor(Treeprocessor): def _get_meta(self): """ Return meta data suported by this ext as a tuple """ level = int(self.config['level']) - 1 - force = self._str2bool(self.config['forceid']) + force = parseBoolValue(self.config['forceid']) if hasattr(self.md, 'Meta'): if 'header_level' in self.md.Meta: level = int(self.md.Meta['header_level'][0]) - 1 if 'header_forceid' in self.md.Meta: - force = self._str2bool(self.md.Meta['header_forceid'][0]) + force = parseBoolValue(self.md.Meta['header_forceid'][0]) return level, force - def _str2bool(self, s, default=False): - """ Convert a string to a booleen value. """ - s = str(s) - if s.lower() in ['0', 'f', 'false', 'off', 'no', 'n']: - return False - elif s.lower() in ['1', 't', 'true', 'on', 'yes', 'y']: - return True - return default - class HeaderIdExtension(Extension): def __init__(self, configs): diff --git a/markdown/extensions/smarty.py b/markdown/extensions/smarty.py index 18f9217..a0737b7 100644 --- a/markdown/extensions/smarty.py +++ b/markdown/extensions/smarty.py @@ -68,6 +68,7 @@ from __future__ import unicode_literals from . import Extension from ..inlinepatterns import HtmlPattern +from ..util import parseBoolValue def canonicalize(regex): """ @@ -173,15 +174,7 @@ class SmartyExtension(Extension): 'smart_ellipses': [True, 'Educate ellipses'] } for key, value in configs: - if not isinstance(value, str): - value = bool(value) - elif value.lower() in ('true', 't', 'yes', 'y', '1'): - value = True - elif value.lower() in ('false', 'f', 'no', 'n', '0'): - value = False - else: - raise ValueError('Cannot parse bool value: %s' % value) - self.setConfig(key, value) + self.setConfig(key, parseBoolValue(value)) def _addPatterns(self, md, patterns, serie): for ind, pattern in enumerate(patterns): diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 99afba0..89468d6 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -13,7 +13,7 @@ from __future__ import absolute_import from __future__ import unicode_literals from . import Extension from ..treeprocessors import Treeprocessor -from ..util import etree +from ..util import etree, parseBoolValue, AMP_SUBSTITUTE from .headerid import slugify, unique, itertext, stashedHTML2text import re @@ -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 = ("%spara;" % AMP_SUBSTITUTE + 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 @@ -127,7 +135,10 @@ class TocTreeprocessor(Treeprocessor): div.attrib["class"] = "toc" header_rgx = re.compile("[Hh][123456]") - self.use_anchors = self.config["anchorlink"] in [1, '1', True, 'True', 'true'] + 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 d292aad..d0ef8a3 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -84,6 +84,19 @@ def isBlockLevel(tag): # Some ElementTree tags are not strings, so return False. return False +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, string_type): + return bool(value) + elif value.lower() in ('true', 'yes', 'y', 'on', '1'): + return True + elif value.lower() in ('false', 'no', 'n', 'off', '0'): + return False + elif fail_on_errors: + raise ValueError('Cannot parse bool value: %r' % value) + """ MISC AUXILIARY CLASSES ============================================================================= |