From 6777b288c505ff14162c2d5bf03363502b4ded70 Mon Sep 17 00:00:00 2001 From: musicEnfanthen Date: Mon, 29 Apr 2024 13:08:06 +0200 Subject: [PATCH] feat(utils): replace glyphs in comments --- convert_source_description/utils.py | 3 ++- convert_source_description/utils_helper.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/convert_source_description/utils.py b/convert_source_description/utils.py index 158e80d..aa9ee7c 100644 --- a/convert_source_description/utils.py +++ b/convert_source_description/utils.py @@ -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) diff --git a/convert_source_description/utils_helper.py b/convert_source_description/utils_helper.py index 5784853..fb89ceb 100644 --- a/convert_source_description/utils_helper.py +++ b/convert_source_description/utils_helper.py @@ -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 ############################################