From 03e4f3b86a50224f61c54aa3355e802d3fa31282 Mon Sep 17 00:00:00 2001 From: Michael Goerz Date: Fri, 5 Jan 2024 16:03:37 -0500 Subject: [PATCH] Use low-level TOML writing No need to use the TOML library and the overhead of constructing a new Dict for every line and then letting the TOML writer deconstruct it again. --- Project.toml | 2 -- src/write_inventory.jl | 72 +++++++++++++++++++++++++++++++++--------- test/Project.toml | 1 - 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index d520702..8387839 100644 --- a/Project.toml +++ b/Project.toml @@ -9,7 +9,6 @@ DocInventories = "43dc2714-ed3b-44b5-b226-857eda1aa7de" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" MarkdownAST = "d0879d2d-cac2-40c8-9cee-1863dc0c7391" -TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" [compat] CodecZlib = "0.7" @@ -17,5 +16,4 @@ DocInventories = "0.2" Documenter = "1" Markdown = "1" MarkdownAST = "0.1.2" -TOML = "1" julia = "1.6" diff --git a/src/write_inventory.jl b/src/write_inventory.jl index d3a80a5..39df38e 100644 --- a/src/write_inventory.jl +++ b/src/write_inventory.jl @@ -3,7 +3,6 @@ using Documenter.HTMLWriter: HTML, HTMLContext, get_url, pretty_url, getpage, pa using Documenter.MDFlatten: mdflatten using Documenter: Documenter, anchor_fragment, doccat import Documenter: Selectors -using TOML # to properly escape strings using CodecZlib @@ -42,6 +41,9 @@ function write_inventory(doc::Documenter.Document) if isempty(version) @warn "No `version` in `makedocs`. Please pass `version` as a keyword argument." end + # TODO: If this gets moved to Documenter, this function should be called + # at the end of the HTML Writer and we wouldn't need to check for HTML + # output here, or construct a dummy `ctx`. i_html = findfirst(fmt -> (fmt isa HTML), doc.user.format) if isnothing(i_html) @info "Skip writing $(repr(filename)): No HTML output" @@ -57,8 +59,8 @@ function write_inventory(doc::Documenter.Document) write(io_toml, "format = \"DocInventories v0\"\n") # TODO: If this gets moved to Documenter, it should be # format = "Documenter Inventory v1" - TOML.print(io_toml, Dict("project" => project)) - TOML.print(io_toml, Dict("version" => version)) + _write_toml_val(io_toml, "project", project) + _write_toml_val(io_toml, "version", version) write(io_toml, "\n") write(io_inv_header, "# Sphinx inventory version 2\n") @@ -77,9 +79,9 @@ function write_inventory(doc::Documenter.Document) line = "$name $domain:$role $priority $uri $dispname\n" write(io_inv, line) write(io_toml, "[[$domain.$role]]\n") - TOML.print(io_toml, Dict("name" => name)) - TOML.print(io_toml, Dict("uri" => uri)) - (dispname != "-") && TOML.print(io_toml, Dict("dispname" => dispname)) + _write_toml_val(io_toml, "name", name) + _write_toml_val(io_toml, "uri", uri) + (dispname != "-") && _write_toml_val(io_toml, "dispname", dispname) end write(io_toml, "\n") @@ -97,9 +99,9 @@ function write_inventory(doc::Documenter.Document) line = "$name $domain:$role $priority $uri $dispname\n" write(io_inv, line) write(io_toml, "[[$domain.$role]]\n") - TOML.print(io_toml, Dict("name" => name)) - TOML.print(io_toml, Dict("uri" => uri)) - (dispname != "-") && TOML.print(io_toml, Dict("dispname" => dispname)) + _write_toml_val(io_toml, "name", name) + _write_toml_val(io_toml, "uri", uri) + (dispname != "-") && _write_toml_val(io_toml, "dispname", dispname) end write(io_toml, "\n") @@ -117,8 +119,8 @@ function write_inventory(doc::Documenter.Document) line = "$name $domain:$role $priority $uri $dispname\n" write(io_inv, line) write(io_toml, "[[$domain.$role]]\n") - TOML.print(io_toml, Dict("name" => name)) - TOML.print(io_toml, Dict("uri" => uri)) + _write_toml_val(io_toml, "name", name) + _write_toml_val(io_toml, "uri", uri) end close(io_inv) @@ -129,6 +131,46 @@ function write_inventory(doc::Documenter.Document) end +function _write_toml_val(io::IO, name::AbstractString, value::AbstractString) + # Cf. TOML.Internals.Printer.print_toml_escaped, but that's way too + # internal to just use. + write(io, name) + write(io, " = \"") + for c::AbstractChar in value + if !isvalid(c) + msg = "Invalid character $(repr(c)) encountered while writing TOML" + throw(ArgumentError(msg)) + end + if c == '\b' + print(io, '\\', 'b') + elseif c == '\t' + print(io, '\\', 't') + elseif c == '\n' + print(io, '\\', 'n') + elseif c == '\f' + print(io, '\\', 'f') + elseif c == '\r' + print(io, '\\', 'r') + elseif c == '"' + print(io, '\\', '"') + elseif c == '\\' + print(io, "\\", '\\') + elseif iscntrl(c) + print(io, "\\u") + print(io, string(UInt32(c), base=16, pad=4)) + else + print(io, c) + end + end + write(io, "\"\n") +end + + +function _write_toml_val(io::IO, name::AbstractString, value::Int64) + write(io, name, " = ", value, "\n") +end + + function _get_inventory_uri(doc, ctx, name::AbstractString, anchor::Documenter.Anchor) filename = relpath(anchor.file, doc.user.build) page_url = pretty_url(ctx, get_url(ctx, filename)) @@ -136,7 +178,7 @@ function _get_inventory_uri(doc, ctx, name::AbstractString, anchor::Documenter.A # https://github.com/JuliaDocs/Documenter.jl/issues/2387 page_url = replace(page_url, "\\" => "/") end - label = escapeuri(Documenter.anchor_label(anchor)) + label = _escapeuri(Documenter.anchor_label(anchor)) if label == name uri = page_url * raw"#$" else @@ -185,6 +227,6 @@ end _utf8_chars(str::AbstractString) = (Char(c) for c in codeunits(str)) -escapeuri(c::Char) = string('%', uppercase(string(Int(c), base=16, pad=2))) -escapeuri(str::AbstractString) = - join(_issafe(c) ? c : escapeuri(c) for c in _utf8_chars(str)) +_escapeuri(c::Char) = string('%', uppercase(string(Int(c), base=16, pad=2))) +_escapeuri(str::AbstractString) = + join(_issafe(c) ? c : _escapeuri(c) for c in _utf8_chars(str)) diff --git a/test/Project.toml b/test/Project.toml index f21294e..eee3cbd 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -13,6 +13,5 @@ MarkdownAST = "d0879d2d-cac2-40c8-9cee-1863dc0c7391" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" -TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestingUtilities = "40452611-1178-4e48-bdfc-3af4bebad9c9"