Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect "edit branch" #184

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.11.0] - 2022-01-25
### 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
- Markdown execution now also support `image/svg+xml`. ([#182][github-182], [#183][github-183])
Expand Down Expand Up @@ -159,10 +166,14 @@ 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
[Unreleased]: https://github.com/fredrikekre/Literate.jl/compare/v2.11.0...HEAD
[2.11.0]: https://github.com/fredrikekre/Literate.jl/compare/v2.10.0...v2.11.0
[2.10.0]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.4...v2.10.0
[2.9.4]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.3...v2.9.4
[2.9.3]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.2...v2.9.3
[2.9.2]: https://github.com/fredrikekre/Literate.jl/compare/v2.9.1...v2.9.2
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Literate"
uuid = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
version = "2.10.0"
version = "2.11.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
2 changes: 1 addition & 1 deletion docs/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
deps = ["Base64", "IOCapture", "JSON", "REPL"]
path = ".."
uuid = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
version = "2.10.0"
version = "2.11.0"

[[LogExpFunctions]]
deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"]
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