Merge pull request #2 from MillironX:splatting

Change html element functions to use argument splatting
pull/3/head
Thomas A. Christensen II 2 years ago committed by GitHub
commit 9a4b8364f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
- Arrays of content replaced with argument splatting ([#2](https://github.com/MillironX/Kelpie.jl/pull/2))
## [0.1.0] - 2022-04-04
### Added

@ -31,16 +31,16 @@ Node.
```julia
import EzXML: prettyprint
doc = html([
head([
doc = html(
head(
title("Kelpie.jl is awesome!"),
]),
body([
header([
),
body(
header(
h1("Dogs are cool"),
h2("Julia is cool"),
]),
main([
),
main(
img(;
src="/kelpie-on-sheep-back.jpg",
alt="A Kelpie herding sheep"
@ -49,9 +49,9 @@ doc = html([
p("Kelpies make great herding dogs for $animal.")
for animal in ["cows", "sheep", "chickens"]
]...,
]),
]),
])
),
),
)
prettyprint(doc)
```

@ -61,10 +61,10 @@ const HTML_ELEMENTS = [
]
"""
link_or_text!(node, content)
link_or_text!(node, content...)
Converts `content` to an `EzXML.TextNode` if it isn't already an `EzXML.Node` and links it
to `node`. Will link all nodes in `content` if content is a vector.
Converts each `content` to an `EzXML.TextNode` if it isn't already an `EzXML.Node` and links
it to `node`.
```jldoctest
julia> import EzXML: ElementNode, prettyprint; import Kelpie: link_or_text!
@ -77,30 +77,24 @@ julia> prettyprint(link_or_text!(ElementNode("div"), ElementNode("br")))
julia> prettyprint(link_or_text!(ElementNode("h1"), "Kelpie.jl"))
<h1>Kelpie.jl</h1>
julia> prettyprint(link_or_text!(ElementNode("div"), ["The end", ElementNode("hr")]))
julia> prettyprint(link_or_text!(ElementNode("div"), "The end", ElementNode("hr")))
<div>The end<hr/></div>
```
"""
function link_or_text!(node, content)
if typeof(content) <: EzXML.Node
link!(node, content)
else
link!(node, EzXML.TextNode(string(content)))
end #if
return node
end #function
function link_or_text!(node, content::AbstractArray)
function link_or_text!(node, content...)
for con in content
link_or_text!(node, con)
if typeof(con) <: EzXML.Node
link!(node, con)
else
link!(node, EzXML.TextNode(string(con)))
end #if
end #for
return node
end #function
"""
html_element(name, content=nothing; kwargs...)
html_element(name, content...=nothing; kwargs...)
Creates a new `EzXML.Node` with name `name`, containing `content`, and with attributes
specified by `kwargs`.
@ -117,7 +111,7 @@ julia> prettyprint(html_element("span", "MillironX"; class="label-primary"))
<span class="label-primary">MillironX</span>
```
"""
function html_element(name::AbstractString, content=nothing; kwargs...)
function html_element(name::AbstractString, content...=nothing; kwargs...)
el = EzXML.ElementNode(name)
for (key, value) in kwargs
@ -125,34 +119,34 @@ function html_element(name::AbstractString, content=nothing; kwargs...)
end #for
if !isnothing(content)
link_or_text!(el, content)
link_or_text!(el, content...)
end #if
return el
end #function
"""
html(content)
html(content...)
Creates a new HTML document filled with `content`.
"""
function html(content)
function html(content...)
doc = EzXML.HTMLDocumentNode(nothing, nothing)
link_or_text!(doc, content)
link_or_text!(doc, content...)
return doc
end #function
for symbol in HTML_ELEMENTS
name = string(symbol)
@eval function $symbol(content=nothing; kwargs...)
return html_element($name, content; kwargs...)
@eval function $symbol(content...=nothing; kwargs...)
return html_element($name, content...; kwargs...)
end #function
@eval export $symbol
end
function html_div(content=nothing; kwargs...)
function html_div(content...=nothing; kwargs...)
return html_element("div", content; kwargs...)
end #function

Loading…
Cancel
Save