Skip to content
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
35 changes: 22 additions & 13 deletions lib/ex_doc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,30 @@ defmodule ExDoc do
end
end

defp guess_url(url = <<"https://github.com/", _ :: binary>>, ref) do
append_slash(url) <> "blob/#{ref}/%{path}#L%{line}"
end

defp guess_url(url = <<"https://gitlab.com/", _ :: binary>>, ref) do
append_slash(url) <> "blob/#{ref}/%{path}#L%{line}"
end

defp guess_url(url = <<"https://bitbucket.org/", _ :: binary>>, ref) do
append_slash(url) <> "src/#{ref}/%{path}#cl-%{line}"
defp guess_url(url, ref) do
with {:ok, host_with_path} <- http_or_https(url),
{:ok, pattern} <- known_pattern(host_with_path, ref) do
"https://" <> append_slash(host_with_path) <> pattern
else
_ -> url
end
end

defp guess_url(other, _) do
other
end
defp http_or_https("http://" <> rest),
do: {:ok, rest}
defp http_or_https("https://" <> rest),
do: {:ok, rest}
defp http_or_https(_),
do: :error

defp known_pattern("github.com/" <> _, ref),
do: {:ok, "blob/#{ref}/%{path}#L%{line}"}
defp known_pattern("gitlab.com/" <> _, ref),
do: {:ok, "blob/#{ref}/%{path}#L%{line}"}
defp known_pattern("bitbucket.org/" <> _, ref),
do: {:ok, "src/#{ref}/%{path}#cl-%{line}"}
defp known_pattern(_host_with_path, _ref),
do: :error

defp append_slash(url) do
if :binary.last(url) == ?/, do: url, else: url <> "/"
Expand Down
29 changes: 18 additions & 11 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ defmodule ExDoc.Formatter.HTMLTest do
end

test "guess url base on source_url and source_root options" do
generate_docs doc_config(source_url: "https://github.com/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!("#{output_dir()}/CompiledWithDocs.html")
assert content =~ "https://github.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L13"

generate_docs doc_config(source_url: "https://gitlab.com/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!("#{output_dir()}/CompiledWithDocs.html")
assert content =~ "https://gitlab.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L13"

generate_docs doc_config(source_url: "https://bitbucket.org/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!("#{output_dir()}/CompiledWithDocs.html")
assert content =~ "https://bitbucket.org/elixir-lang/ex_doc/src/master/test/fixtures/compiled_with_docs.ex#cl-13"
file_path = "#{output_dir()}/CompiledWithDocs.html"
for scheme <- ["http", "https"] do
generate_docs doc_config(source_url: "#{scheme}://github.com/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!(file_path)
assert content =~ "https://github.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L13"

generate_docs doc_config(source_url: "#{scheme}://gitlab.com/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!(file_path)
assert content =~ "https://gitlab.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L13"

generate_docs doc_config(source_url: "#{scheme}://bitbucket.org/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!(file_path)
assert content =~ "https://bitbucket.org/elixir-lang/ex_doc/src/master/test/fixtures/compiled_with_docs.ex#cl-13"

generate_docs doc_config(source_url: "#{scheme}://example.com/elixir-lang/ex_doc", source_root: File.cwd!)
content = File.read!(file_path)
assert content =~ "#{scheme}://example.com/elixir-lang/ex_doc"
end
end

test "find formatter when absolute path to module is given" do
Expand Down