From 3b0870e6fae8098feea762956ee5c11019a3b928 Mon Sep 17 00:00:00 2001 From: eksperimental Date: Sat, 20 May 2017 14:42:24 +0700 Subject: [PATCH] Deal with http:// when guessing source_url_pattern Closes https://github.com/elixir-lang/ex_doc/issues/717 It rewrites "http" to "https" when building source_url_pattern. --- lib/ex_doc.ex | 35 ++++++++++++++++++----------- test/ex_doc/formatter/html_test.exs | 29 +++++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/lib/ex_doc.ex b/lib/ex_doc.ex index 3b1e96e9d..0791227f6 100644 --- a/lib/ex_doc.ex +++ b/lib/ex_doc.ex @@ -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 <> "/" diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index 33135f039..0eee25b8b 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -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