aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Shachnev <mitya57@gmail.com>2013-09-25 11:39:16 +0400
committerDmitry Shachnev <mitya57@gmail.com>2013-09-25 11:49:56 +0400
commit85e1a0c29c3f659fa1f36e94788a00b3e864b9d3 (patch)
tree2fd98a181ad9462b60911b2e3a37ef5688a33a10
parente92d7cf65e394b378cd3f9a370fb8e0761e7f62c (diff)
downloadmarkdown-85e1a0c29c3f659fa1f36e94788a00b3e864b9d3.tar.gz
markdown-85e1a0c29c3f659fa1f36e94788a00b3e864b9d3.tar.bz2
markdown-85e1a0c29c3f659fa1f36e94788a00b3e864b9d3.zip
Add new utility function parseBoolValue()
and use it in all extension that need parsing bool config values.
-rw-r--r--markdown/extensions/headerid.py15
-rw-r--r--markdown/extensions/smarty.py11
-rw-r--r--markdown/extensions/toc.py4
-rw-r--r--markdown/util.py13
4 files changed, 20 insertions, 23 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..516f2c5 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
from .headerid import slugify, unique, itertext, stashedHTML2text
import re
@@ -127,7 +127,7 @@ 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"])
# Get a list of id attributes
used_ids = set()
diff --git a/markdown/util.py b/markdown/util.py
index d292aad..0027176 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, str):
+ 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
=============================================================================