mirror of
https://gitea.invidious.io/iv-org/shard-kilt.git
synced 2024-08-15 00:43:15 +00:00
Add ability to register template engine outside the library
This commit is contained in:
parent
5f23a69c15
commit
ee7059effd
3 changed files with 72 additions and 10 deletions
52
src/kilt.cr
52
src/kilt.cr
|
@ -2,17 +2,55 @@ require "ecr/macros"
|
|||
require "./kilt/*"
|
||||
|
||||
module Kilt
|
||||
# macro only constants
|
||||
TEMPLATES = [] of Int32
|
||||
OVERRIDES = [] of Int32
|
||||
|
||||
macro register_template(extension, embed_macro)
|
||||
{% ::Kilt::TEMPLATES << {extension, embed_macro} %}
|
||||
::Kilt._override_embed
|
||||
end
|
||||
|
||||
macro embed(filename, io_name = "__kilt_io__")
|
||||
{% if filename.ends_with?(".ecr") %}
|
||||
embed_ecr({{filename}}, {{io_name}})
|
||||
{% elsif filename.ends_with?(".slang") %}
|
||||
embed_slang({{filename}}, {{io_name}})
|
||||
{% else %}
|
||||
raise Kilt::Exception.new("Unsupported template type \"" + {{filename.split(".").last}} + "\"")
|
||||
{% end %}
|
||||
{{::Kilt::OVERRIDES.last.id}}.embed({{filename}}, {{io_name}})
|
||||
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}})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue