diff --git a/docs/changelog.md b/docs/changelog.md index 828d283cb..afd33b302 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,9 @@ See the [Contributing Guide](contributing.md) for details. ### Fixed +* The `fenced_code` extension no longer raises `TypeError` for boolean options such as + `linenums` set in the `attr_list` of a code block when `codehilite` is not + enabled; they are now serialized the same way the serializer does (#1713). * The `toc` extension no longer raises `TypeError` for a heading whose `data-toc-label` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 944b4e42b..8afc20518 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -27,7 +27,7 @@ from .codehilite import CodeHilite, CodeHiliteExtension, parse_hl_lines from .attr_list import get_attrs_and_remainder, sanitize_name, AttrListExtension from ..util import parseBoolValue -from ..serializers import _escape_attrib_html +from ..serializers import _escape_attrib_html, BOOLEAN_ATTRIBUTES import re from typing import TYPE_CHECKING, Any, Iterable @@ -160,7 +160,7 @@ def run(self, lines: list[str]) -> list[str]: # pairs were defined on the code block, and the `use_pygments` key was not set to `True`. The # `use_pygments` key could be either set to `False` or not defined. It is omitted from output. kv_pairs = ''.join( - f' {sanitize_name(k)}="{_escape_attrib_html(v)}"' + self._serialize_attr(sanitize_name(k), v) for k, v in config.items() if k != 'use_pygments' ) code = self._escape(m.group('code')) @@ -199,6 +199,18 @@ def handle_attrs( configs[k] = v return id, classes, configs + def _serialize_attr(self, key: str, value: Any) -> str: + """ Serialize an attribute the same way the serializer does. """ + if value is None: + return f' {key}' + if value is True: + if self.md.output_format == 'html' and key in BOOLEAN_ATTRIBUTES['html']: + return f' {key}' + return f' {key}="{key}"' + if value is False: + return '' + return f' {key}="{_escape_attrib_html(value)}"' + def _escape(self, txt: str) -> str: """ basic html escaping """ txt = txt.replace('&', '&') diff --git a/tests/test_syntax/extensions/test_fenced_code.py b/tests/test_syntax/extensions/test_fenced_code.py index 43a691591..65b5e1ec5 100644 --- a/tests/test_syntax/extensions/test_fenced_code.py +++ b/tests/test_syntax/extensions/test_fenced_code.py @@ -304,6 +304,56 @@ def testFencedLanguageNoCodehiliteWithAttrList(self): extensions=['fenced_code', 'attr_list'] ) + def testFencedCodeBoolAttrsNoCodehiliteWithAttrList(self): + # Boolean options are serialized like the serializer would (see #1713). + for output_format in ('html', 'xhtml'): + with self.subTest(output_format=output_format): + self.assertMarkdownRenders( + self.dedent( + ''' + ``` { .python linenums guess_lang=true noclasses=false } + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                        
+ ''' + ), + extensions=['fenced_code', 'attr_list'], + output_format=output_format + ) + + def testFencedCodeBoolAttrsHtmlBooleanAttributes(self): + # A `True` value of an HTML boolean attribute is a bare key in `html` and `key="key"` in `xhtml`. + md = markdown.Markdown(extensions=['fenced_code', 'attr_list'], output_format='html') + pre = md.preprocessors['fenced_code_block'] + self.assertEqual(pre._serialize_attr('hidden', True), ' hidden') + self.assertEqual(pre._serialize_attr('linenums', True), ' linenums="linenums"') + md = markdown.Markdown(extensions=['fenced_code', 'attr_list'], output_format='xhtml') + pre = md.preprocessors['fenced_code_block'] + self.assertEqual(pre._serialize_attr('hidden', True), ' hidden="hidden"') + + def testFencedCodeBoolAttrsCodehilite(self): + self.assertMarkdownRenders( + self.dedent( + ''' + ``` { .python linenums=false } + # Some python code + ``` + ''' + ), + self.dedent( + ''' +
# Some python code
+                
+ ''' + ), + extensions=['codehilite', 'fenced_code', 'attr_list'] + ) + def testFencedCodeAttrNameSanitizedNoCodehiliteWithAttrList(self): # An attribute name is sanitized so it cannot break out of the `code` tag (see #1663). self.assertMarkdownRenders(