Skip to content

Commit

Permalink
bump version, added some array-like methods such as pop!, loosen comp…
Browse files Browse the repository at this point in the history
…at a bit
  • Loading branch information
joshday committed Sep 20, 2024
1 parent d747138 commit b92b11d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Cobweb"
uuid = "ec354790-cf28-43e8-bb59-b484409b7bad"
authors = ["joshday <emailjoshday@gmail.com> and contributors"]
version = "0.6.1"
version = "0.7.0"

[deps]
DefaultApplication = "3f0dd361-4fe0-5fc6-8523-80b14ec94d85"
Expand All @@ -10,8 +10,8 @@ Scratch = "6c6a2e73-6563-6170-7368-637461726353"

[compat]
DefaultApplication = "1"
OrderedCollections = "1.5"
Scratch = "1.1"
OrderedCollections = "1"
Scratch = "1"
julia = "1"

[extras]
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# Features

- View any `"text/html"`-representable object in your browser with `preview(x)`.
- Nice syntax for writing HTML: `h.<tag>(children...; attrs...)`
- Open `"text/html"`-representable objects in your browser with `preview(x)`.
- Clean syntax for writing HTML: `h.<tag>(children...; attrs...)`

```julia
h.div(class="myclass", style="color:red;")("content!")
Expand All @@ -29,14 +29,13 @@ body = h.body(
h.script("const buttonClicked = () => alert('This button was clicked!')"),
)


preview(body)
```

<br>
<br>

# Creating Nodes with `Cobweb.h`
# Writing HTML with `Cobweb.h`

### `h` is a pretty special function

Expand All @@ -49,7 +48,7 @@ h.tag # == h(:tag)

### `h` Creates a `Cobweb.Node`

- `Cobweb.Node`s are callable:
- `Cobweb.Node`s are callable (creates a copy with new children/attributes):

```julia
h.div("hi") # positional arguments add *children*
Expand All @@ -65,14 +64,17 @@ h.div("hi")(style="border:none;")

### Child Elements can be Anything

- If `!showable("text/html", child)`, `child` will be added as `HTML(child)`.
- If a `child` isn't `MIME"text/html"`-representable, it will be added as a `HTML(child)`.
- Note: `HTML(x)` means "insert this into HTML as `print(x)`".

```julia
# e.g. Strings have no text/html representation, so the added child is `HTML("hi")`
h.div("hi")
# <div>hi</div>

# You can take advantage of Julia structs that already have text/html representations:
using Markdown

md_example = h.div(md"""
# Here is Some Markdown
Expand Down Expand Up @@ -102,7 +104,7 @@ node
- `Node`s act like a `Vector` when it comes to children:

```julia
node = Cobweb.h.div
node = Cobweb.h.div # <div></div>

push!(node, Cobweb.h.h1("Hello!"))

Expand All @@ -111,6 +113,8 @@ node # <div><h1>Hello!</h1></div>
node[1] = "changed"

node # <div>changed</div>

collect(node) # ["changed"]
```

<br>
Expand Down
17 changes: 13 additions & 4 deletions src/Cobweb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ Base.iterate(o::Node) = iterate(children(o))
Base.iterate(o::Node, state) = iterate(children(o), state)
Base.push!(o::Node, x) = push!(children(o), x)
Base.append!(o::Node, x) = append!(children(o), x)
Base.deleteat!(o::Node, x) = deleteat!(children(o), x)
Base.pop!(o::Node) = pop!(children(o))
Base.popfirst!(o::Node) = popfirst!(children(o))
Base.splice!(o::Node, i::Integer) = splice!(children(o), i)
Base.splice!(o::Node, i::Integer, x) = splice!(children(o), i, x)



#-----------------------------------------------------------------------------# show Node
function print_opening_tag(io::IO, o::Node; self_close::Bool = false)
Expand Down Expand Up @@ -244,13 +251,15 @@ Base.show(io::IO, ::MIME"text/html", o::Comment) = print(io, "<!-- ", o.x, " -->
IFrame(content; attrs...)
Create an `<iframe srcdoc=\$content \$(attrs...)>`.
This can be helpful to work around environments that block loading scripts, such as Jupyter notebooks.
"""
struct IFrame{T, KW}
struct IFrame{T}
content::T
kw::KW
IFrame(x::T; kw...) where {T} = new{T, typeof(kw)}(x, kw)
kw::OrderedDict{Symbol, Any}
IFrame(x::T = ""; kw...) where {T} = new{T}(x, OrderedDict{Symbol,Any}(kw))
end
function Base.show(io::IO, ::MIME"text/html", o::IFrame)
function Base.show(io::IO, ::MIME"text/html", o::IFrame{T}) where {T}
show(io, h.iframe(; srcdoc=escape(repr("text/html", o.content)), o.kw...))
end

Expand Down

0 comments on commit b92b11d

Please sign in to comment.