Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed macro expansion when crystal spec is run. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/temel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ end
# Since we are not able to pass params to macros, we must pass all tags explicitly.

{% for html_tag in %w(a abbr address area article aside audio b base bdi bdo blockquote body br button canvas caption cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link map mark menu meta meter nav noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track u ul var video wbr) %}
tag {{html_tag.id}}
tag {{html_tag}}
{% end %}
30 changes: 15 additions & 15 deletions src/temel/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ macro attributes(list)
end

macro legalize_tag(name)
"#{ "{{name}}".gsub(/_/, '-') }"
"#{ "{{name.id}}".gsub(/_/, '-') }"
end

macro tag(name)
def {{name}}
"<#{ legalize_tag {{name.id}} } />"
def {{name.id}}
"<#{ legalize_tag {{name}} } />"
end
def {{name}}(attrs : NamedTuple)
"<#{ legalize_tag {{name.id}} } #{attributes attrs} />"
def {{name.id}}(attrs : NamedTuple)
"<#{ legalize_tag {{name}} } #{attributes attrs} />"
end
def {{name}}(content : String|Int)
"<#{ legalize_tag {{name.id}} }>#{content}</#{ legalize_tag {{name.id}} }>"
def {{name.id}}(content : String|Int)
"<#{ legalize_tag {{name}} }>#{content}</#{ legalize_tag {{name}} }>"
end

# Block Based DSL
def {{name}}(&block)
def {{name.id}}(&block)
content = yield
content = content.join "" if content.is_a? Array
"<#{ legalize_tag {{name.id}} }>#{content}</#{ legalize_tag {{name.id}} }>"
"<#{ legalize_tag {{name}} }>#{content}</#{ legalize_tag {{name}} }>"
end
def {{name}}(attrs : NamedTuple, &block)
def {{name.id}}(attrs : NamedTuple, &block)
content = yield
content = content.join "" if content.is_a? Array
"<#{ legalize_tag {{name.id}} } #{attributes attrs}>#{content}</#{ legalize_tag {{name.id}} }>"
"<#{ legalize_tag {{name}} } #{attributes attrs}>#{content}</#{ legalize_tag {{name}} }>"
end

# Argument Based DSL
def {{name}}(*elements)
"<#{ legalize_tag {{name.id}} }>#{elements.join ""}</#{ legalize_tag {{name.id}} }>"
def {{name.id}}(*elements)
"<#{ legalize_tag {{name}} }>#{elements.join ""}</#{ legalize_tag {{name}} }>"
end
def {{name}}(attrs : NamedTuple, *elements)
"<#{ legalize_tag {{name.id}} } #{attributes attrs}>#{elements.join ""}</#{ legalize_tag {{name.id}} }>"
def {{name.id}}(attrs : NamedTuple, *elements)
"<#{ legalize_tag {{name}} } #{attributes attrs}>#{elements.join ""}</#{ legalize_tag {{name}} }>"
end
end