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:
parent
f7ee3e379a
commit
926f286091
1 changed files with 34 additions and 0 deletions
|
@ -2,6 +2,8 @@ module Kelpie
|
|||
|
||||
import EzXML: link!, EzXML
|
||||
|
||||
export html_element
|
||||
|
||||
"""
|
||||
link_or_text!(node, content)
|
||||
|
||||
|
@ -41,4 +43,36 @@ function link_or_text!(node, content::AbstractArray)
|
|||
return node
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue