I've run into two use cases where I'd like to show an image to text/html directly to get embeddable output:
-
to render code block results in Pollen.jl

-
for rich HTML display of images inside other HTML structures, like a table
For example, inside a PrettyTables.jl table using HTML output:

Is there an implementation of show(::IO, ::MIME"text/html", ::ColorantMatrix) somewhere?
Workaround
For now, I've been using the below functions to render images (and other data that can be rendered to image mimetypes like plots) to HTML. However, it would be nice to have a method, at least for images (ColorantMatrixes) for showing to text/html that falls back on png/jpg/svg output.
function Base.show(io::IO, mime::MIME"text/html", x::ShowAsHTML)
if showable(mime, x.val)
show(io, mime, x.val)
elseif showable(MIME("image/jpg"), x.val)
buf = IOBuffer()
show(buf, MIME("image/jpg"), x.val)
print(io, """<img src="data:image/png;base64,$(Base64.base64encode(take!(buf)))"/>""")
elseif showable(MIME("image/png"), x.val)
buf = IOBuffer()
show(buf, MIME("image/png"), x.val)
print(io, """<img src="data:image/png;base64,$(Base64.base64encode(take!(buf)))"/>""")
elseif showable(MIME("image/svg+xml"), x.val)
buf = IOBuffer()
show(buf, MIME("image/svg+xml"), x.val)
print(io, replace(String(take!(buf)), "\n" => ""))
else
print(io, x)
end
end
I've run into two use cases where I'd like to show an image to
text/htmldirectly to get embeddable output:to render code block results in Pollen.jl

for rich HTML display of images inside other HTML structures, like a table

For example, inside a PrettyTables.jl table using HTML output:
Is there an implementation of
show(::IO, ::MIME"text/html", ::ColorantMatrix)somewhere?Workaround
For now, I've been using the below functions to render images (and other data that can be rendered to image mimetypes like plots) to HTML. However, it would be nice to have a method, at least for images (
ColorantMatrixes) for showing totext/htmlthat falls back on png/jpg/svg output.