Skip to content

Commit

Permalink
feat(render): accept options to modify rendering behavior
Browse files Browse the repository at this point in the history
These can be defined both at 'render time' and when the `Diagram` that
is being rendered is constructed. This makes sure `Diagram`s rendered
implicitly, i.e. through `Base.show`, can use these `options` while at
the same allowing these options to be overridden when rendering.
  • Loading branch information
bauglir committed Jul 13, 2022
1 parent 6ff23e3 commit 5e7f1d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Kroki.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ UriSafeBase64Payload(diagram::Diagram) = foldl(
Renders a [`Diagram`](@ref) through a Kroki service to the specified output
format.
Allows the specification of [diagram
options](https://docs.kroki.io/kroki/setup/diagram-options) through the
`options` keyword. The `options` default to those specified on the
[`Diagram`](@ref).
If the Kroki service responds with an error, throws an
[`InvalidDiagramSpecificationError`](@ref
Kroki.Exceptions.InvalidDiagramSpecificationError) or
Expand All @@ -140,7 +145,11 @@ _SVG output is supported for all [`Diagram`](@ref) types_. See [Kroki's
website](https://kroki.io/#support) for an overview of other supported output
formats per diagram type. Note that this list may not be entirely up-to-date.
"""
render(diagram::Diagram, output_format::AbstractString) =
render(
diagram::Diagram,
output_format::AbstractString;
options::Dict{String, String} = diagram.options,
) =
try
getfield(
request(
Expand All @@ -154,6 +163,11 @@ render(diagram::Diagram, output_format::AbstractString) =
],
'/',
),
# Pass all diagram options as headers to Kroki by prepending the
# necessary prefix to all provided `options`. This ensures this package
# does not have to be updated whenever new options are added to the
# service
"Kroki-Diagram-Options-" .* keys(options) .=> values(options),
),
:body,
)
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ end
# after the render, these should be ignored when matching
@test endswith(rendered, r"</svg>\s?")
end

@testset "takes `options` into account" begin
expected_theme_name = "materia"
options = Dict{String, String}("theme" => expected_theme_name)
diagram = Diagram(:plantuml, "A -> B: C"; options)

@testset "defaults to `Diagram` options" begin
rendered = String(render(diagram, "svg"))

@test occursin("!theme $(expected_theme_name)", rendered)
end

@testset "allows definition at render-time" begin
expected_overridden_theme = "sketchy"
rendered = String(
render(diagram, "svg"; options = Dict("theme" => expected_overridden_theme)),
)

@test occursin("!theme $(expected_overridden_theme)", rendered)
end
end
end

@testset "`Base.show`" begin
Expand Down

0 comments on commit 5e7f1d7

Please sign in to comment.