Skip to content

Commit

Permalink
fix, document and test html_to_pdf with URL
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Feb 13, 2024
1 parent 57bc6d9 commit 0273da4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
11 changes: 8 additions & 3 deletions src/PlutoPDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const screenshot_default_options = (
scale=2,
)

"""
The same as `pluto_to_pdf`, but the first argument is a path to an HTML file or a URL (to a Pluto notebook hosted online).
"""
function html_to_pdf(
html_path::AbstractString,
output_path::Union{AbstractString,Nothing}=nothing,
Expand All @@ -50,18 +53,20 @@ function html_to_pdf(
open=true,
console_output=true
)
bin_script = normpath(joinpath(@__DIR__, "../node/bin.js"))
is_url = startswith(html_path, "http://") || startswith(html_path, "https://")
is_url && @assert output_path !== nothing "You need to specify an output path when the input is a URL"

output_path = tamepath(something(output_path, Pluto.numbered_until_new(splitext(html_path)[1]; suffix=".pdf", create_file=false)))

screenshot_dir_path = if screenshot_dir_path === nothing
nothing
else
mkpath(tamepath(screenshot_dir_path))
end

@info "Generating pdf..."
cmd = `$(node()) $bin_script $(tamepath(html_path)) $(output_path) $(JSON.json(
bin_script = normpath(joinpath(@__DIR__, "../node/bin.js"))
cmd = `$(node()) $bin_script $(is_url ? html_path : tamepath(html_path)) $(output_path) $(JSON.json(
(; default_options..., options...)
)) $(something(screenshot_dir_path, "")) $(JSON.json((; screenshot_default_options..., screenshot_options...)))`
if console_output
Expand Down
76 changes: 44 additions & 32 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,49 @@ testfile2 = download("https://raw.githubusercontent.com/JuliaPluto/PlutoSliderSe

is_CI = get(ENV, "CI", "no") == "no"

outfile = tempname(; cleanup=false) * ".pdf"
outdir = tempname(; cleanup=false)

@info "Files" outfile outdir testfile testfile2

result = pluto_to_pdf(testfile, outfile, outdir; open=is_CI, options=(format="A5",), screenshot_options=(scale=4,))

@test result == outfile

@test isfile(outfile)
@test dirname(outfile) == dirname(testfile)
@test endswith(outfile, ".pdf")

@test isdir(outdir)
filez = readdir(outdir)
@test length(filez) == 28
@test all(endswith.(filez, ".png"))
@test length(read(joinpath(outdir, filez[1]))) > 1000



outfile2 = pluto_to_pdf(testfile2; open=is_CI, options=(format="A5",))
@info "Result" outfile2
@test isfile(outfile2)
@test dirname(outfile2) == dirname(testfile2)
@test endswith(outfile2, ".pdf")

output_dir = get(ENV, "TEST_OUTPUT_DIR", nothing)
outfile_local = tempname(; cleanup=false) * ".pdf"
outfile_url = tempname(; cleanup=false) * ".pdf"
outdir_local = tempname(; cleanup=false)
outdir_url = tempname(; cleanup=false)

@info "Files" outfile_local outdir_local outfile_url outdir_url testfile testfile2

result_local = pluto_to_pdf(testfile, outfile_local, outdir_local; open=is_CI, options=(format="A5",), screenshot_options=(scale=4,))
@test result_local == outfile_local
@test dirname(outfile_local) == dirname(testfile)

result_url = html_to_pdf("https://featured.plutojl.org/puzzles-games/tower%20of%20hanoi", outfile_url, outdir_url; open=is_CI, options=(format="A4",), screenshot_options=(scale=3,))
@test result_url == outfile_url

@testset "Main $(type)" for type in (:local, :url)
outfile = getproperty(@__MODULE__, Symbol(:outfile_, type))
outdir = getproperty(@__MODULE__, Symbol(:outdir_, type))

@test isfile(outfile)
@test endswith(outfile, ".pdf")

@test isdir(outdir)
filez = readdir(outdir)
@test length(filez) == 28
@test all(endswith.(filez, ".png"))
@test length(read(joinpath(outdir, filez[1]))) > 1000
end

if @show(output_dir) isa String
mkpath(output_dir)
@assert isdir(output_dir)
cp(outfile, joinpath(output_dir, "hanoi.pdf"))
outfile_small = pluto_to_pdf(testfile2; open=is_CI, options=(format="A5",))
@info "Result" outfile_small
@test isfile(outfile_small)
@test dirname(outfile_small) == dirname(testfile2)
@test endswith(outfile_small, ".pdf")

test_output_dir = get(ENV, "TEST_OUTPUT_DIR", nothing)

if @show(test_output_dir) isa String
mkpath(test_output_dir)
@assert isdir(test_output_dir)



cp(outfile_local, joinpath(test_output_dir, "hanoi.pdf"))
cp(outfile_small, joinpath(test_output_dir, "small.pdf"))
cp(outdir_local, joinpath(test_output_dir, "hanoi_screenshots"))
end

0 comments on commit 0273da4

Please sign in to comment.