Do not highlight sub words and ignore case when bold_keywords is given (#348)

This commit is contained in:
Alexandre Bassel
2025-02-09 12:01:07 -05:00
committed by GitHub
parent 6e0629c1ab
commit 375490cda5
2 changed files with 20 additions and 15 deletions

View File

@@ -183,10 +183,22 @@ class EntryType(abc.ABC):
def make_keywords_bold_in_a_string(string: str, keywords: list[str]) -> str:
"""Make the given keywords bold in the given string."""
replacement_map = {keyword: f"**{keyword}**" for keyword in keywords}
for keyword, replacement in replacement_map.items():
string = string.replace(keyword, replacement)
"""Make the given keywords bold in the given string, handling capitalization and substring issues.
Examples:
>>> make_keywords_bold_in_a_string("I know java and javascript", ["java"])
'I know **java** and javascript'
>>> make_keywords_bold_in_a_string("Experience with aws, Aws and AWS", ["aws"])
'Experience with **aws**, **Aws** and **AWS**'
"""
def bold_match(match):
return f"**{match.group(0)}**"
for keyword in keywords:
# Use re.escape to ensure special characters in keywords are handled
pattern = r'\b' + re.escape(keyword) + r'\b'
string = re.sub(pattern, bold_match, string, flags=re.IGNORECASE)
return string

View File

@@ -31,19 +31,12 @@ def make_given_keywords_bold_in_sections(
if sections_input is None:
return None
for section_title, entries in sections_input.items():
new_entries = []
for entry in entries:
for entries in sections_input.values():
for i, entry in enumerate(entries):
if isinstance(entry, str):
new_entry = entry_types.make_keywords_bold_in_a_string(entry, keywords)
entries[i] = entry_types.make_keywords_bold_in_a_string(entry, keywords)
elif callable(getattr(entry, "make_keywords_bold", None)):
new_entry = entry.make_keywords_bold(keywords) # type: ignore
else:
new_entry = entry
new_entries.append(new_entry)
sections_input[section_title] = new_entries
entries[i] = entry.make_keywords_bold(keywords) # type: ignore
return sections_input