From 5e7f1d7ccdfa601734d5d2ef865e6f7421141ae2 Mon Sep 17 00:00:00 2001 From: Joris Kraak Date: Wed, 13 Jul 2022 23:23:19 +0200 Subject: [PATCH] feat(render): accept `options` to modify rendering behavior 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. --- src/Kroki.jl | 16 +++++++++++++++- test/runtests.jl | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Kroki.jl b/src/Kroki.jl index 6b6f218..ffa8dcb 100644 --- a/src/Kroki.jl +++ b/src/Kroki.jl @@ -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 @@ -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( @@ -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, ) diff --git a/test/runtests.jl b/test/runtests.jl index e6ad5d6..25d3f5e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -130,6 +130,27 @@ end # after the render, these should be ignored when matching @test endswith(rendered, r"\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