Fix substituting multiple variables in templates. Fixes #6
This commit is contained in:
parent
821e670fd5
commit
e89a5773d8
1 changed files with 6 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
# reminder - A maubot plugin that reacts to messages that match predefined rules.
|
# reminder - A maubot plugin that reacts to messages that match predefined rules.
|
||||||
# Copyright (C) 2019 Tulir Asokan
|
# Copyright (C) 2021 Tulir Asokan
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
@ -68,13 +68,11 @@ class Template:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _replace_variables(tpl: str, variables: Dict[str, Any]) -> str:
|
def _replace_variables(tpl: str, variables: Dict[str, Any]) -> str:
|
||||||
for match in variable_regex.finditer(tpl):
|
full_var_match = variable_regex.fullmatch(tpl)
|
||||||
val = variables[match.group(1)]
|
if full_var_match:
|
||||||
if match.start() == 0 and match.end() == len(tpl):
|
|
||||||
# Whole field is a single variable, just return the value to allow non-string types.
|
# Whole field is a single variable, just return the value to allow non-string types.
|
||||||
return val
|
return variables[full_var_match.group(1)]
|
||||||
tpl = tpl[:match.start()] + val + tpl[match.end():]
|
return variable_regex.sub(lambda match: str(variables[match.group(1)]), tpl)
|
||||||
return tpl
|
|
||||||
|
|
||||||
def execute(self, evt: Event, rule_vars: Dict[str, Any], extra_vars: Dict[str, str]
|
def execute(self, evt: Event, rule_vars: Dict[str, Any], extra_vars: Dict[str, str]
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
|
|
Loading…
Reference in a new issue