Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IO Context available inside <script> #27

Merged
merged 3 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;)'/>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia's default show method for a struct will omit the module if it is being rendered inside that same module. Before this PR, this information was not available, and it showed the full name with module prefix.

#-> <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