diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-03-08 03:27:56 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-03-08 03:27:56 -0500 |
commit | 623292eb16593a02e11786116ab6efd5a14047fe (patch) | |
tree | 1fa00d849c6fe304c93bcb1afdeb19ca56e7429e /src/guff/template.cr | |
parent | 4a5da91ee4df8b6ca886e4c2433fb04bc6499041 (diff) | |
download | old-guff-623292eb16593a02e11786116ab6efd5a14047fe.tar.bz2 old-guff-623292eb16593a02e11786116ab6efd5a14047fe.zip |
lots of fixes
Diffstat (limited to 'src/guff/template.cr')
-rw-r--r-- | src/guff/template.cr | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/guff/template.cr b/src/guff/template.cr index 4b54fb6..f5a1693 100644 --- a/src/guff/template.cr +++ b/src/guff/template.cr @@ -7,16 +7,19 @@ module Guff @has_keys = @tokens.select { |t| t.type == :key }.size > 0 end - def run(args = nil : Hash(String, String)?) : String + def run(args : Nil) : String + raise_missing if @has_keys + + # return literal string + @string + end + + def run( + args = {} of String => String : Hash(String, String) + ) : String if @has_keys # check template args - if !args || args.size == 0 - raise "missing template args: %s" % [@tokens.select { |t| - t.type == :key - }.map { |t| - t.value - }.sort.join(", ")] - end + raise_missing if args.size == 0 # build result String.build do |r| @@ -30,6 +33,14 @@ module Guff end end + private def raise_missing + raise "missing template args: %s" % [@tokens.select { |t| + t.type == :key + }.map { |t| + t.value + }.sort.join(", ")] + end + SCAN_RE = %r{ # match key (?:%\{(?<key>[^\}]+)\}) |