Browse Source

Automatically detect "edit branch", and let users

override using edit_url kwarg, closes #179.
pull/186/head
Fredrik Ekre 4 years ago
parent
commit
3f4c17eca4
  1. 7
      CHANGELOG.md
  2. 23
      src/Literate.jl
  3. 8
      test/runtests.jl

7
CHANGELOG.md

@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Literate now tries to figure out the branch/commit that `EditURL` should point to
automatically instead of always defaulting to `"master"`. For typical setups the
auto-detection should be sufficient, but you can also set it explicitly by passing
`edit_commit`, for example `edit_commit = "main"`. ([#179][github-179], [#184][github-184])
## [2.10.0] - 2022-01-24
### Added
@ -159,8 +164,10 @@ https://discourse.julialang.org/t/ann-literate-jl/10651 for release announcement @@ -159,8 +164,10 @@ https://discourse.julialang.org/t/ann-literate-jl/10651 for release announcement
[github-169]: https://github.com/fredrikekre/Literate.jl/pull/169
[github-171]: https://github.com/fredrikekre/Literate.jl/issues/171
[github-172]: https://github.com/fredrikekre/Literate.jl/pull/172
[github-179]: https://github.com/fredrikekre/Literate.jl/issues/179
[github-182]: https://github.com/fredrikekre/Literate.jl/issues/182
[github-183]: https://github.com/fredrikekre/Literate.jl/pull/183
[github-184]: https://github.com/fredrikekre/Literate.jl/pull/184
[Unreleased]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.4...HEAD
[2.9.4]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.3...v2.9.4

23
src/Literate.jl

@ -264,8 +264,21 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -264,8 +264,21 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") :
("````julia" => "````")
cfg["image_formats"] = _DEFAULT_IMAGE_FORMATS
# Guess the package (or repository) root url
edit_commit = "master" # TODO: Make this configurable like Documenter?
# Guess the package (or repository) root url with "master" as fallback
# see JuliaDocs/Documenter.jl#1751
fallback_edit_commit = "master"
if (git = Sys.which("git"); git !== nothing)
try
str = read(pipeline(ignorestatus(
setenv(`$(git) remote show origin`; dir=dirname(inputfile))
), stderr=devnull), String)
if (m = match(r"^\s*HEAD branch:\s*(.*)$"m, str); m !== nothing)
fallback_edit_commit = String(m[1])
end
catch
end
end
cfg["edit_commit"] = get(user_config, "edit_commit", fallback_edit_commit)
deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter?
# Strip build version from a tag (cf. JuliaDocs/Documenter.jl#1298, Literate.jl#162)
function version_tag_strip_build(tag)
@ -288,7 +301,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -288,7 +301,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
else
"previews/PR$(get(ENV, "TRAVIS_PULL_REQUEST", "##"))"
end
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)"
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
@ -307,7 +320,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -307,7 +320,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
else
"dev"
end
cfg["repo_root_url"] = "https://github.com/$(repo_slug)/blob/$(edit_commit)"
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
@ -315,7 +328,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -315,7 +328,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
end
elseif haskey(ENV, "GITLAB_CI")
if (url = get(ENV, "CI_PROJECT_URL", nothing)) !== nothing
cfg["repo_root_url"] = "$(url)/blob/$(edit_commit)"
cfg["repo_root_url"] = "$(url)/blob/$(cfg["edit_commit"])"
end
if (url = get(ENV, "CI_PAGES_URL", nothing)) !== nothing &&
(m = match(r"https://(.+)", url)) !== nothing

8
test/runtests.jl

@ -762,6 +762,14 @@ end end @@ -762,6 +762,14 @@ end end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test !occursin("md\"\"\"", markdown)
# edit_commit
withenv(ACTIONS_ENV...) do
Literate.markdown(inputfile, outdir; edit_commit="retsam")
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("blob/retsam/", markdown)
@test !occursin("blob/master/", markdown)
# execute
write(inputfile, """
using DisplayAs

Loading…
Cancel
Save