Skip to content

Commit

Permalink
Make IO Context available inside <script> (#27)
Browse files Browse the repository at this point in the history
* Make IO Context properties available inside proxied IO

* Update NEWS.md

* move tests into ## Regression tests
  • Loading branch information
fonsp authored May 10, 2022
1 parent 880e957 commit 31ef75d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## v0.9.4

- Pull #27: Make IO Context available inside `<script>`

## v0.9.3

- Pull #18: Interpolating within a paired tags
Expand Down
2 changes: 1 addition & 1 deletion docs/src/attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ desirable for use inside an attribute value.
struct Custom data::String end

@htl "<tag att=$(Custom("A&B"))/>"
#-> <tag att='Custom(&quot;A&amp;B&quot;)'/>
#-> <tag att='Custom(&quot;A&amp;B&quot;)'/>

This can be sometimes addressed by implementing `Base.print()`.

Expand Down
10 changes: 8 additions & 2 deletions docs/src/primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ it. There are several wrappers which drive special proxy handling.

This utility class acts wraps an `IO` stream to provide HTML escaping.

io = IOBuffer()
io_buffer = IOBuffer()
io = IOContext(io_buffer, :hello => "world")
ep = EscapeProxy(io)

macro echo(expr)
:($expr; print(String(take!(io))))
:($expr; print(String(take!(io_buffer))))
end

The result of this proxy is that regular content printed to it is passed
Expand All @@ -24,6 +25,11 @@ along to the wrapped `IO`, after escaping the ampersand (`&`), less-than
@echo print(ep, "(&'<\")")
#-> (&amp;&apos;&lt;&quot;)

Any [IO context properties](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple{IO,%20Pair}) will be reflected by the `EscapeProxy`:

@echo print(ep, get(ep, :hello, "oops"))
#-> world

## Bypass

This wrapper simply prints its content.
Expand Down
18 changes: 18 additions & 0 deletions docs/src/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ It's important to handle unicode content properly.

@htl("<script>alert($(s))</script>")
#-> <script>alert("α\n")</script>

## Regression tests

Any [IO context properties](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple{IO,%20Pair}) of the renderer can be used, as expected:

struct Hello end

function Base.show(io::IO, ::MIME"text/javascript", ::Hello)
print(io, get(io, :hello, "oops"))
end

h = @htl("""<script>const x = $(Hello())</script>""")

repr(
MIME"text/html"(), h;
context=(:hello => "world")
)
#-> "<script>const x = world</script>"
12 changes: 11 additions & 1 deletion src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ end

Base.print(io::IO, x::Bypass) = print(io, x.content)

abstract type IOProxy <: IO end

"""
EscapeProxy(io) - wrap an `io` to perform HTML escaping
Expand All @@ -44,7 +46,7 @@ julia> print(ep, Bypass("<tag/>"))
<tag/>
```
"""
struct EscapeProxy{T<:IO} <: IO
struct EscapeProxy{T<:IO} <: IOProxy
io::T
end

Expand Down Expand Up @@ -108,3 +110,11 @@ function Base.unsafe_write(ep::EscapeProxy, input::Ptr{UInt8}, nbytes::UInt)
end
return written
end

# IO passthrough methods:
Base.in(key_value::Pair, io::IOProxy) = in(key_value, io.io)
Base.haskey(io::IOProxy, key) = haskey(io.io, key)
Base.getindex(io::IOProxy, key) = getindex(io.io, key)
Base.get(io::IOProxy, key, default) = get(io.io, key, default)
Base.keys(io::IOProxy) = keys(io.io)
Base.displaysize(io::IOProxy) = displaysize(io.io)
2 changes: 1 addition & 1 deletion src/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ julia> print(gp, "</script>")
ERROR: "Content within a script tag must not contain `</script>`"
```
"""
mutable struct ScriptTagProxy{T<:IO} <: IO where {T}
mutable struct ScriptTagProxy{T<:IO} <: IOProxy where {T}
io::T
index::Int

Expand Down
2 changes: 1 addition & 1 deletion src/style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ julia> print(gp, "</style>")
ERROR: "Content within a style tag must not contain `</style>`"
```
"""
mutable struct StyleTagProxy{T<:IO} <: IO where {T}
mutable struct StyleTagProxy{T<:IO} <: IOProxy where {T}
io::T
index::Int

Expand Down

0 comments on commit 31ef75d

Please sign in to comment.