Skip to content

Commit

Permalink
Add generate_redirects to generated HTML redirects of Documenter builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenpi committed Aug 18, 2023
1 parent a3d22ff commit cb0af7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/DocumenterTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function package_devpath(pkg::Module)
end

include("walkdocs.jl")
include("redirects.jl")
include("genkeys.jl")
include("Generator.jl")
include("Themes.jl")
Expand Down
48 changes: 48 additions & 0 deletions src/redirects.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
generate_redirects(;
src::AbstractString,
out::AbstractString,
rooturl::AbstractString,
force::Bool=false,
)
Given a Documenter-generated HTML documentation directory `src`, generates a corresponding
directory where each of the HTML files is replaced by a redirect to the corresponding file
on the under `rooturl`.
Technically, the function replaces each of the HTML files with a HTML file that contains
a `<meta http-equiv="refresh" content="0` redirect.
If `force = true`, the destination `out` will first be deleted.
"""
function generate_redirects(;
src::AbstractString,
out::AbstractString,
rooturl::AbstractString,
force::Bool=false,
)
rooturl = rstrip(rooturl, '/')
outdir = abspath(out)
if ispath(outdir)
if force
@warn "Destination path exists, removing" force outdir
rm(outdir, recursive=true, force=true)
else
error("output directory already exists: $outdir")
end
end
mkpath(outdir)
# Walk over all the HTML files in the source directory and generate the
# corresponding HTML redirect file.
walkdocs(src) do fileinfo
path_elements = splitpath(fileinfo.relpath)
mkpath(joinpath(outdir, path_elements[1:end-1]...))
open(joinpath(outdir, path_elements...), write=true) do io
redirect_url = join((rooturl, path_elements...), '/')
write(io, """
<!--This file is automatically generated by Documenter.jl-->
<meta http-equiv="refresh" content="0; url=$(redirect_url)"/>
""")
end
end
end

0 comments on commit cb0af7f

Please sign in to comment.