diff --git a/README.md b/README.md index f5e565bdd..ffd4d83dc 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/ex_doc/config.ex b/lib/ex_doc/config.ex index 996356cc9..f76714f97 100644 --- a/lib/ex_doc/config.ex +++ b/lib/ex_doc/config.ex @@ -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()}], diff --git a/lib/ex_doc/utils.ex b/lib/ex_doc/utils.ex index 30e25ba99..beb77dcc5 100644 --- a/lib/ex_doc/utils.ex +++ b/lib/ex_doc/utils.ex @@ -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 @@ -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 diff --git a/test/ex_doc/formatter/epub_test.exs b/test/ex_doc/formatter/epub_test.exs index 02c131b85..c1f02225c 100644 --- a/test/ex_doc/formatter/epub_test.exs +++ b/test/ex_doc/formatter/epub_test.exs @@ -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: ""}, + before_closing_body_tag: %{epub: "

StaticDemo

"} + ) + ) + + oebps_dir = tmp_dir <> "/epub/OEBPS" + + for basename <- @example_basenames do + content = File.read!(Path.join(oebps_dir, basename)) + assert content =~ ~r[\s*] + assert content =~ ~r[

StaticDemo

\s*] + 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, diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index 6b90c6abf..0ef3f1cda 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -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: ""}, + before_closing_body_tag: %{html: "

StaticDemo

"}, + extras: ["test/fixtures/README.md"] + ) + ) + + content = File.read!(tmp_dir <> "/html/api-reference.html") + assert content =~ ~r[\s*] + assert content =~ ~r[

StaticDemo

\s*] + + content = File.read!(tmp_dir <> "/html/readme.html") + assert content =~ ~r[\s*] + assert content =~ ~r[

StaticDemo

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

Demo

\s*] 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