5
Blade is a PHP templating engine. For this challenge, you only need to implement a modified version one feature -- text replacement.
Challenge Description
Given an input file and a map of keys to their replacements, write an interpreter that reads through the file to find double open braces ({{
) followed by a key, followed by double closing braces (}}
). Whitespace inside the braces should be ignored, unless it is inside of the key.
Requirements
If a key is not found in the map, your program should exit without outputting anything
Keys must be nestable. See the examples for a valid nesting example.
Keys may contain any character, but cannot start or end with a space. Whitespace should be trimmed from the start and end of the key.
Example input/output
Input blade file:
Hello, {{user_{{username }}}}! It is { NICE } to meet you!
My name is {{ my n@m3}}!
Input map:
{
'user_Joe': 'Joseph, user #1234',
'username': 'Joe',
'my n@m3': 'Henry'
}
Expected output:
Hello, Joseph, user#1234! It is { NICE } to meet you!
My name is Henry!
Marking:
Feel free to hard code input/output and exclude it from your byte count. However, if you implement the following features, take the corresponding value away from your byte count.
Read filename of blade template from stdin or arguments: 15 bytes
Read variable mapping as JSON from stdin or arguments: 20 bytes
Read variable mapping in any other format from stdin or arguments: 12 bytes
Bonus
Allow calculations (+
, -
, *
, /
, %
, etc) in your blade script. Example input file:
{{a + b}}
Input map:
{ 'a': 1, 'b': 4 }
Expected output:
5
Keys may contain any character
including{
and}
? – feersum – 2015-11-09T01:29:57.547True. in fact, the following key should be valid: '<?>#{dfd }E}{}3' – Brian Hannay – 2015-11-09T03:55:36.143
Then how are you supposed to decide if
{{
or}}
is part of a set of braces or part of the name of a key? – feersum – 2015-11-09T04:15:49.773If there are two of the same brace in a row, it is safe to assume that it is part of a set. If there is only 1 in a row, it may be part of a key or part of the text left alone – Brian Hannay – 2015-11-09T04:25:19.983