From 926f286091fb9bbe31a91c7a7ec2999c188a8db4 Mon Sep 17 00:00:00 2001
From: "Thomas A. Christensen II" <25492070+MillironX@users.noreply.github.com>
Date: Thu, 31 Mar 2022 19:05:23 -0500
Subject: [PATCH] Add generic HTML element generator
Signed-off-by: Thomas A. Christensen II <25492070+MillironX@users.noreply.github.com>
---
src/Kelpie.jl | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/src/Kelpie.jl b/src/Kelpie.jl
index 3104778..fc8882a 100644
--- a/src/Kelpie.jl
+++ b/src/Kelpie.jl
@@ -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"))
+
+
+julia> prettyprint(html_element("span", "MillironX"; class="label-primary"))
+MillironX
+```
+"""
+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