A tiny mail-merge template engine
A bank in Johannesburg sends personalised letters. The letter template contains placeholders like {{ name | title }}. A placeholder starts at {{ and ends at the nearest following }} on the same line; a {{ without a closing }} on its line stays as plain text. The inserted text is never scanned again for placeholders.
Inside the braces, parts are separated by | and every part is trimmed of spaces. The first part is the variable name. Remaining parts are filters applied left to right: upper, lower, trim (strip spaces from both ends of the value), title (every space-separated word gets an upper-case first letter and lower-case rest) and default:TEXT. The default filter replaces the value by TEXT (trimmed) when the variable is missing or its value is empty or only spaces; later filters then work on that text. The other filters never change a missing variable. If after all filters a variable is still missing, the placeholder becomes <missing:name>.
Task: line 1 is K, then K lines key=value (split at the first =; the key is trimmed, the value is kept exactly as written, possibly empty). Then a line with M and M template lines. Print the rendered template lines.
Sample tests
Input
3
name= fatima al-RASHID
city=cape town
nick=
4
Dear {{ name | trim | title }},
Your branch in {{city|upper}} is ready.
We will call you {{ nick | default: friend | title }}.
Ref: {{ ref }} / {{ ref | upper }} / {{ ref | default:N/A }}Expected output
Dear Fatima Al-rashid, Your branch in CAPE TOWN is ready. We will call you Friend. Ref: <missing:ref> / <missing:ref> / N/A
Input
1
x=5
2
{{x}} + {{x}} = ten
unclosed {{ x and {{x}}Expected output
5 + 5 = ten
unclosed <missing:x and {{x>Your answer is also checked against 2 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
