Skip to content

Commit

Permalink
Use low-level TOML writing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goerz committed Jan 5, 2024
1 parent 1bd05b0 commit 03e4f3b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ 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"
DocInventories = "0.2"
Documenter = "1"
Markdown = "1"
MarkdownAST = "0.1.2"
TOML = "1"
julia = "1.6"
72 changes: 57 additions & 15 deletions src/write_inventory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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)
Expand All @@ -129,14 +131,54 @@ 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))
if Sys.iswindows()
# 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
Expand Down Expand Up @@ -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))
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 03e4f3b

Please sign in to comment.