Skip to content

Add a walkdocs function to walk over all HTML files of a docs directory #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# DocumenterTools.jl changelog

## Unreleased

* ![Enhancement][badge-enhancement] DocumenterTools now provides a `DocumenterTools.walkdocs` function. ([#75][github-75])

## Version `v0.1.17`

* ![Enhancement][badge-enhancement] The compiled CSS files generated by DocumenterTools are now minified. ([#71][github-71])
Expand Down Expand Up @@ -102,6 +106,7 @@ Maintenance release declaring compatibility with Documenter 0.25. ([#39][github-
[github-64]: https://github.com/JuliaDocs/DocumenterTools.jl/issues/64
[github-65]: https://github.com/JuliaDocs/DocumenterTools.jl/pull/65
[github-71]: https://github.com/JuliaDocs/DocumenterTools.jl/pull/71
[github-75]: https://github.com/JuliaDocs/DocumenterTools.jl/pull/75


[badge-breaking]: https://img.shields.io/badge/BREAKING-red.svg
Expand Down
1 change: 1 addition & 0 deletions src/DocumenterTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function package_devpath(pkg::Module)
return normpath(joinpath(path, "..", ".."))
end

include("walkdocs.jl")
include("genkeys.jl")
include("Generator.jl")
include("Themes.jl")
Expand Down
25 changes: 12 additions & 13 deletions src/OutdatedWarning.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export OutdatedWarning

module OutdatedWarning
import ..DocumenterTools
using Gumbo, AbstractTrees, Documenter

OLD_VERSION_CSS = replace("""
Expand Down Expand Up @@ -168,20 +169,18 @@ function generate(io::IO, root::String;force = false)
end

print(io, "Processing $(dir): ")
for (root, _, files) in walkdir(path)
for file in files
_, ext = splitext(file)
if ext == ".html"
try
did_change = add_old_docs_notice(joinpath(root, file), force)
print(io, did_change ? "✓" : ".")
catch err
if err isa InterruptException
rethrow()
end
@debug "Fatally failed to add a outdated warning" exception = (err, catch_backtrace())
print(io, "!")
DocumenterTools.walkdocs(path) do fileinfo
_, ext = splitext(fileinfo.filename)
if ext == ".html"
try
did_change = add_old_docs_notice(fileinfo.fullpath, force)
print(io, did_change ? "✓" : ".")
catch err
if err isa InterruptException
rethrow()
end
@debug "Fatally failed to add a outdated warning" exception = (err, catch_backtrace())
print(io, "!")
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions src/walkdocs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
walkdocs(f, dir::AbstractString; collect::Bool=false)

Takes a directory `dir`, which is assumed to contain Documenter-generated HTML documentation,
walks over all the files and calls `f` on each of the HTML files it find. `f` will be called
with a single object that has the following fields (all strings):

- `root`: the root directory of the walk, i.e. `dir` (but as an absolute path)
- `filename`: file name
- `relpath`: path to the file, relative to `dir`
- `fullpath`: absolute path to the file

If `collect = true` is set, the function also "collects" all the return values from `f`
from each of the function calls, essentially making `walkdocs` behave like a `map` function
applied on each of the HTML files.
"""
function walkdocs(f, dir::AbstractString; collect::Bool=false)
dir = abspath(dir)
isdir(dir) || error("docwalker: dir is not a directory\n dir = $(dir)")

mapped_collection = collect ? Any[] : nothing
for (root, _, files) in walkdir(dir)
for file in files
_, ext = splitext(file)
(ext == ".html") || continue
file_fullpath = joinpath(root, file)
file_relpath = Base.relpath(file_fullpath, dir)
fileinfo = (;
root = dir,
filename = file,
relpath = file_relpath,
fullpath = file_fullpath,
)
r = f(fileinfo)
if collect
push!(mapped_collection, r)
end
end
end
return mapped_collection
end
13 changes: 7 additions & 6 deletions test/outdated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ mktempdir() do TMP
cp(joinpath(TMP, "fixtures", "pre"), transient_path, force=true)
OutdatedWarning.generate(transient_path)

for (root, _, files) in walkdir(transient_path)
for file in files
content = read(joinpath(root, file), String)
expected = read(joinpath(replace(root, "transient" => "post"), file), String)
@test replace(content, "\r\n" => "\n") == replace(expected, "\r\n" => "\n")
end
DocumenterTools.walkdocs(transient_path) do fileinfo
content = read(fileinfo.fullpath, String)
expected = read(
joinpath(replace(dirname(fileinfo.fullpath), "transient" => "post"), fileinfo.filename),
String
)
@test replace(content, "\r\n" => "\n") == replace(expected, "\r\n" => "\n")
end

rm(joinpath(TMP, "fixtures"), recursive=true)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import Documenter
DocumenterTools.genkeys(DocumenterMarkdown)
end

@testset "walkdocs" begin
include("walkdocs.jl")
end

@testset "outdated warnings" begin
include("outdated.jl")
end
Expand Down
19 changes: 19 additions & 0 deletions test/walkdocs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let fileinfos = []
rs = DocumenterTools.walkdocs(joinpath(@__DIR__, "fixtures")) do fileinfo
push!(fileinfos, fileinfo)

@test isabspath(fileinfo.root)
@test isabspath(fileinfo.fullpath)
@test !isabspath(fileinfo.relpath)
@test joinpath(fileinfo.root, fileinfo.relpath) == fileinfo.fullpath
end
@test rs === nothing
@test length(fileinfos) == 10
end

let rs = DocumenterTools.walkdocs(joinpath(@__DIR__, "fixtures"), collect=true) do fileinfo
fileinfo.root
end
@test length(rs) == 10
@test all(s -> isa(s, String), rs)
end