Skip to content

Commit 13e27d4

Browse files
committed
feat: add before_closing_body_tag map support
1 parent 80a9b66 commit 13e27d4

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

lib/ex_doc/config.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ defmodule ExDoc.Config do
5151
apps: [atom()],
5252
assets: nil | String.t(),
5353
authors: nil | [String.t()],
54-
before_closing_body_tag: (atom() -> String.t()) | mfa(),
55-
before_closing_head_tag: (atom() -> String.t()) | mfa(),
54+
before_closing_body_tag: (atom() -> String.t()) | mfa() | map(),
55+
before_closing_head_tag: (atom() -> String.t()) | mfa() | map(),
5656
canonical: nil | String.t(),
5757
cover: nil | Path.t(),
5858
deps: [{ebin_path :: String.t(), doc_url :: String.t()}],

lib/ex_doc/utils.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ defmodule ExDoc.Utils do
88
apply(m, f, [module | a])
99
end
1010

11+
def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module)
12+
when is_map(before_closing_head_tag) and is_map_key(before_closing_head_tag, module) do
13+
Map.fetch!(before_closing_head_tag, module)
14+
end
15+
1116
def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module) do
1217
before_closing_head_tag.(module)
1318
end
@@ -19,6 +24,11 @@ defmodule ExDoc.Utils do
1924
apply(m, f, [module | a])
2025
end
2126

27+
def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module)
28+
when is_map(before_closing_body_tag) and is_map_key(before_closing_body_tag, module) do
29+
Map.fetch!(before_closing_body_tag, module)
30+
end
31+
2232
def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module) do
2333
before_closing_body_tag.(module)
2434
end

test/ex_doc/formatter/epub_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ defmodule ExDoc.Formatter.EPUBTest do
193193

194194
test "before_closing_*_tags required by the user are in the right place using MFA",
195195
%{tmp_dir: tmp_dir} = context do
196+
generate_docs_and_unzip(
197+
context,
198+
doc_config(context,
199+
before_closing_head_tag: %{epub: "<meta name=StaticDemo>"},
200+
before_closing_body_tag: %{epub: "<p>StaticDemo</p>"}
201+
)
202+
)
203+
204+
oebps_dir = tmp_dir <> "/epub/OEBPS"
205+
206+
for basename <- @example_basenames do
207+
content = File.read!(Path.join(oebps_dir, basename))
208+
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
209+
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]
210+
end
211+
end
212+
213+
test "before_closing_*_tags required by the user are in the right place using a map",
214+
%{tmp_dir: tmp_dir} = context do
196215
generate_docs_and_unzip(
197216
context,
198217
doc_config(context,

test/ex_doc/formatter/html_test.exs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,28 @@ defmodule ExDoc.Formatter.HTMLTest do
547547
refute content_last =~ ~r{Next Page}
548548
end
549549

550-
test "before_closing_*_tags required by the user are placed in the right place",
550+
test "before_closing_*_tags required by the user are placed in the right place using a map",
551+
%{
552+
tmp_dir: tmp_dir
553+
} = context do
554+
generate_docs(
555+
doc_config(context,
556+
before_closing_head_tag: %{html: "<meta name=StaticDemo>"},
557+
before_closing_body_tag: %{html: "<p>StaticDemo</p>"},
558+
extras: ["test/fixtures/README.md"]
559+
)
560+
)
561+
562+
content = File.read!(tmp_dir <> "/html/api-reference.html")
563+
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
564+
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]
565+
566+
content = File.read!(tmp_dir <> "/html/readme.html")
567+
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
568+
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]
569+
end
570+
571+
test "before_closing_*_tags required by the user are placed in the right place using MFA",
551572
%{
552573
tmp_dir: tmp_dir
553574
} = context do
@@ -568,7 +589,7 @@ defmodule ExDoc.Formatter.HTMLTest do
568589
assert content =~ ~r[<p>Demo</p>\s*</body>]
569590
end
570591

571-
test "before_closing_*_tags required by the user are placed in the right place using MFA",
592+
test "before_closing_*_tags required by the user are placed in the right place",
572593
%{
573594
tmp_dir: tmp_dir
574595
} = context do

0 commit comments

Comments
 (0)