MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / parseBoolValue

Function parseBoolValue

markdown/util.py:136–152  ·  view source on GitHub ↗

Parses a string representing a boolean value. If parsing was successful, returns `True` or `False`. If `preserve_none=True`, returns `True`, `False`, or `None`. If parsing was not successful, raises `ValueError`, or, if `fail_on_errors=False`, returns `None`.

(value: str | None, fail_on_errors: bool = True, preserve_none: bool = False)

Source from the content-addressed store, hash-verified

134
135
136def parseBoolValue(value: str | None, fail_on_errors: bool = True, preserve_none: bool = False) -> bool | None:
137 """Parses a string representing a boolean value. If parsing was successful,
138 returns `True` or `False`. If `preserve_none=True`, returns `True`, `False`,
139 or `None`. If parsing was not successful, raises `ValueError`, or, if
140 `fail_on_errors=False`, returns `None`."""
141 if not isinstance(value, str):
142 if preserve_none and value is None:
143 return value
144 return bool(value)
145 elif preserve_none and value.lower() == 'none':
146 return None
147 elif value.lower() in ('true', 'yes', 'y', 'on', '1'):
148 return True
149 elif value.lower() in ('false', 'no', 'n', 'off', '0', 'none'):
150 return False
151 elif fail_on_errors:
152 raise ValueError('Cannot parse bool value: %r' % value)
153
154
155def code_escape(text: str) -> str:

Callers 4

handle_attrsMethod · 0.85
__init__Method · 0.85
__init__Method · 0.85
setConfigMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected