2026-04-02 13:35:41 +02:00

22 lines
898 B
Python

from fledermaus_formatter.table_handler.Entry import Entry
from copy import deepcopy
class FindAndReplace:
def __init__(self, map: dict[str, list[str]], reqire_full_match: bool = False):
self.map = map
self.require_full_match = reqire_full_match
def execute_on_cell(self, entry) -> str:
res = deepcopy(entry.value)
for correct_name, bad_names in self.map.items():
for bad_name in bad_names:
if self.require_full_match and bad_name == res:
res = correct_name
break
elif not self.require_full_match:
prior_val = res
res = res.replace(bad_name, correct_name)
if prior_val != res or correct_name in res:
break
return Entry(value=res, fg_color=entry.fg_color, bg_color=entry.bg_color)