Skip to content

Commit

Permalink
feat(utils): replace glyphs in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
musicEnfanthen committed Apr 29, 2024
1 parent 14616fb commit 6777b28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion convert_source_description/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def create_textcritics(self, soup: BeautifulSoup) -> TextCritics:
comment['measure'] = self.utils_helper.strip_tag_and_clean(columns_in_row[0], 'td')
comment['system'] = self.utils_helper.strip_tag_and_clean(columns_in_row[1], 'td')
comment['position'] = self.utils_helper.strip_tag_and_clean(columns_in_row[2], 'td')
comment['comment'] = self.utils_helper.strip_tag_and_clean(columns_in_row[3], 'td')
comment_text = self.utils_helper.strip_tag_and_clean(columns_in_row[3], 'td')
comment['comment'] = self.utils_helper.replace_glyphs(comment_text)

textcritics['comments'].append(comment)

Expand Down
22 changes: 22 additions & 0 deletions convert_source_description/utils_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ def find_siglum_indices(self, paras: List[Tag]) -> List[int]:

return siglum_indices

############################################
# Public method: replace_glyphs
############################################

def replace_glyphs(self, text: str) -> str:
"""
Replaces glyph strings in a given text.
Args:
text (str): The given text.
Returns:
str: The replaced text.
"""
glyphs = ["a", "b", "bb", "#", "x", "f", "ff", "fff", "p", "pp", "ppp"]
glyph_pattern = '|'.join(re.escape(glyph) for glyph in glyphs)

return re.sub(
rf'\[({glyph_pattern})\]',
lambda match: f"{{{{ref.getGlyph('{match.group(0)}')}}}}",
text)

############################################
# Public method: strip_tag_and_clean
############################################
Expand Down

0 comments on commit 6777b28

Please sign in to comment.