Skip to content

Commit a07443e

Browse files
authored
feat: add before_closing_body_tag map support (#1676)
1 parent 80a9b66 commit a07443e

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ docs: [
248248
]
249249
```
250250

251+
Or you can pass a map where the key is the format:
252+
253+
```elixir
254+
docs: [
255+
# ...
256+
before_closing_head_tag: %{html: "...", epub: "..."},
257+
before_closing_body_tag: %{html: "...", epub: "..."}
258+
]
259+
```
260+
251261
### Rendering Math
252262

253263
If you write TeX-style math in your Markdown, such as `$\sum_{i}^{N} x_i$`, it ends up as raw text on the generated pages. To render expressions, we recommend using [KaTeX](https://katex.org/), a JavaScript library that turns expressions into graphics. To load and trigger KaTeX on every documentation page, we can insert the following HTML:

lib/ex_doc/config.ex

+2-2
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

+10
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) do
13+
Map.get(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) do
29+
Map.get(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

+20-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,26 @@ defmodule ExDoc.Formatter.EPUBTest do
191191
end
192192
end
193193

194-
test "before_closing_*_tags required by the user are in the right place using MFA",
194+
test "before_closing_*_tags required by the user are in the right place using map",
195+
%{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 MFA",
195214
%{tmp_dir: tmp_dir} = context do
196215
generate_docs_and_unzip(
197216
context,

test/ex_doc/formatter/html_test.exs

+23-2
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)