Fix substituting multiple variables in templates. Fixes #6

This commit is contained in:
Tulir Asokan 2021-06-14 12:52:05 +03:00
parent 821e670fd5
commit e89a5773d8

View file

@ -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]: