From f2c8fedb28c6d275c3c7e2a32d3176241fa4674f Mon Sep 17 00:00:00 2001 From: LuisFigueroaG Date: Wed, 7 Oct 2026 13:10:51 -0300 Subject: [PATCH 1/2] Don't apply smarty-pants inside inline HTML tags smarty-pants ran over the whole span text, so quotes in tag attributes and the dashes in HTML comments were turned into entities, breaking the markup (e.g. b became src="a.png” ...). Swap tags and comments out for placeholders while the substitutions run, which also makes the footnote-ref special case unnecessary. Fixes #150 --- CHANGES.md | 1 + lib/markdown2.py | 19 ++++++++++++++----- ...smarty_pants_html_attributes_issue150.html | 5 +++++ ...smarty_pants_html_attributes_issue150.opts | 1 + ...smarty_pants_html_attributes_issue150.text | 5 +++++ 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 test/tm-cases/smarty_pants_html_attributes_issue150.html create mode 100644 test/tm-cases/smarty_pants_html_attributes_issue150.opts create mode 100644 test/tm-cases/smarty_pants_html_attributes_issue150.text diff --git a/CHANGES.md b/CHANGES.md index 8911b5cb..cfebef0d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ - [pull #720] Add `wiki-links` extra for `[[Page Name]]` style links (#221) - [pull #722] Harden URL safety checks and sanitization in safe mode (#721) - [pull #728] Fix XSS from zero-padded colon entities in link URLs (#726) +- Fix `smarty-pants` extra converting quotes inside inline HTML tags and comments (#150) ## python-markdown2 2.5.5 diff --git a/lib/markdown2.py b/lib/markdown2.py index b3c29847..db497f5b 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -4167,6 +4167,18 @@ def run(self, text: str): for a discussion of some diversion from the original SmartyPants. """ + # Swap out inline HTML tags and comments so their attribute quotes + # and `--` are left alone (#150). The placeholders are non-space, so + # the quote rules see the same context around them as before. + tags = {} + + def hash_tag(match: re.Match[str]) -> str: + key = _hash_text(match.group(0)) + tags[key] = match.group(0) + return key + + text = self.md._sorta_html_tokenize_re.sub(hash_tag, text) + if "'" in text: # guard for perf text = self.contractions(text) text = self._opening_single_quote_re.sub("‘", text) @@ -4182,11 +4194,8 @@ def run(self, text: str): text = text.replace(" . . . ", "…") text = text.replace(". . .", "…") - # TODO: Temporary hack to fix https://github.com/trentm/python-markdown2/issues/150 - if "footnotes" in self.md.extras and "footnote-ref" in text: - # Quotes in the footnote back ref get converted to "smart" quotes - # Change them back here to ensure they work. - text = text.replace('class="footnote-ref”', 'class="footnote-ref"') + for key, tag in tags.items(): + text = text.replace(key, tag) return text diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.html b/test/tm-cases/smarty_pants_html_attributes_issue150.html new file mode 100644 index 00000000..cbc7a06d --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.html @@ -0,0 +1,5 @@ +

An inline image Alt Text keeps its attributes.

+ +

A raw link and a “quoted” word.

+ +

An inline is left alone, but “quotes” and dashes – outside it still change.

diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.opts b/test/tm-cases/smarty_pants_html_attributes_issue150.opts new file mode 100644 index 00000000..d0390c68 --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.opts @@ -0,0 +1 @@ +{"extras": ["smarty-pants"]} diff --git a/test/tm-cases/smarty_pants_html_attributes_issue150.text b/test/tm-cases/smarty_pants_html_attributes_issue150.text new file mode 100644 index 00000000..af3417f4 --- /dev/null +++ b/test/tm-cases/smarty_pants_html_attributes_issue150.text @@ -0,0 +1,5 @@ +An inline image Alt Text keeps its attributes. + +A raw link and a "quoted" word. + +An inline is left alone, but "quotes" and dashes -- outside it still change. From f8ac6839af11de3128899a3346f89141a8f78ba8 Mon Sep 17 00:00:00 2001 From: LuisFigueroaG Date: Wed, 7 Oct 2026 13:24:12 -0300 Subject: [PATCH 2/2] Add the PR number to the change note --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index cfebef0d..adcae02a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,7 +17,7 @@ - [pull #720] Add `wiki-links` extra for `[[Page Name]]` style links (#221) - [pull #722] Harden URL safety checks and sanitization in safe mode (#721) - [pull #728] Fix XSS from zero-padded colon entities in link URLs (#726) -- Fix `smarty-pants` extra converting quotes inside inline HTML tags and comments (#150) +- [pull #735] Fix `smarty-pants` extra converting quotes inside inline HTML tags and comments (#150) ## python-markdown2 2.5.5