Skip to content

Commit

Permalink
feat: add before_closing_body_tag map support (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis authored Mar 7, 2023
1 parent 80a9b66 commit a07443e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 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
10 changes: 10 additions & 0 deletions lib/ex_doc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ 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
Map.get(before_closing_head_tag, module, "")
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 +24,11 @@ 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
Map.get(before_closing_body_tag, module, "")
end

def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module) do
before_closing_body_tag.(module)
end
Expand Down
21 changes: 20 additions & 1 deletion test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,26 @@ defmodule ExDoc.Formatter.EPUBTest do
end
end

test "before_closing_*_tags required by the user are in the right place using MFA",
test "before_closing_*_tags required by the user are in the right place using map",
%{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 MFA",
%{tmp_dir: tmp_dir} = context do
generate_docs_and_unzip(
context,
Expand Down
25 changes: 23 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,28 @@ 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_*_tags required by the user are placed in the right place using MFA",
%{
tmp_dir: tmp_dir
} = context do
Expand All @@ -568,7 +589,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 a07443e

Please # to comment.