diff --git a/CHANGELOG.md b/CHANGELOG.md index 20c315b32c..cc7461cbcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,11 @@ * ![Bugfix][badge-bugfix] A bad `repo` argument to `deploydocs` containing a protocol now throws an error instead of being misinterpreted. ([#1531][github-1531], [#1533][github-1533]) +* ![Bugfix][badge-bugfix] SVG images generated by `@example` blocks are now properly scaled to page width by URI-encoding the images, instead of directly embedding the SVG tags into the HTML. ([#1537][github-1537], [#1538][github-1538]) + * ![Maintenance][badge-maintenance] Documenter is no longer compatible with IOCapture v0.1 and now requires IOCapture v0.2. ([#1549][github-1549]) + ## Version `v0.26.3` * ![Maintenance][badge-maintenance] The internal naming of the temporary modules used to run doctests changed to accommodate upcoming printing changes in Julia. ([JuliaLang/julia#39841][julia-39841], [#1540][github-1540]) @@ -766,6 +769,8 @@ [github-1529]: https://github.com/JuliaDocs/Documenter.jl/pull/1529 [github-1531]: https://github.com/JuliaDocs/Documenter.jl/issues/1531 [github-1533]: https://github.com/JuliaDocs/Documenter.jl/pull/1533 +[github-1537]: https://github.com/JuliaDocs/Documenter.jl/issues/1537 +[github-1538]: https://github.com/JuliaDocs/Documenter.jl/pull/1538 [github-1540]: https://github.com/JuliaDocs/Documenter.jl/pull/1540 [github-1549]: https://github.com/JuliaDocs/Documenter.jl/pull/1549 [github-1551]: https://github.com/JuliaDocs/Documenter.jl/pull/1551 diff --git a/src/Writers/HTMLWriter.jl b/src/Writers/HTMLWriter.jl index ed95a42fcb..e4d000a5da 100644 --- a/src/Writers/HTMLWriter.jl +++ b/src/Writers/HTMLWriter.jl @@ -1748,7 +1748,49 @@ function mdconvert(d::Dict{MIME,Any}, parent; kwargs...) if haskey(d, MIME"text/html"()) out = Documents.RawHTML(d[MIME"text/html"()]) elseif haskey(d, MIME"image/svg+xml"()) - out = Documents.RawHTML(d[MIME"image/svg+xml"()]) + svg = d[MIME"image/svg+xml"()] + svg_tag_match = match(r"]*>", svg) + if svg_tag_match === nothing + # There is no svg tag so we don't do any more advanced + # processing and just return the svg as RawHTML. + # The svg string should be invalid but that's not our concern here. + out = Documents.RawHTML(svg) + else + # The xmlns attribute has to be present for data:image/svg+xml + # to work (https://stackoverflow.com/questions/18467982). + # If it doesn't exist, we splice it into the first svg tag. + # This should never invalidate otherwise valid svg. + svg_tag = svg_tag_match.match + xmlns_present = occursin("xmlns", svg_tag) + if !xmlns_present + svg = replace(svg, " " "%25") + svg = replace(svg, "#" => "%23") + + singles = count(==('\''), svg) + doubles = count(==('"'), svg) + if singles > doubles + # Replace every " with %22 because it terminates the src=" string otherwise + svg = replace(svg, "\"" => "%22") + sep = "\"" + else + # Replace every ' with %27 because it terminates the src=' string otherwise + svg = replace(svg, "\'" => "%27") + sep = "'" + end + + out = Documents.RawHTML(string("")) + end + elseif haskey(d, MIME"image/png"()) out = Documents.RawHTML(string("")) elseif haskey(d, MIME"image/webp"()) diff --git a/test/examples/src/man/tutorial.md b/test/examples/src/man/tutorial.md index 01c0bceaaf..9c920a0eb1 100644 --- a/test/examples/src/man/tutorial.md +++ b/test/examples/src/man/tutorial.md @@ -182,6 +182,8 @@ Base.show(io, ::MIME"image/svg+xml", svg::SVG) = write(io, svg.code) end # module ``` +Without xmlns tag: + ```@example inlinesvg using .InlineSVG SVG(""" @@ -195,6 +197,81 @@ SVG(""" """) ``` +With xmlns tag: + +```@example inlinesvg +using .InlineSVG +SVG(""" + + + + + + + +""") +``` + +With single quotes: + +```@example inlinesvg +using .InlineSVG +SVG(""" + + + + + + + +""") +``` + +With a mixture of single and double quotes: + +```@example inlinesvg +using .InlineSVG +SVG(""" + + + + + + + +""") +``` + +With viewBox and without xmlns, making the svg really large to test that it is resized correctly: + +```@example inlinesvg +using .InlineSVG +SVG(""" + + + + + + + +""") +``` + +Without viewBox and without xmlns, making the svg really large to test that it is resized correctly: + +```@example inlinesvg +using .InlineSVG +SVG(""" + + + + + + + +""") +``` + _Note: we can't define the `show` method in the `@example` block due to the world age counter in Julia 0.6 (Documenter's `makedocs` is not aware of any of the new method definitions happening in `eval`s)._