mirror of
https://github.com/MillironX/Kelpie.jl.git
synced 2025-01-15 09:19:07 -05:00
Merge pull request #2 from MillironX:splatting
Change html element functions to use argument splatting
This commit is contained in:
commit
9a4b8364f6
3 changed files with 33 additions and 35 deletions
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Arrays of content replaced with argument splatting ([#2](https://github.com/MillironX/Kelpie.jl/pull/2))
|
||||||
|
|
||||||
## [0.1.0] - 2022-04-04
|
## [0.1.0] - 2022-04-04
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
20
README.md
20
README.md
|
@ -31,16 +31,16 @@ Node.
|
||||||
```julia
|
```julia
|
||||||
import EzXML: prettyprint
|
import EzXML: prettyprint
|
||||||
|
|
||||||
doc = html([
|
doc = html(
|
||||||
head([
|
head(
|
||||||
title("Kelpie.jl is awesome!"),
|
title("Kelpie.jl is awesome!"),
|
||||||
]),
|
),
|
||||||
body([
|
body(
|
||||||
header([
|
header(
|
||||||
h1("Dogs are cool"),
|
h1("Dogs are cool"),
|
||||||
h2("Julia is cool"),
|
h2("Julia is cool"),
|
||||||
]),
|
),
|
||||||
main([
|
main(
|
||||||
img(;
|
img(;
|
||||||
src="/kelpie-on-sheep-back.jpg",
|
src="/kelpie-on-sheep-back.jpg",
|
||||||
alt="A Kelpie herding sheep"
|
alt="A Kelpie herding sheep"
|
||||||
|
@ -49,9 +49,9 @@ doc = html([
|
||||||
p("Kelpies make great herding dogs for $animal.")
|
p("Kelpies make great herding dogs for $animal.")
|
||||||
for animal in ["cows", "sheep", "chickens"]
|
for animal in ["cows", "sheep", "chickens"]
|
||||||
]...,
|
]...,
|
||||||
]),
|
),
|
||||||
]),
|
),
|
||||||
])
|
)
|
||||||
|
|
||||||
prettyprint(doc)
|
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
|
Converts each `content` to an `EzXML.TextNode` if it isn't already an `EzXML.Node` and links
|
||||||
to `node`. Will link all nodes in `content` if content is a vector.
|
it to `node`.
|
||||||
|
|
||||||
```jldoctest
|
```jldoctest
|
||||||
julia> import EzXML: ElementNode, prettyprint; import Kelpie: link_or_text!
|
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"))
|
julia> prettyprint(link_or_text!(ElementNode("h1"), "Kelpie.jl"))
|
||||||
<h1>Kelpie.jl</h1>
|
<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>
|
<div>The end<hr/></div>
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
function link_or_text!(node, content)
|
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)
|
|
||||||
for con in 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
|
end #for
|
||||||
|
|
||||||
return node
|
return node
|
||||||
end #function
|
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
|
Creates a new `EzXML.Node` with name `name`, containing `content`, and with attributes
|
||||||
specified by `kwargs`.
|
specified by `kwargs`.
|
||||||
|
@ -117,7 +111,7 @@ julia> prettyprint(html_element("span", "MillironX"; class="label-primary"))
|
||||||
<span class="label-primary">MillironX</span>
|
<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)
|
el = EzXML.ElementNode(name)
|
||||||
|
|
||||||
for (key, value) in kwargs
|
for (key, value) in kwargs
|
||||||
|
@ -125,34 +119,34 @@ function html_element(name::AbstractString, content=nothing; kwargs...)
|
||||||
end #for
|
end #for
|
||||||
|
|
||||||
if !isnothing(content)
|
if !isnothing(content)
|
||||||
link_or_text!(el, content)
|
link_or_text!(el, content...)
|
||||||
end #if
|
end #if
|
||||||
|
|
||||||
return el
|
return el
|
||||||
end #function
|
end #function
|
||||||
|
|
||||||
"""
|
"""
|
||||||
html(content)
|
html(content...)
|
||||||
|
|
||||||
Creates a new HTML document filled with `content`.
|
Creates a new HTML document filled with `content`.
|
||||||
"""
|
"""
|
||||||
function html(content)
|
function html(content...)
|
||||||
doc = EzXML.HTMLDocumentNode(nothing, nothing)
|
doc = EzXML.HTMLDocumentNode(nothing, nothing)
|
||||||
link_or_text!(doc, content)
|
link_or_text!(doc, content...)
|
||||||
return doc
|
return doc
|
||||||
end #function
|
end #function
|
||||||
|
|
||||||
for symbol in HTML_ELEMENTS
|
for symbol in HTML_ELEMENTS
|
||||||
name = string(symbol)
|
name = string(symbol)
|
||||||
|
|
||||||
@eval function $symbol(content=nothing; kwargs...)
|
@eval function $symbol(content...=nothing; kwargs...)
|
||||||
return html_element($name, content; kwargs...)
|
return html_element($name, content...; kwargs...)
|
||||||
end #function
|
end #function
|
||||||
|
|
||||||
@eval export $symbol
|
@eval export $symbol
|
||||||
end
|
end
|
||||||
|
|
||||||
function html_div(content=nothing; kwargs...)
|
function html_div(content...=nothing; kwargs...)
|
||||||
return html_element("div", content; kwargs...)
|
return html_element("div", content; kwargs...)
|
||||||
end #function
|
end #function
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue