From 2d4c05a824ebff1b17e715395922571b53a1a497 Mon Sep 17 00:00:00 2001 From: Peter Bieringer Date: Thu, 11 Jun 2026 08:54:10 +0200 Subject: [PATCH] sharing/bday/template: improve placeholder replacement --- radicale/item/__init__.py | 22 ++++++++++++++++------ radicale/sharing/__init__.py | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/radicale/item/__init__.py b/radicale/item/__init__.py index 94c9219f..fee43d1e 100644 --- a/radicale/item/__init__.py +++ b/radicale/item/__init__.py @@ -382,27 +382,37 @@ def verify(file: str, encoding: str): def replace_placeholders(text: str, placeholder_mapping: dict) -> str: + logger.trace("item/convert_vcf_to_ics: resolve placeholders: %r", text) + for placeholder in placeholder_mapping: text = text.replace(placeholder, placeholder_mapping[placeholder]) - # resolve {..|..} recursive - pattern = re.compile('(.*)(\\[)([^|]+)\\|(.+)(\\])(.*)') - logger.trace("item/convert_vcf_to_ics: resolve [..|..] starting with: %r", text) + logger.trace("item/convert_vcf_to_ics: resolve [..|..] in : %r", text) + + # resolve [..|..] recursive + pattern = re.compile('(.*)(\\[)([^|]+)\\|([^\\]]*)(\\])(.*)') while True: match = pattern.match(text) if not match: # nothing more todo break else: - if match[3].startswith('!') and match[3].endswith('!'): + logger.trace("item/convert_vcf_to_ics: resolve match : %r", match[0]) + # check for still unresolved placeholders + unresolved = False + for placeholder in VCF_TO_ICS_SUPPORTED_PLACEHOLDERS: + if "!" + placeholder + "!" in match[3]: + unresolved = True + break + if unresolved: # not resolved variable if '|' in match[4]: # further recursion required text = match[1] + match[2] + match[4] + match[5] + match[6] - logger.trace("item/convert_vcf_to_ics: resolve [..|..] match/replace/continue result: %r", text) + logger.trace("item/convert_vcf_to_ics: resolve continue : %r", text) else: text = match[1] + match[4] + match[6] - logger.trace("item/convert_vcf_to_ics: resolve [..|..] match/replace/final result: %r", text) + logger.trace("item/convert_vcf_to_ics: resolve final result: %r", text) break else: # resolved variable diff --git a/radicale/sharing/__init__.py b/radicale/sharing/__init__.py index 431d7b9d..ca4d331b 100644 --- a/radicale/sharing/__init__.py +++ b/radicale/sharing/__init__.py @@ -147,7 +147,7 @@ def check_template(data: Any) -> str: placeholder_mapping["{" + placeholder + "}"] = '!' + placeholder + '!' result = item.replace_placeholders(data, placeholder_mapping) - logger.trace("replace placeholders: %r -> %r", data, result) + logger.trace("replace placeholders result: %r -> %r", data, result) pattern = re.compile('.*{.*}.*') if pattern.search(result): raise ValueError("template contains unsupported placeholder {..}: %r" % result)