diff options
author | Dmitry Shachnev <mitya57@gmail.com> | 2013-09-25 11:39:16 +0400 |
---|---|---|
committer | Dmitry Shachnev <mitya57@gmail.com> | 2013-09-25 11:49:56 +0400 |
commit | 85e1a0c29c3f659fa1f36e94788a00b3e864b9d3 (patch) | |
tree | 2fd98a181ad9462b60911b2e3a37ef5688a33a10 /markdown/util.py | |
parent | e92d7cf65e394b378cd3f9a370fb8e0761e7f62c (diff) | |
download | markdown-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.
Diffstat (limited to 'markdown/util.py')
-rw-r--r-- | markdown/util.py | 13 |
1 files changed, 13 insertions, 0 deletions
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 ============================================================================= |