Improve regex for bolding search terms

Co-authored by @DUOLabs333
main
Ben Busby 2021-10-26 16:15:24 -06:00
parent 6763c2e99d
commit 6decab5a51
No known key found for this signature in database
GPG Key ID: 339B7B7EB5333D14
1 changed files with 8 additions and 11 deletions

View File

@ -1,9 +1,8 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup, NavigableString
import os import os
import urllib.parse as urlparse import urllib.parse as urlparse
from urllib.parse import parse_qs from urllib.parse import parse_qs
import re import re
from bs4 import NavigableString
SKIP_ARGS = ['ref_src', 'utm'] SKIP_ARGS = ['ref_src', 'utm']
@ -52,16 +51,14 @@ def bold_search_terms(response: str, query: str) -> BeautifulSoup:
def replace_any_case(element: NavigableString, target_word: str) -> None: def replace_any_case(element: NavigableString, target_word: str) -> None:
# Replace all instances of the word, but maintaining the same case in # Replace all instances of the word, but maintaining the same case in
# the replacement # the replacement
if len(element) == len(target_word):
return
element.replace_with( element.replace_with(
element.replace( re.sub(r'\b((?![{}<>-])' + target_word + r'(?![{}<>-]))\b',
target_word.lower(), f'<b>{target_word.lower()}</b>' r'<b>\1</b>',
).replace( element,
target_word.capitalize(), f'<b>{target_word.capitalize()}</b>' flags=re.I)
).replace(
target_word.title(), f'<b>{target_word.title()}</b>'
).replace(
target_word.upper(), f'<b>{target_word.upper()}</b>'
)
) )
# Split all words out of query, grouping the ones wrapped in quotes # Split all words out of query, grouping the ones wrapped in quotes