Simplify code using Hash at compile time

This commit is contained in:
Oleksii Fedorov 2016-02-15 01:41:53 +01:00
parent ee7059effd
commit 2ccb346bf4

View file

@ -2,59 +2,29 @@ require "ecr/macros"
require "./kilt/*" require "./kilt/*"
module Kilt module Kilt
# macro only constants # macro only constant
TEMPLATES = [] of Int32 TEMPLATES = {} of String => Int32
OVERRIDES = [] of Int32
macro register_template(extension, embed_macro) macro register_template(ext, embed_macro)
{% ::Kilt::TEMPLATES << {extension, embed_macro} %} {% ::Kilt::TEMPLATES[ext] = embed_macro %}
::Kilt._override_embed
end end
macro embed(filename, io_name = "__kilt_io__") 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 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__") macro file(filename, io_name = "__kilt_io__")
def to_s({{io_name.id}}) def to_s({{io_name.id}})
Kilt.embed({{filename}}, {{io_name}}) Kilt.embed({{filename}}, {{io_name}})
end end
end end
end end
::Kilt.register_template(".ecr", embed_ecr)