Browse Source

Make URLs configurable, fixes #79.

pull/84/head
Fredrik Ekre 6 years ago
parent
commit
2b3e78bfde
  1. 18
      docs/Manifest.toml
  2. 114
      src/Literate.jl
  3. 279
      test/runtests.jl

18
docs/Manifest.toml

@ -34,9 +34,9 @@ version = "1.1.0"
[[DataStructures]] [[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"] deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "1fe8fad5fc84686dcbc674aa255bc867a64f8132" git-tree-sha1 = "a1b652fb77ae8ca7ea328fa7ba5aa151036e5c10"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.5" version = "0.17.6"
[[Dates]] [[Dates]]
deps = ["Printf"] deps = ["Printf"]
@ -58,9 +58,9 @@ version = "0.8.1"
[[Documenter]] [[Documenter]]
deps = ["Base64", "Dates", "DocStringExtensions", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"] deps = ["Base64", "Dates", "DocStringExtensions", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "0e52069b5970cb27234153f578227947565152c5" git-tree-sha1 = "7f6ad432deb42aa108bc79205ee96d28724b084f"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.24.0" version = "0.24.1"
[[FFMPEG]] [[FFMPEG]]
deps = ["BinaryProvider", "Libdl"] deps = ["BinaryProvider", "Libdl"]
@ -74,10 +74,10 @@ uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.6.1" version = "0.6.1"
[[GR]] [[GR]]
deps = ["Base64", "DelimitedFiles", "LinearAlgebra", "Pkg", "Printf", "Random", "Serialization", "Sockets", "Test"] deps = ["Base64", "DelimitedFiles", "LinearAlgebra", "Printf", "Random", "Serialization", "Sockets", "Test"]
git-tree-sha1 = "2b29ed254586324366e43f4283880978e6956d1f" git-tree-sha1 = "c690c2ab22ac9ee323d9966deae61a089362b25c"
uuid = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" uuid = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
version = "0.42.0" version = "0.44.0"
[[GeometryTypes]] [[GeometryTypes]]
deps = ["ColorTypes", "FixedPointNumbers", "IterTools", "LinearAlgebra", "StaticArrays"] deps = ["ColorTypes", "FixedPointNumbers", "IterTools", "LinearAlgebra", "StaticArrays"]
@ -173,9 +173,9 @@ version = "0.6.1"
[[Plots]] [[Plots]]
deps = ["Base64", "Contour", "Dates", "FFMPEG", "FixedPointNumbers", "GR", "GeometryTypes", "JSON", "LinearAlgebra", "Measures", "NaNMath", "Pkg", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "Reexport", "Requires", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs"] deps = ["Base64", "Contour", "Dates", "FFMPEG", "FixedPointNumbers", "GR", "GeometryTypes", "JSON", "LinearAlgebra", "Measures", "NaNMath", "Pkg", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "Reexport", "Requires", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs"]
git-tree-sha1 = "0d7cf139b508defb467ac4fd655f4bd4d92f30cf" git-tree-sha1 = "11c75a31269c1c64790e7cb910346f64cd4440c1"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
version = "0.27.0" version = "0.27.1"
[[Printf]] [[Printf]]
deps = ["Unicode"] deps = ["Unicode"]

114
src/Literate.jl

@ -175,56 +175,16 @@ function replace_default(content, sym;
push!(repls, "@__NAME__" => config["name"]::String) push!(repls, "@__NAME__" => config["name"]::String)
# fix links # fix links
if get(ENV, "DOCUMENTATIONGENERATOR", "") == "true" if get(ENV, "DOCUMENTATIONGENERATOR", "") == "true"
## DocumentationGenerator.jl ## DocumentationGenerator.jl
### URL to the root of the deployment, see
### https://github.com/JuliaDocs/DocumentationGenerator.jl/pull/76
base_url = get(ENV, "DOCUMENTATIONGENERATOR_BASE_URL", "DOCUMENTATIONGENERATOR_BASE_URL") base_url = get(ENV, "DOCUMENTATIONGENERATOR_BASE_URL", "DOCUMENTATIONGENERATOR_BASE_URL")
### replace @__REPO_ROOT_URL__ to master/commit
# TODO
# repo_root_url = "https://github.com/$(travis_repo_slug)/blob/$(commit)"
# push!(repls, "@__REPO_ROOT_URL__" => repo_root_url)
### replace @__NBVIEWER_ROOT_URL__ to dev or version directory
nbviewer_root_url = "https://nbviewer.jupyter.org/urls/$(base_url)" nbviewer_root_url = "https://nbviewer.jupyter.org/urls/$(base_url)"
push!(repls, "@__NBVIEWER_ROOT_URL__" => nbviewer_root_url) push!(repls, "@__NBVIEWER_ROOT_URL__" => nbviewer_root_url)
### replace @__BINDER_ROOT_URL__ to dev or version directory
### TODO: Binder requires files to be in a git repository :(
if match(r"@__BINDER_ROOT_URL__", content) !== nothing
@warn("mybinder.org requires the notebook to be in a git repository, " *
"which does not work with DocumentationGenerator.jl")
end
elseif haskey(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL") || haskey(ENV, "GITHUB_ACTION")
## Travis CI / GitHub Actions
### Use same logic as Documenter to figure out the deploy folder
repo_slug = get(ENV, "TRAVIS_REPO_SLUG", get(ENV, "GITHUB_REPOSITORY", "REPO_SLUG"))
tag = get(ENV, "TRAVIS_TAG") do
github_ref = get(ENV, "GITHUB_REF", nothing)
github_ref === nothing && return nothing
m = match(r"^refs/tags/(.*)$", github_ref)
m === nothing && return nothing
return String(m.captures[1])
end
### use the versioned directory for links, even for the stable
### and release folders since these will not change
folder = (tag === nothing || isempty(tag)) ? "dev" : tag
### replace @__REPO_ROOT_URL__ to master/commit
repo_root_url = "https://github.com/$(repo_slug)/blob/$(commit)"
push!(repls, "@__REPO_ROOT_URL__" => repo_root_url)
### replace @__NBVIEWER_ROOT_URL__ to dev or version directory
nbviewer_root_url = "https://nbviewer.jupyter.org/github/$(repo_slug)/blob/$(branch)/$(folder)"
push!(repls, "@__NBVIEWER_ROOT_URL__" => nbviewer_root_url)
### replace @__BINDER_ROOT_URL__ to dev or version directory
binder_root_url = "https://mybinder.org/v2/gh/$(repo_slug)/$(branch)?filepath=$(folder)"
push!(repls, "@__BINDER_ROOT_URL__" => binder_root_url)
else else
## Warn about broken link expansions push!(repls, "@__REPO_ROOT_URL__" => get(config, "repo_root_url", "<unknown>"))
if (match(r"@__REPO_ROOT_URL__", content) !== nothing) || push!(repls, "@__NBVIEWER_ROOT_URL__" => get(config, "nbviewer_root_url", "<unknown>"))
(match(r"@__NBVIEWER_ROOT_URL__", content) !== nothing) || push!(repls, "@__BINDER_ROOT_URL__" => get(config, "binder_root_url", "<unknown>"))
(match(r"@__BINDER_ROOT_URL__", content) !== nothing)
@warn("expansion of `@__REPO_ROOT_URL__`, `@__NBVIEWER_ROOT_URL__` and " *
" `@__BINDER_ROOT_URL__` will only be correct if running from " *
"DocumentationGenerator.jl, Travis CI or GitHub Actions.")
end
end end
# run some Documenter specific things # run some Documenter specific things
@ -262,6 +222,53 @@ function create_configuration(inputfile; user_config, user_kwargs)
cfg["codefence"] = get(user_config, "documenter", true) ? cfg["codefence"] = get(user_config, "documenter", true) ?
("```@example $(get(user_config, "name", cfg["name"]))" => "```") : ("```julia" => "```") ("```@example $(get(user_config, "name", cfg["name"]))" => "```") : ("```julia" => "```")
cfg["execute"] = true cfg["execute"] = true
# Guess the package (or repository) root url
edit_commit = "master" # TODO: Make this configurable like Documenter?
deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter?
if haskey(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL") # Travis CI
repo_slug = get(ENV, "TRAVIS_REPO_SLUG", "unknown-repository")
deploy_folder = if get(ENV, "TRAVIS_PULL_REQUEST", nothing) == "false"
get(ENV, "TRAVIS_TAG", get(user_config, "devurl", "dev"))
else
"previews/PR$(get(ENV, "TRAVIS_PULL_REQUEST", "##"))"
end
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)"
cfg["nbviewer_root_url"] = "https://nbviewer.jupyter.org/github/$(repo_slug)/blob/$(deploy_branch)/$(deploy_folder)"
cfg["binder_root_url"] = "https://mybinder.org/v2/gh/$(repo_slug)/$(deploy_branch)?filepath=$(deploy_folder)"
if (dir = get(ENV, "TRAVIS_BUILD_DIR", nothing)) !== nothing
cfg["repo_root_path"] = dir
end
elseif haskey(ENV, "GITHUB_ACTIONS")
repo_slug = get(ENV, "GITHUB_REPOSITORY", "unknown-repository")
deploy_folder = if get(ENV, "GITHUB_EVENT_NAME", nothing) == "push"
if (m = match(r"^refs\/tags\/(.*)$", get(ENV, "GITHUB_REF", ""))) !== nothing
String(m.captures[1])
else
get(user_config, "devurl", "dev")
end
elseif (m = match(r"refs\/pull\/(\d+)\/merge", get(ENV, "GITHUB_REF", ""))) !== nothing
"previews/PR$(m.captures[1])"
else
"dev"
end
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)"
cfg["nbviewer_root_url"] = "https://nbviewer.jupyter.org/github/$(repo_slug)/blob/$(deploy_branch)/$(deploy_folder)"
cfg["binder_root_url"] = "https://mybinder.org/v2/gh/$(repo_slug)/$(deploy_branch)?filepath=$(deploy_folder)"
if (dir = get(ENV, "GITHUB_WORKSPACE", nothing)) !== nothing
cfg["repo_root_path"] = dir
end
elseif haskey(ENV, "GITLAB_CI")
if (url = get(ENV, "CI_PROJECT_URL", nothing)) !== nothing
cfg["repo_root_url"] = "$(url)/blob/$(edit_commit)"
end
if (url = get(ENV, "CI_PAGES_URL", nothing)) !== nothing &&
(m = match(r"https://(.+)", url)) !== nothing
cfg["nbviewer_root_url"] = "https://nbviewer.jupyter.org/urls/$(m[1])"
end
if (dir = get(ENV, "CI_PROJECT_DIR", nothing)) !== nothing
cfg["repo_root_path"] = dir
end
end
# Merge default_config with user_config # Merge default_config with user_config
merge!(cfg, user_config) merge!(cfg, user_config)
@ -285,6 +292,11 @@ See the manual section about [Configuration](@ref) for more information.
| `keep_comments` | When `true`, keeps markdown lines as comments in the output script. | `false` | Only applicable for `Literate.script`. | | `keep_comments` | When `true`, keeps markdown lines as comments in the output script. | `false` | Only applicable for `Literate.script`. |
| `codefence` | Pair containing opening and closing fence for wrapping code blocks. | `````"```julia" => "```"````` | If `documenter` is `true` the default is `````"```@example"=>"```"`````. | | `codefence` | Pair containing opening and closing fence for wrapping code blocks. | `````"```julia" => "```"````` | If `documenter` is `true` the default is `````"```@example"=>"```"`````. |
| `execute` | Whether to execute and capture the output. | `true` | Only applicable for `Literate.notebook`. | | `execute` | Whether to execute and capture the output. | `true` | Only applicable for `Literate.notebook`. |
| `devurl` | URL for "in-development" docs. | `"dev"` | See [Documenter docs](https://juliadocs.github.io/Documenter.jl/). Unused if `repo_root_url`/`nbviewer_root_url`/`binder_root_url` are set. |
| `repo_root_url` | URL to the root of the repository. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for `@__REPO_ROOT_URL__`. |
| `nbviewer_root_url` | URL to the root of the repository as seen on nbviewer. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for `@__NBVIEWER_ROOT_URL__`. |
| `binder_root_url` | URL to the root of the repository as seen on mybinder. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for `@__BINDER_ROOT_URL__`. |
| `repo_root_path` | Filepath to the root of the repository. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for computing [Documenters `EditURL`](@ref Interaction-with-Documenter). |
""" """
const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation
@ -377,20 +389,8 @@ function markdown(inputfile, outputdir; config::Dict=Dict(), kwargs...)
# run some Documenter specific things # run some Documenter specific things
if config["documenter"]::Bool if config["documenter"]::Bool
# change the Edit on GitHub link # change the Edit on GitHub link
repo = get(ENV, "TRAVIS_REPO_SLUG", get(ENV, "GITHUB_REPOSITORY", nothing)) path = relpath(inputfile, get(config, "repo_root_path", pwd())::String)
if repo === nothing
path = ""
else
pkg = String(first(split(last(split(repo, '/')), '.')))
pkgsrc = Base.find_package(pkg)
if pkgsrc === nothing
path = ""
else
repo_root = first(split(pkgsrc, joinpath("src", pkg * ".jl")))
path = relpath(inputfile, repo_root)
path = replace(path, "\\" => "/") path = replace(path, "\\" => "/")
end
end
content = """ content = """
# ```@meta # ```@meta
# EditURL = "@__REPO_ROOT_URL__/$(path)" # EditURL = "@__REPO_ROOT_URL__/$(path)"

279
test/runtests.jl

@ -237,9 +237,31 @@ content = """
#nb # # Explicit markdown cell with metadata #nb # # Explicit markdown cell with metadata
""" """
const expansion_warning = get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true" ? () : (:warn, r"expansion of") const TRAVIS_ENV = Dict(
"TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl",
@testset "Literate.script" begin "TRAVIS_TAG" => "v1.2.0",
"TRAVIS_PULL_REQUEST" => "false",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true",
"TRAVIS_BUILD_DIR" => normpath(joinpath(@__DIR__, "..")),
)
const ACTIONS_ENV = Dict(
"GITHUB_ACTIONS" => "true",
"GITHUB_ACTION" => "Build docs",
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl",
"GITHUB_EVENT_NAME" => "push",
"GITHUB_REF" => "refs/tags/v1.2.0",
"GITHUB_WORKSPACE" => normpath(joinpath(@__DIR__, "..")),
(k => nothing for k in keys(TRAVIS_ENV))...,
)
const GITLAB_ENV = Dict(
"GITLAB_CI" => "true",
"CI_PROJECT_URL" => "https://gitlab.com/fredrikekre/Literate.jl",
"CI_PAGES_URL" => "https://fredrikekre.gitlab.io/Literate.jl",
"CI_PROJECT_DIR" => normpath(joinpath(@__DIR__, "..")),
(k => nothing for k in keys(TRAVIS_ENV))...,
(k => nothing for k in keys(ACTIONS_ENV))...,
)
@testset "Literate.script" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox mktempdir(@__DIR__) do sandbox
cd(sandbox) do cd(sandbox) do
# write content to inputfile # write content to inputfile
@ -248,9 +270,7 @@ const expansion_warning = get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true"
outdir = mktempdir(pwd()) outdir = mktempdir(pwd())
# test defaults # test defaults
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...) do
"TRAVIS_TAG" => "v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
Literate.script(inputfile, outdir) Literate.script(inputfile, outdir)
end end
expected_script = """ expected_script = """
@ -290,54 +310,70 @@ const expansion_warning = get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true"
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test script == expected_script @test script == expected_script
# no tag -> latest directory # Travis with with PR preview build
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "", "TRAVIS_TAG" => nothing,
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do "TRAVIS_PULL_REQUEST" => "42") do
Literate.script(inputfile, outdir) Literate.script(inputfile, outdir)
end end
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", script) @test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", script)
# GitHub Actions with a tag # Travis with no tag -> dev directory
withenv("GITHUB_ACTION" => "Build docs", withenv(TRAVIS_ENV...,
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl",
"GITHUB_REF" => "refs/tags/v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing,
"TRAVIS_REPO_SLUG" => nothing,
"TRAVIS_TAG" => nothing) do "TRAVIS_TAG" => nothing) do
Literate.script(inputfile, outdir) Literate.script(inputfile, outdir)
end end
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/", script) @test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", script)
# GitHub Actions without a tag # GitHub Actions with a tag
withenv("GITHUB_ACTION" => "Build docs", withenv(ACTIONS_ENV...) do
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl",
"GITHUB_REF" => "refs/heads/master",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing,
"TRAVIS_REPO_SLUG" => nothing,
"TRAVIS_TAG" => nothing) do
Literate.script(inputfile, outdir) Literate.script(inputfile, outdir)
end end
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", script) @test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl", script)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
"GITHUB_EVENT_NAME" => "pull_request",
"GITHUB_REF" => "refs/pull/42/merge") do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", script)
# GitHub Actions without a tag -> dev directory
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/heads/master") do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", script)
# building under DocumentationGenerator.jl # building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true", withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do "DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
@test_logs((:warn, r"mybinder.org"), match_mode=:any, Literate.script(inputfile, outdir)
Literate.script(inputfile, outdir))
end end
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", script) @test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", script)
@test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script) @test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
# pre- and post-processing # pre- and post-processing
@test_logs(expansion_warning, match_mode=:any,
Literate.script(inputfile, outdir, Literate.script(inputfile, outdir,
preprocess = x -> replace(x, "PLACEHOLDER3" => "3REDLOHECALP"), preprocess = x -> replace(x, "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = x -> replace(x, "PLACEHOLDER4" => "4REDLOHECALP"))) postprocess = x -> replace(x, "PLACEHOLDER4" => "4REDLOHECALP"))
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test !occursin("PLACEHOLDER1", script) @test !occursin("PLACEHOLDER1", script)
@test !occursin("PLACEHOLDER2", script) @test !occursin("PLACEHOLDER2", script)
@ -347,16 +383,14 @@ const expansion_warning = get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true"
@test occursin("4REDLOHECALP", script) @test occursin("4REDLOHECALP", script)
# name # name
@test_logs(expansion_warning, match_mode=:any, Literate.script(inputfile, outdir, name = "foobar")
Literate.script(inputfile, outdir, name = "foobar"))
script = read(joinpath(outdir, "foobar.jl"), String) script = read(joinpath(outdir, "foobar.jl"), String)
@test occursin("name: foobar", script) @test occursin("name: foobar", script)
@test !occursin("name: inputfile", script) @test !occursin("name: inputfile", script)
@test !occursin("name: @__NAME__", script) @test !occursin("name: @__NAME__", script)
# keep_comments # keep_comments
@test_logs(expansion_warning, match_mode=:any, Literate.script(inputfile, outdir, keep_comments = true)
Literate.script(inputfile, outdir, keep_comments = true))
script = read(joinpath(outdir, "inputfile.jl"), String) script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# # Example", script) @test occursin("# # Example", script)
@test occursin("# foo, bar", script) @test occursin("# foo, bar", script)
@ -366,9 +400,9 @@ const expansion_warning = get(ENV, "HAS_JOSH_K_SEAL_OF_APPROVAL", "") == "true"
@test_throws ArgumentError Literate.script("nonexistent.jl", outdir) @test_throws ArgumentError Literate.script("nonexistent.jl", outdir)
end end
end end
end end end
@testset "Literate.markdown" begin @testset "Literate.markdown" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox mktempdir(@__DIR__) do sandbox
cd(sandbox) do cd(sandbox) do
# write content to inputfile # write content to inputfile
@ -377,9 +411,7 @@ end
outdir = mktempdir(pwd()) outdir = mktempdir(pwd())
# test defaults # test defaults
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...) do
"TRAVIS_TAG" => "v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
Literate.markdown(inputfile, outdir) Literate.markdown(inputfile, outdir)
end end
expected_markdown = """ expected_markdown = """
@ -476,54 +508,85 @@ end
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test markdown == expected_markdown @test markdown == expected_markdown
# no tag -> latest directory # Travis with PR preview build
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "", "TRAVIS_TAG" => nothing,
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do "TRAVIS_PULL_REQUEST" => "42") do
Literate.markdown(inputfile, outdir) Literate.markdown(inputfile, outdir)
end end
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", markdown) @test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# GitHub Actions with a tag # Travis with no tag -> dev directory
withenv("GITHUB_ACTION" => "Build docs", withenv(TRAVIS_ENV...,
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl",
"GITHUB_REF" => "refs/tags/v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing,
"TRAVIS_REPO_SLUG" => nothing,
"TRAVIS_TAG" => nothing) do "TRAVIS_TAG" => nothing) do
Literate.markdown(inputfile, outdir) Literate.markdown(inputfile, outdir)
end end
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/", markdown) @test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# GitHub Actions without a tag # GitHub Actions with a tag
withenv("GITHUB_ACTION" => "Build docs", withenv(ACTIONS_ENV...) do
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl", Literate.markdown(inputfile, outdir)
"GITHUB_REF" => "refs/heads/master", end
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing, markdown = read(joinpath(outdir, "inputfile.md"), String)
"TRAVIS_REPO_SLUG" => nothing, @test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
"TRAVIS_TAG" => nothing) do @test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/pull/42/merge",
"GITHUB_EVENT_NAME" => "pull_request") do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# GitHub Actions without a tag -> dev directory
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/heads/master") do
Literate.markdown(inputfile, outdir) Literate.markdown(inputfile, outdir)
end end
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", markdown) @test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# GitLab CI with GitLab Pages
withenv(GITLAB_ENV...) do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://gitlab.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/urls/fredrikekre.gitlab.io/Literate.jl/file.jl", markdown)
@test_broken occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://gitlab.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
# building under DocumentationGenerator.jl # building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true", withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do "DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
@test_logs((:warn, r"mybinder.org"), match_mode=:any, Literate.markdown(inputfile, outdir)
Literate.markdown(inputfile, outdir))
end end
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", markdown) @test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", markdown)
@test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown) @test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
# pre- and post-processing # pre- and post-processing
@test_logs(expansion_warning, match_mode=:any,
Literate.markdown(inputfile, outdir, Literate.markdown(inputfile, outdir,
preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"), preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = x -> replace(replace(x, "PLACEHOLDER2" => "2REDLOHECALP"), "PLACEHOLDER4" => "4REDLOHECALP"))) postprocess = x -> replace(replace(x, "PLACEHOLDER2" => "2REDLOHECALP"), "PLACEHOLDER4" => "4REDLOHECALP"))
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test !occursin("PLACEHOLDER1", markdown) @test !occursin("PLACEHOLDER1", markdown)
@test !occursin("PLACEHOLDER2", markdown) @test !occursin("PLACEHOLDER2", markdown)
@ -535,8 +598,7 @@ end
@test occursin("4REDLOHECALP", markdown) @test occursin("4REDLOHECALP", markdown)
# documenter = false # documenter = false
@test_logs(expansion_warning, match_mode=:any, Literate.markdown(inputfile, outdir, documenter = false)
Literate.markdown(inputfile, outdir, documenter = false))
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("```julia", markdown) @test occursin("```julia", markdown)
@test !occursin("```@example", markdown) @test !occursin("```@example", markdown)
@ -544,16 +606,14 @@ end
@test !occursin("EditURL", markdown) @test !occursin("EditURL", markdown)
# codefence # codefence
@test_logs(expansion_warning, match_mode=:any, Literate.markdown(inputfile, outdir, codefence = "```c" => "```")
Literate.markdown(inputfile, outdir, codefence = "```c" => "```"))
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("```c", markdown) @test occursin("```c", markdown)
@test !occursin("```@example", markdown) @test !occursin("```@example", markdown)
@test !occursin("```julia", markdown) @test !occursin("```julia", markdown)
# name # name
@test_logs(expansion_warning, match_mode=:any, Literate.markdown(inputfile, outdir, name = "foobar")
Literate.markdown(inputfile, outdir, name = "foobar"))
markdown = read(joinpath(outdir, "foobar.md"), String) markdown = read(joinpath(outdir, "foobar.md"), String)
@test occursin("```@example foobar", markdown) @test occursin("```@example foobar", markdown)
@test !occursin("```@example inputfile", markdown) @test !occursin("```@example inputfile", markdown)
@ -565,9 +625,9 @@ end
@test_throws ArgumentError Literate.markdown("nonexistent.jl", outdir) @test_throws ArgumentError Literate.markdown("nonexistent.jl", outdir)
end end
end end
end end end
@testset "Literate.notebook" begin @testset "Literate.notebook" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox mktempdir(@__DIR__) do sandbox
cd(sandbox) do cd(sandbox) do
# write content to inputfile # write content to inputfile
@ -576,9 +636,7 @@ end
outdir = mktempdir(pwd()) outdir = mktempdir(pwd())
# test defaults # test defaults
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...) do
"TRAVIS_TAG" => "v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
Literate.notebook(inputfile, outdir, execute = false) Literate.notebook(inputfile, outdir, execute = false)
end end
expected_cells = rstrip.(( expected_cells = rstrip.((
@ -731,33 +789,32 @@ end
end end
# no tag -> latest directory # no tag -> latest directory
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl", withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "", "TRAVIS_TAG" => nothing) do
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
Literate.notebook(inputfile, outdir, execute = false) Literate.notebook(inputfile, outdir, execute = false)
end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", notebook) @test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", notebook)
# GitHub Actions with a tag # GitHub Actions with a tag
withenv("GITHUB_ACTION" => "Build docs", withenv(ACTIONS_ENV...) do
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl",
"GITHUB_REF" => "refs/tags/v1.2.0",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing,
"TRAVIS_REPO_SLUG" => nothing,
"TRAVIS_TAG" => nothing) do
Literate.notebook(inputfile, outdir, execute = false) Literate.notebook(inputfile, outdir, execute = false)
end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/", notebook) @test occursin("fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/", notebook)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/pull/42/merge",
"GITHUB_EVENT_NAME" => "pull_request") do
Literate.notebook(inputfile, outdir, execute = false)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/", notebook)
# GitHub Actions without a tag # GitHub Actions without a tag
withenv("GITHUB_ACTION" => "Build docs", withenv(ACTIONS_ENV...,
"GITHUB_REPOSITORY" => "fredrikekre/Literate.jl", "GITHUB_REF" => "refs/heads/master") do
"GITHUB_REF" => "refs/heads/master",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => nothing,
"TRAVIS_REPO_SLUG" => nothing,
"TRAVIS_TAG" => nothing) do
Literate.notebook(inputfile, outdir, execute = false) Literate.notebook(inputfile, outdir, execute = false)
end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@ -766,8 +823,7 @@ end
# building under DocumentationGenerator.jl # building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true", withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do "DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
@test_logs((:warn, r"mybinder.org"), match_mode=:any, Literate.notebook(inputfile, outdir, execute = false)
Literate.notebook(inputfile, outdir, execute = false))
end end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", notebook) @test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", notebook)
@ -783,10 +839,9 @@ end
end end
return nb return nb
end end
@test_logs(expansion_warning, match_mode=:any,
Literate.notebook(inputfile, outdir, execute = false, Literate.notebook(inputfile, outdir, execute = false,
preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"), preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = post)) postprocess = post)
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test !occursin("PLACEHOLDER1", notebook) @test !occursin("PLACEHOLDER1", notebook)
@test !occursin("PLACEHOLDER2", notebook) @test !occursin("PLACEHOLDER2", notebook)
@ -798,23 +853,20 @@ end
@test occursin("4REDLOHECALP", notebook) @test occursin("4REDLOHECALP", notebook)
# documenter = false # documenter = false
@test_logs(expansion_warning, match_mode=:any, Literate.notebook(inputfile, outdir, documenter = false, execute = false)
Literate.notebook(inputfile, outdir, documenter = false, execute = false))
notebook = read(joinpath(outdir, "inputfile.ipynb"), String) notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("# [Example](@id example-id", notebook) @test occursin("# [Example](@id example-id", notebook)
@test occursin("[foo](@ref), [bar](@ref bbaarr)", notebook) @test occursin("[foo](@ref), [bar](@ref bbaarr)", notebook)
# name # name
@test_logs(expansion_warning, match_mode=:any, Literate.notebook(inputfile, outdir, name = "foobar", execute = false)
Literate.notebook(inputfile, outdir, name = "foobar", execute = false))
notebook = read(joinpath(outdir, "foobar.ipynb"), String) notebook = read(joinpath(outdir, "foobar.ipynb"), String)
@test occursin("name: foobar", notebook) @test occursin("name: foobar", notebook)
@test !occursin("name: inputfile", notebook) @test !occursin("name: inputfile", notebook)
@test !occursin("name: @__NAME__", notebook) @test !occursin("name: @__NAME__", notebook)
# execute = true # execute = true
@test_logs(expansion_warning, match_mode=:any, Literate.notebook(inputfile, outdir)
Literate.notebook(inputfile, outdir))
expected_outputs = rstrip.(( expected_outputs = rstrip.((
""" """
"cells": [ "cells": [
@ -877,4 +929,33 @@ end
@test occursin("\"application/vnd.vegalite.v2+json\":", notebook) @test occursin("\"application/vnd.vegalite.v2+json\":", notebook)
end end
end end
end end
@testset "Configuration" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
# write content to inputfile
inputfile = "inputfile.jl"
write(inputfile, content)
outdir = mktempdir(pwd())
config=Dict(
"repo_root_url" => "www.example1.com",
"nbviewer_root_url" => "www.example2.com",
"binder_root_url" => "www.example3.com",
)
# Overwriting of URLs
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl",
"TRAVIS_TAG" => nothing,
"TRAVIS_PULL_REQUEST" => "false",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
Literate.script(inputfile, outdir; config=config)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("Link to repo root: www.example1.com/file.jl", script)
@test occursin("Link to nbviewer: www.example2.com/file.jl", script)
@test occursin("Link to binder: www.example3.com/file.jl", script)
end
end end
end end

Loading…
Cancel
Save