diff --git a/src/kilt.cr b/src/kilt.cr index 5e42bc8..aa87fea 100644 --- a/src/kilt.cr +++ b/src/kilt.cr @@ -2,59 +2,29 @@ require "ecr/macros" require "./kilt/*" module Kilt - # macro only constants - TEMPLATES = [] of Int32 - OVERRIDES = [] of Int32 + # macro only constant + TEMPLATES = {} of String => Int32 - macro register_template(extension, embed_macro) - {% ::Kilt::TEMPLATES << {extension, embed_macro} %} - ::Kilt._override_embed + macro register_template(ext, embed_macro) + {% ::Kilt::TEMPLATES[ext] = embed_macro %} end macro embed(filename, io_name = "__kilt_io__") - {{::Kilt::OVERRIDES.last.id}}.embed({{filename}}, {{io_name}}) + {% ext = filename.split(".").last %} + {% ext_with_dot = ".#{ext.id}" %} + + {% if ::Kilt::TEMPLATES[ext_with_dot] == nil %} + raise Kilt::Exception.new("Unsupported template type \"" + {{ext}} + "\"") + {% else %} + {{::Kilt::TEMPLATES[ext_with_dot]}}({{filename}}, {{io_name}}) + {% end %} end - macro _override_embed - {% override_name = "::Kilt::Override#{OVERRIDES.size}" %} - - module {{override_name.id}} - macro embed(filename, io_name) - {% start = true %} - - {% for template in ::Kilt::TEMPLATES %} - {% extension = template[0] %} - {% embed_macro = template[1] %} - - {% if start == true %} - {% start = false %} - - \{% if filename.ends_with?({{extension}}) %} - {{embed_macro.id}}(\{{filename}}, \{{io_name}}) - - {% else %} - - \{% elsif filename.ends_with?({{extension}}) %} - {{embed_macro.id}}(\{{filename}}, \{{io_name}}) - - {% end %} - {% end %} - - \{% else %} - raise Kilt::Exception.new("Unsupported template type \"" + \{{filename.split(".").last}} + "\"") - \{% {{:end.id}} %} - end - end - - {% ::Kilt::OVERRIDES << override_name %} - end - - register_template(".ecr", embed_ecr) - macro file(filename, io_name = "__kilt_io__") def to_s({{io_name.id}}) Kilt.embed({{filename}}, {{io_name}}) end end - end + +::Kilt.register_template(".ecr", embed_ecr)