16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/pray/substitute.rb', line 16
def substitute_pray_symbols(text, symbols)
lookup = symbols.is_a?(Hash) ? symbols : symbols.to_h
output = +""
rest = text
loop do
start = rest.index(PLACEHOLDER_PREFIX)
unless start
output << rest
return output
end
output << rest[0...start]
after_prefix = rest[(start + PLACEHOLDER_PREFIX.length)..]
end_index = after_prefix.index(PLACEHOLDER_SUFFIX)
raise Error.render("unclosed ((pray:...) placeholder") unless end_index
path = after_prefix[0...end_index]
unless pray_symbol_key?(path)
raise Error.render("invalid ((pray:...)) path `#{path}`")
end
value = lookup[path]
unless value
raise Error.render(
"unknown pray symbol `#{path}`; declare it in `pray do ... end`"
)
end
output << value
rest = after_prefix[(end_index + PLACEHOLDER_SUFFIX.length)..]
end
end
|