Use only a single marker function

Multipe colors/expressions can instead be combined at definition time
This commit is contained in:
Kovid Goyal
2020-01-13 11:57:19 +05:30
parent 072cd29e3c
commit 35fb702833
9 changed files with 107 additions and 119 deletions

View File

@@ -21,10 +21,10 @@ def get_output_variables(left_address, right_address, color_address):
)
def marker_from_regex(expression, color):
def marker_from_regex(expression, color, flags=re.UNICODE):
color = max(1, min(color, 3))
if isinstance(expression, str):
pat = re.compile(expression)
pat = re.compile(expression, flags=flags)
else:
pat = expression
@@ -39,6 +39,28 @@ def marker_from_regex(expression, color):
return marker
def marker_from_multiple_regex(regexes, flags=re.UNICODE):
expr = ''
color_map = {}
for i, (color, spec) in enumerate(regexes):
grp = 'mcg{}'.format(i)
expr += '|(?P<{}>{})'.format(grp, spec)
color_map[grp] = color
expr = expr[1:]
pat = re.compile(expr, flags=flags)
def marker(text, left_address, right_address, color_address):
left, right, color = get_output_variables(left_address, right_address, color_address)
for match in pat.finditer(text):
left.value = match.start()
right.value = match.end() - 1
grp = next(k for k, v in match.groupdict().items() if v is not None)
color.value = color_map[grp]
yield
return marker
def marker_from_text(expression, color):
return marker_from_regex(re.escape(expression), color)