Skip to content

Commit 1ab20e5

Browse files
authored
feat: add param to set html size threshold (#238)
* feat: add param to set html size threshold * fix * more changes * fiox
1 parent 66ff6ba commit 1ab20e5

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

src/DocumentationGenerator.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function try_use_package(packagespec, envdir)
6161
return succeeded
6262
end
6363

64-
function build_package_docs(packagespec::Pkg.Types.PackageSpec, buildpath, registry; src_prefix="", href_prefix="")
64+
function build_package_docs(packagespec::Pkg.Types.PackageSpec, buildpath, registry; src_prefix="", href_prefix="", html_size_threshold_bytes=nothing)
6565
type, uri = doctype(packagespec, registry)
6666

6767
@info("$(packagespec.name) specifies docs of type $(type).")
@@ -71,7 +71,7 @@ function build_package_docs(packagespec::Pkg.Types.PackageSpec, buildpath, regis
7171
elseif type == "git-repo"
7272
build_git_docs(packagespec, buildpath, uri; src_prefix=src_prefix, href_prefix=href_prefix)
7373
elseif type == "vendored"
74-
build_local_docs(packagespec, buildpath, uri; src_prefix=src_prefix, href_prefix=href_prefix)
74+
build_local_docs(packagespec, buildpath, uri; src_prefix=src_prefix, href_prefix=href_prefix, html_size_threshold_bytes=html_size_threshold_bytes)
7575
else
7676
@error("Invalid doctype specified: $(type).")
7777
Dict(
@@ -102,7 +102,8 @@ function build_documentation(
102102
registry = joinpath(homedir(), ".julia/registries/General"),
103103
timeout = RUNNER_TIMEOUT,
104104
max_timeout = RUNNER_MAX_TIMEOUT,
105-
kill_timeout = RUNNER_KILL_TIMEOUT
105+
kill_timeout = RUNNER_KILL_TIMEOUT,
106+
html_size_threshold_bytes=nothing
106107
)
107108

108109
has_xvfb = try
@@ -149,7 +150,7 @@ function build_documentation(
149150
update_only = update_only,
150151
timeout = timeout,
151152
max_timeout = max_timeout,
152-
kill_timeout = kill_timeout)
153+
kill_timeout = kill_timeout, html_size_threshold_bytes=html_size_threshold_bytes)
153154
push!(process_queue, proc)
154155
end
155156
end
@@ -290,6 +291,7 @@ function start_builder(package, version;
290291
timeout = RUNNER_TIMEOUT,
291292
max_timeout = RUNNER_MAX_TIMEOUT,
292293
kill_timeout = RUNNER_KILL_TIMEOUT,
294+
html_size_threshold_bytes=nothing
293295
)
294296

295297
workerfile = joinpath(@__DIR__, "workerfile.jl")
@@ -332,6 +334,7 @@ function start_builder(package, version;
332334
$href_prefix
333335
$(server_type)
334336
$(isempty(api_url) ? "-" : api_url)
337+
$(isnothing(html_size_threshold_bytes) ? "-" : html_size_threshold_bytes)
335338
$(update_only ? "update" : "build")
336339
337340
```

src/builders.jl

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ using HTMLSanitizer
44
using Highlights
55
using Downloads
66
using Documenter
7-
function build_git_docs(packagespec, buildpath, uri; src_prefix="", href_prefix="")
7+
8+
function build_git_docs(packagespec, buildpath, uri; src_prefix="", href_prefix="", html_size_threshold_bytes=nothing)
89
pkgname = packagespec.name
910
return mktempdir() do dir
1011
return cd(dir) do
1112
run(`git clone --depth=1 $(uri) $(pkgname)`)
1213
docsproject = joinpath(dir, pkgname)
1314
return cd(docsproject) do
14-
return build_local_docs(packagespec, buildpath, nothing, docsproject, gitdirdocs = true; src_prefix=src_prefix, href_prefix=href_prefix)
15+
return build_local_docs(packagespec, buildpath, nothing, docsproject, gitdirdocs = true; src_prefix=src_prefix, href_prefix=href_prefix, html_size_threshold_bytes=html_size_threshold_bytes)
1516
end
1617
end
1718
end
@@ -84,7 +85,7 @@ function maybe_redirect(uri)
8485
return uri
8586
end
8687

87-
function build_local_docs(packagespec, buildpath, uri, pkgroot = nothing; gitdirdocs = false, src_prefix="", href_prefix="")
88+
function build_local_docs(packagespec, buildpath, uri, pkgroot = nothing; gitdirdocs = false, src_prefix="", href_prefix="", html_size_threshold_bytes=nothing)
8889
uri = something(uri, "docs")
8990
mktempdir() do envdir
9091
pkgname = packagespec.name
@@ -110,7 +111,7 @@ function build_local_docs(packagespec, buildpath, uri, pkgroot = nothing; gitdir
110111
for docdir in joinpath.(pkgroot, unique([uri, "docs", "doc"]))
111112
if isdir(docdir)
112113
@info("Building vendored Documenter.jl documentation at $(docdir).")
113-
output = build_documenter(packagespec, docdir)
114+
output = build_documenter(packagespec, docdir; html_size_threshold_bytes)
114115
@info("Documentation built at $(output).")
115116
if output !== nothing
116117
@info("Copying build documentation from $(output) to $(buildpath)")
@@ -139,7 +140,7 @@ function build_local_docs(packagespec, buildpath, uri, pkgroot = nothing; gitdir
139140

140141
# fallback docs (readme & docstrings)
141142
return mktempdir() do docsdir
142-
output = build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, could_use_pkg)
143+
output = build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, could_use_pkg, html_size_threshold_bytes)
143144
if output !== nothing
144145
cp(output, buildpath, force = true)
145146
return Dict(
@@ -161,7 +162,7 @@ function build_local_docs(packagespec, buildpath, uri, pkgroot = nothing; gitdir
161162
end
162163
end
163164

164-
function build_legacy_documenter(packagespec, docdir)
165+
function build_legacy_documenter(packagespec, docdir; html_size_threshold_bytes=nothing)
165166
open(joinpath(docdir, "Project.toml"), "w") do io
166167
println(io, """
167168
[deps]
@@ -171,16 +172,16 @@ function build_legacy_documenter(packagespec, docdir)
171172
Documenter = "~0.20"
172173
""")
173174
end
174-
build_documenter(packagespec, docdir)
175+
build_documenter(packagespec, docdir; html_size_threshold_bytes)
175176
end
176177

177-
function build_documenter(packagespec, docdir)
178+
function build_documenter(packagespec, docdir; html_size_threshold_bytes=nothing)
178179
pkgdir = normpath(joinpath(docdir, ".."))
179180
cd(pkgdir) do
180181
docsproject = joinpath(docdir, "Project.toml")
181182
docsmanifest = joinpath(docdir, "Manifest.toml")
182183
if !isfile(docsproject)
183-
return build_legacy_documenter(packagespec, docdir)
184+
return build_legacy_documenter(packagespec, docdir; html_size_threshold_bytes)
184185
end
185186

186187
# fix permissions to allow us to add the main pacakge to the docs project
@@ -190,7 +191,7 @@ function build_documenter(packagespec, docdir)
190191
chmod(joinpath(pkgdir, "Project.toml"), 0o660)
191192
isfile(joinpath(pkgdir, "Manifest.toml")) && chmod(joinpath(pkgdir, "Manifest.toml"), 0o660)
192193

193-
rundcocumenter = joinpath(@__DIR__, "rundocumenter.jl")
194+
rundocumenter = joinpath(@__DIR__, "rundocumenter.jl")
194195

195196
makefile = joinpath(docdir, "make.jl")
196197
if !isfile(makefile)
@@ -203,15 +204,17 @@ function build_documenter(packagespec, docdir)
203204
makefile = joinpath(docdir, jlfiles[1])
204205
@info("Using $(makefile) to generate Documenter docs.")
205206
end
206-
_, builddir = fix_makefile(makefile)
207+
_, builddir = fix_makefile(makefile; html_size_threshold_bytes)
207208
pkgimagesopt = VERSION >= v"1.9" ? "--pkgimages=no" : ""
209+
opt_ser = isnothing(html_size_threshold_bytes) ? "-" : html_size_threshold_bytes
208210
cmd = ```
209211
$(julia())
210212
--project="$(docdir)"
211213
$(isempty(pkgimagesopt) ? [] : pkgimagesopt)
212-
$(rundcocumenter)
214+
$(rundocumenter)
213215
$(pkgdir)
214216
$(makefile)
217+
$(opt_ser)
215218
```
216219

217220
try
@@ -229,7 +232,7 @@ function build_documenter(packagespec, docdir)
229232
end
230233
end
231234

232-
function build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, could_use_pkg)
235+
function build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, could_use_pkg, html_size_threshold_bytes=nothing)
233236
@info("Generating readme-only fallback docs.")
234237

235238
if pkgroot === nothing || !ispath(pkgroot)
@@ -265,7 +268,11 @@ function build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, c
265268
end
266269
end
267270
end
268-
271+
doc_format_str = if isnothing(html_size_threshold_bytes)
272+
"Documenter.HTML()"
273+
else
274+
"Documenter.HTML(;size_threshold=$html_size_threshold_bytes)"
275+
end
269276
makejl_str = """
270277
using Pkg
271278
Pkg.add(name="Documenter", version="1")
@@ -275,7 +282,7 @@ function build_readme_docs(pkgname, pkgroot, docsdir, src_prefix, href_prefix, c
275282
using $(pkgname)
276283
277284
makedocs(
278-
format = Documenter.HTML(),
285+
format = $(doc_format_str),
279286
sitename = "$(pkgname).jl",
280287
modules = [$(pkgname)],
281288
root = "$(docsdir)",

src/rundocumenter.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ let anonymous_module = Module()
55
Base.include(anonymous_module, joinpath(@__DIR__, "utils", "rewrite.jl"))
66
@eval anonymous_module begin
77
function main(args)
8-
if length(args) == 2
8+
if length(args) == 3
99
pkgdir = args[1]
1010
makefile = args[2]
11+
html_size_threshold_bytes = args[3] == "-" ? nothing : tryparse(Int, args[3])
1112
else
1213
makefile = joinpath(pwd(), "make.jl")
1314
pkgdir = normpath(joinpath(pwd(), ".."))
15+
html_size_threshold_bytes = nothing
1416
@info("No directory specified. Falling back to pkgdir=`$(pkgdir)` and makefile=`$(makefile)`.")
1517
end
1618
docsdir = dirname(makefile)
@@ -51,7 +53,7 @@ let anonymous_module = Module()
5153

5254
@info("Using Documenter version $(documenter_version).")
5355

54-
expr, bpath = fix_makefile(makefile, documenter_version)
56+
expr, bpath = fix_makefile(makefile, documenter_version; html_size_threshold_bytes)
5557

5658
@info("`cd`ing to `$(docsdir)` and setting `tls[:SOURCE_PATH]` to `$(makefile)`.")
5759
task_local_storage()[:SOURCE_PATH] = makefile

src/utils/rewrite.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function parseall(str)
1515
end
1616

1717
"""
18-
fix_makefile(makefile)
18+
fix_makefile(makefile; html_size_threshold_bytes=nothing)
1919
2020
Takes in the path to a Documenter.jl-compatible `make.jl` file and
2121
1. Removes all calls to `deploydocs`.
@@ -24,7 +24,7 @@ Takes in the path to a Documenter.jl-compatible `make.jl` file and
2424
2525
Return a tuple of `new_make_expr, buildpath`.
2626
"""
27-
function fix_makefile(makefile, documenter_version = v"1.8.1")
27+
function fix_makefile(makefile, documenter_version = v"1.8.1"; html_size_threshold_bytes=nothing)
2828
# default output path:
2929
buildpath = joinpath(dirname(makefile), "build")
3030
should_break = false
@@ -51,7 +51,12 @@ function fix_makefile(makefile, documenter_version = v"1.8.1")
5151
has_remotes = false
5252
has_repo = false
5353
has_warnonly = false
54-
html = documenter_version < v"0.21" ? QuoteNode(:html) : :(Documenter.HTML())
54+
docs_exp = if isnothing(html_size_threshold_bytes)
55+
:(Documenter.HTML())
56+
else
57+
:(Documenter.HTML(;size_threshold=$html_size_threshold_bytes))
58+
end
59+
html = documenter_version < v"0.21" ? QuoteNode(:html) : docs_exp
5560

5661
fixkwarg = argument -> begin
5762
if Meta.isexpr(argument, :kw)

src/workerfile.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ using DocumentationGenerator
44

55
Pkg.status()
66

7-
function build(uuid, name, url, version, buildpath, registry, deployment_url, src_prefix, href_prefix, server_type, api_url, args...)
7+
function build(uuid, name, url, version, buildpath, registry, deployment_url, src_prefix, href_prefix, server_type, api_url, html_size_threshold_bytes, args...)
88
packagespec = PackageSpec(uuid = uuid, name = name, version = VersionNumber(version))
99
api_url = api_url == "-" ? "" : api_url
10+
html_size_threshold_bytes = html_size_threshold_bytes == "-" ? nothing : tryparse(Int, html_size_threshold_bytes)
1011
withenv(
1112
"DOCUMENTATIONGENERATOR" => "true",
1213
"CI" => "true",
@@ -20,7 +21,7 @@ function build(uuid, name, url, version, buildpath, registry, deployment_url, sr
2021
end
2122
new_metadata = DocumentationGenerator.package_metadata(packagespec, url, server_type; api_url = api_url)
2223
merge!(metadata, new_metadata)
23-
build_meta = DocumentationGenerator.build_package_docs(packagespec, buildpath, registry; src_prefix=src_prefix, href_prefix=href_prefix)
24+
build_meta = DocumentationGenerator.build_package_docs(packagespec, buildpath, registry; src_prefix=src_prefix, href_prefix=href_prefix, html_size_threshold_bytes=html_size_threshold_bytes)
2425
merge!(metadata, build_meta)
2526

2627
isdir(buildpath) || mkpath(buildpath)

0 commit comments

Comments
 (0)