Skip to content

Commit

Permalink
Handle OTP links when generating OTP docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Aug 27, 2021
1 parent 6a0276d commit 24c03d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
12 changes: 8 additions & 4 deletions lib/ex_doc/autolink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ defmodule ExDoc.Autolink do
end

@doc false
def tool(module) do
def tool(module, config) do
if match?("Elixir." <> _, Atom.to_string(module)) do
:ex_doc
else
case app_info(module) do
{true, _} -> :otp
{false, _} -> :ex_doc
{otp, app} = app_info(module)
apps = Enum.uniq(config.apps ++ Keyword.keys(config.deps))

if otp == true and app not in apps do
:otp
else
:ex_doc
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ex_doc/language/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ defmodule ExDoc.Language.Elixir do

case {mode, Refs.get_visibility(ref)} do
{_link_type, :public} ->
Autolink.app_module_url(Autolink.tool(module), module, config)
Autolink.app_module_url(Autolink.tool(module, config), module, config)

{:regular_link, :undefined} ->
nil
Expand Down Expand Up @@ -798,7 +798,7 @@ defmodule ExDoc.Language.Elixir do

case {kind, visibility} do
{_kind, :public} ->
fragment(Autolink.tool(module), kind, name, arity)
fragment(Autolink.tool(module, config), kind, name, arity)

{:function, _visibility} ->
try_autoimported_function(name, arity, mode, config, original_text)
Expand Down Expand Up @@ -829,7 +829,7 @@ defmodule ExDoc.Language.Elixir do

case {mode, Refs.get_visibility({:module, module}), Refs.get_visibility(ref)} do
{_mode, _module_visibility, :public} ->
case Autolink.tool(module) do
case Autolink.tool(module, config) do
:no_tool ->
nil

Expand Down
16 changes: 11 additions & 5 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ defmodule ExDoc.Language.Erlang do
end

defp final_url({:module, module}, config) do
tool = Autolink.tool(module)
tool = Autolink.tool(module, config)
Autolink.app_module_url(tool, module, config)
end

Expand All @@ -362,9 +362,11 @@ defmodule ExDoc.Language.Erlang do
end

defp final_url({kind, module, name, arity}, config) do
tool = Autolink.tool(module)
tool = Autolink.tool(module, config)
module_url = Autolink.app_module_url(tool, module, config)
module_url && module_url <> fragment(tool, kind, name, arity)
# TODO: fix me
module_url = String.trim_trailing(module_url, "#content")
module_url <> fragment(tool, kind, name, arity)
end

defp fragment(:otp, :function, name, arity) do
Expand All @@ -379,11 +381,15 @@ defmodule ExDoc.Language.Erlang do
"#type-#{name}"
end

defp fragment(_, :function, name, arity) do
defp fragment(:ex_doc, :function, name, arity) do
"##{name}/#{arity}"
end

defp fragment(_, :type, name, arity) do
defp fragment(:ex_doc, :callback, name, arity) do
"#c:#{name}/#{arity}"
end

defp fragment(:ex_doc, :type, name, arity) do
"#t:#{name}/#{arity}"
end

Expand Down
17 changes: 17 additions & 0 deletions test/ex_doc/language/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ defmodule ExDoc.Language.ErlangTest do
~s|<a href="https://erlang.org/doc/man/array.html"><code>array</code></a>|
end

test "OTP module when generating OTP docs", c do
assert autolink_doc("{@link array}", [deps: [stdlib: "https://example.com/stdlib"]], c) ==
~s|<a href="https://example.com/stdlib/array.html"><code>array</code></a>|
end

test "app module", c do
assert autolink_doc("{@link //stdlib/array}", c) ==
~s|<a href="https://erlang.org/doc/man/array.html"><code>array</code></a>|
Expand Down Expand Up @@ -73,6 +78,16 @@ defmodule ExDoc.Language.ErlangTest do
~s|<a href="https://erlang.org/doc/man/array.html#new-0"><code>array:new/0</code></a>|
end

test "OTP function when generating OTP docs", c do
assert autolink_doc("{@link array:new/0}", [apps: [:stdlib]], c) ==
~s|<a href="array.html#new/0"><code>array:new/0</code></a>|
end

test "OTP function when generating OTP docs, same module", c do
assert autolink_doc("{@link array:new/0}", [current_module: :array, apps: [:stdlib]], c) ==
~s|<a href="array.html#new/0"><code>array:new/0</code></a>|
end

test "ERTS function", c do
assert autolink_doc("{@link zlib:gunzip/1}", c) ==
~s|<a href="https://erlang.org/doc/man/zlib.html#gunzip-1"><code>zlib:gunzip/1</code></a>|
Expand All @@ -83,6 +98,8 @@ defmodule ExDoc.Language.ErlangTest do
~s|<a href="https://erlang.org/doc/man/array.html#new-0"><code>array:new/0</code></a>|
end

# TODO: test callbacks. No support in EDoc, use :docgen_xml_to_chunks.

test "external function", c do
assert autolink_doc("{@link 'Elixir.EarmarkParser':as_ast/2}", c) ==
~s|<a href="https://hexdocs.pm/earmark_parser/EarmarkParser.html#as_ast/2"><code>'Elixir.EarmarkParser':as_ast/2</code></a>|
Expand Down

0 comments on commit 24c03d0

Please sign in to comment.