Skip to content

Commit

Permalink
Replace OTP /man/ links with /apps/:app/ (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga authored May 30, 2024
1 parent 2f23a05 commit d1b0950
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 78 deletions.
34 changes: 14 additions & 20 deletions lib/ex_doc/autolink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,21 @@ defmodule ExDoc.Autolink do
]

@hexdocs "https://hexdocs.pm/"
@otpdocs "https://www.erlang.org/doc/man/"
@otpappdocs "https://www.erlang.org/doc/apps/"

def app_module_url(tool, module, anchor \\ nil, config)
def app_module_url(tool, module, anchor \\ "#content", config)

def app_module_url(:ex_doc, module, nil, %{current_module: module} = config) do
app_module_url(:ex_doc, module, "#content", config)
end
def app_module_url(:no_tool, _, _, _), do: nil

def app_module_url(:ex_doc, module, anchor, %{current_module: module} = config) do
path = module |> inspect() |> String.trim_leading(":")
ex_doc_app_url(module, config, path, config.ext, "#{anchor}")
end
def app_module_url(tool, module, anchor, config) do
base_url =
case tool do
:ex_doc -> @hexdocs
:otp -> @otpappdocs
end

def app_module_url(:ex_doc, module, anchor, config) do
path = module |> inspect() |> String.trim_leading(":")
ex_doc_app_url(module, config, path, config.ext, "#{anchor}")
end

def app_module_url(:otp, module, anchor, _config) do
@otpdocs <> "#{module}.html#{anchor}"
end

def app_module_url(:no_tool, _, _, _) do
nil
app_url(base_url, module, config, path, config.ext, "#{anchor}")
end

defp string_app_module_url(string, tool, module, anchor, config) do
Expand All @@ -109,12 +99,16 @@ defmodule ExDoc.Autolink do

@doc false
def ex_doc_app_url(module, config, path, ext, suffix) do
app_url(@hexdocs, module, config, path, ext, suffix)
end

defp app_url(base_url, module, config, path, ext, suffix) do
if app = app(module) do
if app in config.apps do
path <> ext <> suffix
else
config.deps
|> Keyword.get_lazy(app, fn -> @hexdocs <> "#{app}" end)
|> Keyword.get_lazy(app, fn -> base_url <> "#{app}" end)
|> String.trim_trailing("/")
|> Kernel.<>("/" <> path <> ".html" <> suffix)
end
Expand Down
6 changes: 3 additions & 3 deletions test/ex_doc/formatter/html/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ defmodule ExDoc.Formatter.HTML.ErlangTest do
html = Floki.raw_html(doc)

assert html =~
~s|-spec</span> foo(<a href=\"#t:t/0\">t</a>()) -&gt; <a href=\"#t:t/0\">t</a>().|
~s|-spec</span> foo(<a href="#t:t/0">t</a>()) -&gt; <a href="#t:t/0">t</a>().|

assert html =~
~s|-type</span> t() :: <a href=\"https://www.erlang.org/doc/man/erlang.html#t:atom/0\">atom</a>().|
~s|-type</span> t() :: <a href="https://www.erlang.org/doc/apps/erts/erlang.html#t:atom/0">atom</a>().|

assert html =~
~s|-type</span> t2() :: #rec{k1 :: <a href=\"https://www.erlang.org/doc/man/uri_string.html#t:uri_string/0\">uri_string:uri_string</a>(), k2 :: <a href=\"https://www.erlang.org/doc/man/uri_string.html#t:uri_string/0\">uri_string:uri_string</a>() \| undefined}.|
~s|-type</span> t2() :: #rec{k1 :: <a href="https://www.erlang.org/doc/apps/stdlib/uri_string.html#t:uri_string/0">uri_string:uri_string</a>(), k2 :: <a href="https://www.erlang.org/doc/apps/stdlib/uri_string.html#t:uri_string/0">uri_string:uri_string</a>() \| undefined}.|
end

defp generate_docs(c) do
Expand Down
16 changes: 8 additions & 8 deletions test/ex_doc/language/elixir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ defmodule ExDoc.Language.ElixirTest do

test "m:module with Erlang module" do
assert autolink_doc("`m::array`") ==
~s|<a href="https://www.erlang.org/doc/man/array.html"><code class="inline">:array</code></a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/array.html"><code class="inline">:array</code></a>|
end

test "m:module with Erlang module and fragment" do
assert autolink_doc("`m::array#fragment`") ==
~s|<a href="https://www.erlang.org/doc/man/array.html#fragment"><code class="inline">:array</code></a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/array.html#fragment"><code class="inline">:array</code></a>|
end

test "module with fragment without m: does not link" do
Expand Down Expand Up @@ -99,7 +99,7 @@ defmodule ExDoc.Language.ElixirTest do

test "erlang stdlib function" do
assert autolink_doc("`:lists.all/2`") ==
~s|<a href="https://www.erlang.org/doc/man/lists.html#all/2"><code class="inline">:lists.all/2</code></a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/lists.html#all/2"><code class="inline">:lists.all/2</code></a>|
end

test "local function" do
Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule ExDoc.Language.ElixirTest do

test "erlang callback" do
assert autolink_doc("`c::gen_server.handle_call/3`") ==
~s|<a href="https://www.erlang.org/doc/man/gen_server.html#c:handle_call/3"><code class="inline">:gen_server.handle_call/3</code></a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3"><code class="inline">:gen_server.handle_call/3</code></a>|
end

test "elixir type" do
Expand All @@ -171,7 +171,7 @@ defmodule ExDoc.Language.ElixirTest do

test "erlang type" do
assert autolink_doc("`t::array.array/0`") ==
~s|<a href="https://www.erlang.org/doc/man/array.html#t:array/0"><code class="inline">:array.array/0</code></a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/array.html#t:array/0"><code class="inline">:array.array/0</code></a>|
end

test "special forms" do
Expand Down Expand Up @@ -204,10 +204,10 @@ defmodule ExDoc.Language.ElixirTest do
~s|<a href="https://hexdocs.pm/elixir/String.html#at/2">custom text</a>|

assert autolink_doc("[custom text](`:lists`)") ==
~s|<a href="https://www.erlang.org/doc/man/lists.html">custom text</a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/lists.html">custom text</a>|

assert autolink_doc("[custom text](`:lists.all/2`)") ==
~s|<a href="https://www.erlang.org/doc/man/lists.html#all/2">custom text</a>|
~s|<a href="https://www.erlang.org/doc/apps/stdlib/lists.html#all/2">custom text</a>|
end

test "mix task" do
Expand Down Expand Up @@ -450,7 +450,7 @@ defmodule ExDoc.Language.ElixirTest do

test "Erlang stdlib types" do
assert autolink_spec(quote(do: t() :: :sets.set())) ==
~s[t() :: <a href="https://www.erlang.org/doc/man/sets.html#t:set/0">:sets.set</a>()]
~s[t() :: <a href="https://www.erlang.org/doc/apps/stdlib/sets.html#t:set/0">:sets.set</a>()]
end

test "escape special HTML characters" do
Expand Down
Loading

0 comments on commit d1b0950

Please sign in to comment.