repr
takes a mimetype arguement.
Sometimes, it is evan used.
e.g.
julia> repr("text/html",Docs.html"<b>foo</b>")
"<b>foo</b>"
julia> repr("text/markdown", "foo")
"foo"
Mostly though it is not:
julia> repr("text/markdown", 1)
ERROR: MethodError: no method matching show(::Base.GenericIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("text/markdown")}, ::Int64)
Closest candidates are:
show(::IO, ::MIME{Symbol("text/plain")}, ::Any) at sysimg.jl:194
show(::IO, ::MIME{Symbol("text/markdown")}, ::Markdown.MD) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/Markdown/src/render/plain.jl:140
show(::IO, ::MIME{Symbol("text/csv")}, ::Any) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/DelimitedFiles/src/DelimitedFiles.jl:828
...
Stacktrace:
[1] __binrepr(::MIME{Symbol("text/markdown")}, ::Int64, ::Nothing) at ./multimedia.jl:127
[2] _textrepr(::MIME{Symbol("text/markdown")}, ::Int64, ::Nothing) at ./multimedia.jl:119
[3] #repr#1(::Nothing, ::Function, ::MIME{Symbol("text/markdown")}, ::Int64) at ./multimedia.jl:115
[4] (::getfield(Base, Symbol("#kw##repr")))(::NamedTuple{(:context,),Tuple{Nothing}}, ::typeof(repr), ::MIME{Symbol("text/markdown")}, ::Int64) at ./none:0
[5] #repr#2(::Nothing, ::Function, ::String, ::Int64) at ./multimedia.jl:116
[6] repr(::String, ::Int64) at ./multimedia.jl:116
[7] top-level scope at none:0
Which makes sense when there is no canonical markdown representation of the thing.
However, markdown is a pretty flexible format.
So I would like to fallback, first to HTML, then to plain text,
then to just what ever repr(x)
returns.
Is there any better way than doing exception handling?
"""
markdown_repr(x)
Returns the most suitable method for displaying `x` in a markdown document.
"""
function markdown_repr(x)
for mime_type in ("text/markdown", "text/html", "text/plain")
try
return repr(mime_type, x)
catch err
err isa MethodError || rethrow(err)
end
end
return repr(x)
end