#11516: Tweak fix to ensure proper highlighting

This commit is contained in:
jeremystretch
2023-01-20 13:43:47 -05:00
parent fcd0481b09
commit 5911041777
2 changed files with 20 additions and 3 deletions

View File

@@ -514,11 +514,21 @@ def clean_html(html, schemes):
def highlight_string(value, highlight, trim_pre=None, trim_post=None, trim_placeholder='...'):
"""
Highlight a string within a string and optionally trim the pre/post portions of the original string.
Args:
value: The body of text being searched against
highlight: The string of compiled regex pattern to highlight in `value`
trim_pre: Maximum length of pre-highlight text to include
trim_post: Maximum length of post-highlight text to include
trim_placeholder: String value to swap in for trimmed pre/post text
"""
# Split value on highlight string
try:
pre, match, post = re.split(fr'({re.escape(highlight)})', value, maxsplit=1, flags=re.IGNORECASE)
except ValueError:
if type(highlight) is re.Pattern:
pre, match, post = highlight.split(value, maxsplit=1)
else:
pre, match, post = re.split(fr'({highlight})', value, maxsplit=1, flags=re.IGNORECASE)
except ValueError as e:
# Match not found
return escape(value)