Skip to content

Commit d8e5fcb

Browse files
authored
support for source_url_pattern in config being a function (#1529)
1 parent f2a71ab commit d8e5fcb

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lib/ex_doc/formatter/html.ex

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ExDoc.Formatter.HTML do
22
@moduledoc false
33

44
alias __MODULE__.{Assets, Templates, SearchItems}
5-
alias ExDoc.{Markdown, GroupMatcher}
5+
alias ExDoc.{Markdown, GroupMatcher, Utils}
66

77
@main "api-reference"
88
@assets_dir "assets"
@@ -387,12 +387,7 @@ defmodule ExDoc.Formatter.HTML do
387387

388388
source_path = input |> Path.relative_to(File.cwd!()) |> String.replace_leading("./", "")
389389

390-
source_url =
391-
if url = source_url_pattern do
392-
url
393-
|> String.replace("%{path}", source_path)
394-
|> String.replace("%{line}", "1")
395-
end
390+
source_url = Utils.source_url_pattern(source_url_pattern, source_path, "1")
396391

397392
%{
398393
id: id,

lib/ex_doc/retriever.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule ExDoc.Retriever do
77
defexception [:message]
88
end
99

10-
alias ExDoc.{DocAST, GroupMatcher, Refs}
10+
alias ExDoc.{DocAST, GroupMatcher, Refs, Utils}
1111
alias ExDoc.Retriever.Error
1212

1313
@doc """
@@ -336,8 +336,7 @@ defmodule ExDoc.Retriever do
336336
defp source_link(%{path: _, url: nil}, _line), do: nil
337337

338338
defp source_link(source, line) do
339-
source_url = Regex.replace(~r/%{path}/, source.url, source.path)
340-
Regex.replace(~r/%{line}/, source_url, to_string(line))
339+
Utils.source_url_pattern(source.url, source.path, to_string(line))
341340
end
342341

343342
defp source_path(module, _config) do

lib/ex_doc/utils.ex

+12
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,16 @@ defmodule ExDoc.Utils do
9090
def to_json(integer) when is_integer(integer) do
9191
Integer.to_string(integer)
9292
end
93+
94+
def source_url_pattern(source_url_pattern, path, line) do
95+
if is_function(source_url_pattern) do
96+
source_url_pattern.(path, line)
97+
else
98+
if url = source_url_pattern do
99+
url
100+
|> String.replace("%{path}", path)
101+
|> String.replace("%{line}", line)
102+
end
103+
end
104+
end
93105
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule ExDoc.Retriever.SourcePatternTest do
2+
use ExUnit.Case, async: true
3+
alias ExDoc.Retriever
4+
import TestHelper
5+
6+
@moduletag :tmp_dir
7+
8+
def source_url_fn_1(path, line),
9+
do: "%{path}:%{line}" |> String.replace("%{path}", path) |> String.replace("%{line}", line)
10+
11+
def source_url_fn_2(path, line),
12+
do: "%{path}-%{line}" |> String.replace("%{path}", path) |> String.replace("%{line}", line)
13+
14+
describe "docs_from_modules/2" do
15+
test "callbacks with source_url_pattern as function", c do
16+
elixirc(c, ~S"""
17+
defmodule ModSource do
18+
@doc "Sample docs."
19+
@callback callback1() :: :ok
20+
end
21+
""")
22+
23+
config = %ExDoc.Config{source_url_pattern: &source_url_fn_1/2}
24+
[mod] = Retriever.docs_from_modules([ModSource], config)
25+
[callback1] = mod.docs
26+
assert Path.basename(callback1.source_url) == "nofile:3"
27+
28+
config = %ExDoc.Config{source_url_pattern: &source_url_fn_2/2}
29+
[mod] = Retriever.docs_from_modules([ModSource], config)
30+
[callback1] = mod.docs
31+
assert Path.basename(callback1.source_url) == "nofile-3"
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)