Skip to content

Commit

Permalink
feat: add 'support' for rendering to JPEG
Browse files Browse the repository at this point in the history
Which diagram types could be rendered to JPEG was already defined
through `LIMITED_DIAGRAM_SUPPORT`. However, no `show` method was
available so far.

The reason this adds 'support' is that the functionality to render to
JPEG appears to be broken in the Kroki service at the time of writing.
This functionality is enabled here to support the output format once the
issues upstream have been addressed, without requiring a new release.
  • Loading branch information
bauglir committed Jul 16, 2022
1 parent c8fce60 commit 5d34345
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Kroki.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ const LIMITED_DIAGRAM_SUPPORT = Dict{MIME, Tuple{Symbol, Vararg{Symbol}}}(
# that actually support the format
Base.show(io::IO, ::MIME"application/pdf", diagram::Diagram) =
write(io, render(diagram, "pdf"))
Base.show(io::IO, ::MIME"image/jpeg", diagram::Diagram) =
write(io, render(diagram, "jpeg"))
Base.show(io::IO, ::MIME"image/png", diagram::Diagram) =
write(io, render(diagram, "png"))

Expand All @@ -241,6 +243,16 @@ Base.show(io::IO, ::MIME"image/svg+xml", diagram::Diagram) =
Base.showable(::T, diagram::Diagram) where {T <: MIME} =
Symbol(lowercase(String(diagram.type))) get(LIMITED_DIAGRAM_SUPPORT, T(), Tuple([]))

# Calling `Base.show` for JPEGs is explicitly disabled, for the time being.
# JPEG rendering is broken for all, supposedly supported, diagram types in the
# Kroki service. Should the support be fixed in the service, this method can be
# easily redefined by consuming software to support JPEG in case Kroki.jl has
# not been updated and released.
#
# Note that this only affects automatic rendering of `Diagram`s to JPEGs in
# supported environments. It is still possible to use `render` to render JPEGs
Base.showable(::MIME"image/jpeg", ::Diagram) = false

# The two-argument `Base.show` version is used to render the "text/plain" MIME
# type. Those `Diagram` types that can render text versions, e.g. PlantUML,
# Structurizr, should render those. All others should render their
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ end
@test !showable("application/pdf", svgbob_diagram)
@test_throws(InvalidOutputFormatError, sprint(show, MIME"image/png"(), svgbob_diagram))
@test !showable(MIME"image/png"(), svgbob_diagram)
@test_throws(
InvalidOutputFormatError,
show(IOBuffer(), MIME"image/jpeg"(), svgbob_diagram)
)
@test !showable("image/jpeg", svgbob_diagram)
testShowMethodRenders(svgbob_diagram, MIME"image/svg+xml"(), "svg")
@test !showable("non-existent/mime-type", svgbob_diagram)

Expand Down

0 comments on commit 5d34345

Please sign in to comment.