Skip to content

Commit

Permalink
feat: add before_closing_body_tag map support
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Mar 6, 2023
1 parent 80a9b66 commit 804327a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ docs: [
]
```

Or you can pass a map where the key is the format:

```elixir
docs: [
# ...
before_closing_head_tag: %{html: "", epub: ""},
before_closing_body_tag: %{html: "", epub: ""}
]
```

### Rendering Math

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:
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_doc/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ defmodule ExDoc.Config do
apps: [atom()],
assets: nil | String.t(),
authors: nil | [String.t()],
before_closing_body_tag: (atom() -> String.t()) | mfa(),
before_closing_head_tag: (atom() -> String.t()) | mfa(),
before_closing_body_tag: (atom() -> String.t()) | mfa() | map(),
before_closing_head_tag: (atom() -> String.t()) | mfa() | map(),
canonical: nil | String.t(),
cover: nil | Path.t(),
deps: [{ebin_path :: String.t(), doc_url :: String.t()}],
Expand Down
24 changes: 24 additions & 0 deletions lib/ex_doc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ defmodule ExDoc.Utils do
apply(m, f, [module | a])
end

def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module)
when is_map(before_closing_head_tag) do
case Map.get(before_closing_head_tag, module) do
nil ->
raise ArgumentError,
"#{inspect(module)} is not defined in before_closing_head_tag configuration"

value ->
value
end
end

def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module) do
before_closing_head_tag.(module)
end
Expand All @@ -19,6 +31,18 @@ defmodule ExDoc.Utils do
apply(m, f, [module | a])
end

def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module)
when is_map(before_closing_body_tag) do
case Map.get(before_closing_body_tag, module) do
nil ->
raise ArgumentError,
"#{inspect(module)} is not defined in before_closing_body_tag configuration"

value ->
value
end
end

def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module) do
before_closing_body_tag.(module)
end
Expand Down
40 changes: 40 additions & 0 deletions test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ defmodule ExDoc.Formatter.EPUBTest do

test "before_closing_*_tags required by the user are in the right place using MFA",
%{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(
context,
doc_config(context,
before_closing_head_tag: %{epub: "<meta name=StaticDemo>"},
before_closing_body_tag: %{epub: "<p>StaticDemo</p>"}
)
)

oebps_dir = tmp_dir <> "/epub/OEBPS"

for basename <- @example_basenames do
content = File.read!(Path.join(oebps_dir, basename))
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]
end
end

test "before_closing_*_tags required by the user are in the right place using a map",
%{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(
context,
doc_config(context,
Expand All @@ -210,6 +229,27 @@ defmodule ExDoc.Formatter.EPUBTest do
end
end

test "before_closing_*_tag required by the user are placed in the right place using a map with the required key",
context do
assert_raise ArgumentError,
~S(:epub is not defined in before_closing_head_tag configuration),
fn ->
generate_docs(
doc_config(context,
before_closing_head_tag: %{html: "<meta name=StaticDemo>"}
)
)
end

assert_raise ArgumentError,
~S(:epub is not defined in before_closing_body_tag configuration),
fn ->
generate_docs(
doc_config(context, before_closing_body_tag: %{html: "<p>StaticDemo</p>"})
)
end
end

test "assets required by the user end up in the right place", %{tmp_dir: tmp_dir} = context do
File.mkdir_p!("test/tmp/epub_assets/hello")
File.touch!("test/tmp/epub_assets/hello/world.png")
Expand Down
46 changes: 44 additions & 2 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,49 @@ defmodule ExDoc.Formatter.HTMLTest do
refute content_last =~ ~r{Next Page}
end

test "before_closing_*_tags required by the user are placed in the right place",
test "before_closing_*_tags required by the user are placed in the right place using a map",
%{
tmp_dir: tmp_dir
} = context do
generate_docs(
doc_config(context,
before_closing_head_tag: %{html: "<meta name=StaticDemo>"},
before_closing_body_tag: %{html: "<p>StaticDemo</p>"},
extras: ["test/fixtures/README.md"]
)
)

content = File.read!(tmp_dir <> "/html/api-reference.html")
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]

content = File.read!(tmp_dir <> "/html/readme.html")
assert content =~ ~r[<meta name=StaticDemo>\s*</head>]
assert content =~ ~r[<p>StaticDemo</p>\s*</body>]
end

test "before_closing_*_tag required by the user are placed in the right place using a map with the required key",
context do
assert_raise ArgumentError,
~S(:html is not defined in before_closing_head_tag configuration),
fn ->
generate_docs(
doc_config(context,
before_closing_head_tag: %{epub: "<meta name=StaticDemo>"}
)
)
end

assert_raise ArgumentError,
~S(:html is not defined in before_closing_body_tag configuration),
fn ->
generate_docs(
doc_config(context, before_closing_body_tag: %{epub: "<p>StaticDemo</p>"})
)
end
end

test "before_closing_*_tags required by the user are placed in the right place using MFA",
%{
tmp_dir: tmp_dir
} = context do
Expand All @@ -568,7 +610,7 @@ defmodule ExDoc.Formatter.HTMLTest do
assert content =~ ~r[<p>Demo</p>\s*</body>]
end

test "before_closing_*_tags required by the user are placed in the right place using MFA",
test "before_closing_*_tags required by the user are placed in the right place",
%{
tmp_dir: tmp_dir
} = context do
Expand Down

0 comments on commit 804327a

Please sign in to comment.