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

Conversation

fonsp
Copy link
Member

@fonsp fonsp commented Mar 29, 2022

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

@fonsp fonsp changed the title Make IO Context properties available inside proxied IO Make IO Context available inside <script> Mar 29, 2022
@@ -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.

@codecov-commenter
Copy link

codecov-commenter commented Mar 29, 2022

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%) ⬇️

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants