JuliaPluto / HypertextLiteral.jl

Julia library for the string interpolation of HTML and SVG
https://juliapluto.github.io/HypertextLiteral.jl/stable/
ISC License
64 stars 10 forks source link

Make IO Context available inside `<script>` #27

Closed fonsp closed 2 years ago

fonsp commented 2 years ago

This PR makes it possible to use get(io, :key, "default") inside custom show methods to query the io context of the renderer. This already worked for objects interpolated inside HTML, because this rendering is not proxied, but not for objects interpolated inside <script>.

# Set up a custom javascript-renderable type
struct Hello end

function Base.show(io::IO, ::MIME"text/javascript", ::Hello)
    # the rendered content depends on the IO context
    print(io, get(io, :hello, "oops"))
end

# Example usage:
h = @htl("""
<script>
const x = $(Hello())
</script>
""")

# Let's look at the output:
repr(
    MIME"text/html"(), h; 
    context=(:hello => "world")
)
#-> "<script>const x = world</script>"
codecov-commenter commented 2 years ago

Codecov Report

Merging #27 (6e85aec) into master (7bbabcc) will decrease coverage by 0.67%. The diff coverage is 16.66%.

@@            Coverage Diff             @@
##           master      #27      +/-   ##
==========================================
- Coverage   95.50%   94.82%   -0.68%     
==========================================
  Files           7        7              
  Lines         690      696       +6     
==========================================
+ Hits          659      660       +1     
- Misses         31       36       +5     
Impacted Files Coverage Δ
src/script.jl 91.45% <ø> (ø)
src/style.jl 69.56% <ø> (ø)
src/primitives.jl 88.33% <16.66%> (-7.97%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7bbabcc...6e85aec. Read the comment docs.