From 3f20390f0c1bf3d4d26e2d9be86a60bf46ff625d Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Tue, 20 Feb 2024 18:14:06 -0500 Subject: [PATCH] Fix escape_except_blockquotes for greater than 9 block quotes in 1 string (#317) * Fix escape_except_blockquotes for greater than 9 block quotes in 1 string When there are more than 9 block quotes in a string `escape_except_blockquotes` will fail do to the fact that the `BLOCKQUOTE_TOKEN_{i}` replacement will incorrectly match for both `BLOCKQUOTE_TOKEN_1` and `BLOCKQUOTE_TOKEN_10` This just extends it to `BLOCKQUOTE_TOKEN_{i}_END` so this accidental match doesn't happen. * update changelog * Updated PR references in 1 changelogs. skip-checks: true --------- Co-authored-by: GitHub Action --- .changelog/_unreleased.toml | 7 +++++++ src/pydoc_markdown/util/misc.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.changelog/_unreleased.toml b/.changelog/_unreleased.toml index b56cc333..dcf1f154 100644 --- a/.changelog/_unreleased.toml +++ b/.changelog/_unreleased.toml @@ -18,3 +18,10 @@ type = "breaking change" description = "Drop Python 3.7 compatibility" author = "@NiklasRosenstein" pr = "https://github.com/NiklasRosenstein/pydoc-markdown/pull/304" + +[[entries]] +id = "78a8f6a1-eaaa-41cf-89f0-e746ddb42491" +type = "fix" +description = "Fix `escape_except_blockquotes` option for greater than 9 blockquotes in a docstring" +author = "@jackgerrits" +pr = "https://github.com/NiklasRosenstein/pydoc-markdown/pull/317" diff --git a/src/pydoc_markdown/util/misc.py b/src/pydoc_markdown/util/misc.py index bca5e077..8b98c044 100644 --- a/src/pydoc_markdown/util/misc.py +++ b/src/pydoc_markdown/util/misc.py @@ -16,13 +16,13 @@ def escape_except_blockquotes(string: str) -> str: # Replace all blockquotes with placeholder tokens to preserve their contents for i, match in enumerate(blockquote_matches): - string = string.replace(match, f"BLOCKQUOTE_TOKEN_{i}") + string = string.replace(match, f"BLOCKQUOTE_TOKEN_{i}_END") # Escape the remaining string escaped_string = html.escape(string) # Replace the placeholder tokens with their original contents for i, match in enumerate(blockquote_matches): - escaped_string = escaped_string.replace(f"BLOCKQUOTE_TOKEN_{i}", match) + escaped_string = escaped_string.replace(f"BLOCKQUOTE_TOKEN_{i}_END", match) return escaped_string