Skip to content

Commit

Permalink
Merge pull request JuliaPluto#3 from JuliaPluto/IO-Context-passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Jan 5, 2022
2 parents a60acf7 + bdcff9f commit f5707f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MarkdownLiteral"
uuid = "736d6165-7244-6769-4267-6b50796e6954"
authors = ["Fons van der Plas <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
CommonMark = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
Expand Down
18 changes: 15 additions & 3 deletions src/MarkdownLiteral.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,24 @@ macro markdown(expr)
])
quote
result = $(esc(Expr(:macrocall, getfield(HypertextLiteral, Symbol("@htl")), __source__, expr)))
htl_output = repr(MIME"text/html"(), result)

$(cm_parser)(htl_output)
CMParsedRenderer(contents = result, parser = $cm_parser)
end
end

Base.@kwdef struct CMParsedRenderer
contents::Any
parser::CommonMark.Parser
end

function Base.show(io::IO, m::MIME"text/html", p::CMParsedRenderer)
# Use the given IO as context, render to HTML
html_render = sprint(show, m, p.contents; context = io)
# Parse the result
cm_parsed = p.parser(html_render)
# And render to HTML again
show(io, m, cm_parsed)
end

# Some aliases, it's up to you which one to import.

const var"@markdownliteral" = var"@markdown"
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,28 @@ import MarkdownLiteral: @markdown

@test htmle isa String
@test occursin("Hello", htmle)
end

@testset "IO Context" begin

struct Thing end

function Base.show(io::IO, ::MIME"text/html", ::Thing)
write(io, get(io, :hello, "asdf"))
end

h = @markdown("""
Hello $(Thing())
""")

s1 = repr(MIME"text/html"(), h)
@test s1 isa String
@test occursin("Hello", s1)
@test occursin("asdf", s1)

s2 = sprint() do io
show(IOContext(io, :hello => "world"), MIME"text/html"(), h)
end
@test s2 isa String
@test occursin("world", s2)
end

0 comments on commit f5707f5

Please sign in to comment.