Skip to content

Commit

Permalink
Change EditURL output to use relative path (#219)
Browse files Browse the repository at this point in the history
This patch changes the generated EditURl to use a relative path (source
file relative output directory) and let Documenter figure out the remote
URL instead. This is required for Documenter version 1, but works also
on older Documenter versions.
  • Loading branch information
fredrikekre committed Aug 4, 2023
1 parent 515cd34 commit e1c980a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
18 changes: 3 additions & 15 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,6 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(cfg["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"
Expand All @@ -362,9 +359,6 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(cfg["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/$(cfg["edit_commit"])"
Expand All @@ -373,9 +367,6 @@ function create_configuration(inputfile; user_config, user_kwargs, type=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
Expand Down Expand Up @@ -423,9 +414,6 @@ Available options:
- `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).
- `image_formats`: A vector of `(mime, ext)` tuples, with the default
`$(_DEFAULT_IMAGE_FORMATS)`. Results which are `showable` with a MIME type are saved with
the first match, with the corresponding extension.
Expand Down Expand Up @@ -470,11 +458,11 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
# run some Documenter specific things for markdown output
if type === :md && isdocumenter(config)
# change the Edit on GitHub link
path = relpath(inputfile, get(config, "repo_root_path", pwd())::String)
path = replace(path, "\\" => "/")
edit_url = relpath(inputfile, config["literate_outputdir"])
edit_url = replace(edit_url, "\\" => "/")
content = """
# ```@meta
# EditURL = "@__REPO_ROOT_URL__/$(path)"
# EditURL = "$(edit_url)"
# ```
""" * content
Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ end end
end
expected_markdown = """
```@meta
EditURL = "https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl"
EditURL = "../inputfile.jl"
```
# [Example](@id example-id)
Expand Down Expand Up @@ -645,7 +645,7 @@ end end
@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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# Travis with no tag -> dev directory
withenv(TRAVIS_ENV...,
Expand All @@ -656,7 +656,7 @@ end end
@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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# GitHub Actions with a tag
withenv(ACTIONS_ENV...) do
Expand All @@ -666,7 +666,7 @@ end end
@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/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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
Expand All @@ -678,7 +678,7 @@ end end
@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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# GitHub Actions without a tag -> dev directory
withenv(ACTIONS_ENV...,
Expand All @@ -689,7 +689,7 @@ end end
@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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# GitLab CI with GitLab Pages
withenv(GITLAB_ENV...) do
Expand All @@ -699,7 +699,7 @@ end end
@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)
@test occursin("EditURL = \"../inputfile.jl\"", markdown)

# building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true",
Expand Down

0 comments on commit e1c980a

Please sign in to comment.