1
0
Fork 0
mirror of https://github.com/MillironX/Kelpie.jl.git synced 2025-01-15 09:19:07 -05:00

Add generic HTML element generator

Signed-off-by: Thomas A. Christensen II <25492070+MillironX@users.noreply.github.com>
This commit is contained in:
Thomas A. Christensen II 2022-03-31 19:05:23 -05:00
parent f7ee3e379a
commit 926f286091
Signed by: millironx
GPG key ID: 139C07724802BC5D

View file

@ -2,6 +2,8 @@ module Kelpie
import EzXML: link!, EzXML import EzXML: link!, EzXML
export html_element
""" """
link_or_text!(node, content) link_or_text!(node, content)
@ -41,4 +43,36 @@ function link_or_text!(node, content::AbstractArray)
return node return node
end #function end #function
"""
html_element(name, content=nothing; kwargs...)
Creates a new `EzXML.Node` with name `name`, containing `content`, and with attributes
specified by `kwargs`.
# Example
```
julia> import EzXML: prettyprint
julia> prettyprint(html_element("img"; src="https://millironx.com/images/charolette.jpg"))
<img src="https://millironx.com/images/charolette.jpg"/>
julia> prettyprint(html_element("span", "MillironX"; class="label-primary"))
<span class="label-primary">MillironX</span>
```
"""
function html_element(name::AbstractString, content=nothing; kwargs...)
el = EzXML.ElementNode(name)
for (key, value) in kwargs
link!(el, EzXML.AttributeNode(replace(string(key), "_" => "-"), string(value)))
end #for
if !isnothing(content)
link_or_text!(el, content)
end #if
return el
end #function
end #module end #module