Skip to content

Commit

Permalink
Fix escape_except_blockquotes for greater than 9 block quotes in 1 st…
Browse files Browse the repository at this point in the history
…ring (#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 <[email protected]>
  • Loading branch information
jackgerrits and GitHub Action authored Feb 20, 2024
1 parent e47241f commit 3f20390
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/pydoc_markdown/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f20390

Please sign in to comment.