Skip to content

Commit

Permalink
Automatically detect "edit branch", and let users
Browse files Browse the repository at this point in the history
override using edit_url kwarg, closes #179.
  • Loading branch information
fredrikekre committed Jan 25, 2022
1 parent 7c4bfc2 commit 3f4c17e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -307,15 +320,15 @@ 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
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)"
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
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f4c17e

Please sign in to comment.