Skip to content

Commit a62675e

Browse files
authored
Add before_closing_body_tag mfa support (#1670)
1 parent 1e73873 commit a62675e

14 files changed

Lines changed: 120 additions & 11 deletions

File tree

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,57 @@ ExDoc renders Markdown content for you, but you can extend it to render complex
215215
```elixir
216216
docs: [
217217
# ...
218+
before_closing_head_tag: &before_closing_head_tag/1,
218219
before_closing_body_tag: &before_closing_body_tag/1
219220
]
220221

221222
# ...
222223

224+
defp before_closing_head_tag(:html) do
225+
"""
226+
<!-- HTML injected at the end of the <head> element -->
227+
"""
228+
end
229+
230+
defp before_closing_head_tag(:epub), do: ""
231+
223232
defp before_closing_body_tag(:html) do
224233
"""
225234
<!-- HTML injected at the end of the <body> element -->
226235
"""
227236
end
228237

229-
defp before_closing_body_tag(_), do: ""
238+
defp before_closing_body_tag(:epub), do: ""
239+
```
240+
241+
You could use `MFA` as well:
242+
243+
```elixir
244+
docs: [
245+
# ...
246+
before_closing_head_tag: {MyModule, :before_closing_head_tag, ["Demo"]},
247+
before_closing_body_tag: {MyModule, :before_closing_body_tag, ["Demo"]}
248+
]
249+
250+
# ...
251+
252+
defmodule MyModule do
253+
def before_closing_head_tag(:html, name) do
254+
# ...
255+
end
256+
257+
def before_closing_head_tag(:epub, name) do
258+
# ...
259+
end
260+
261+
def before_closing_body_tag(:html, name) do
262+
# ...
263+
end
264+
265+
def before_closing_body_tag(:html, name) do
266+
# ...
267+
end
268+
end
230269
```
231270

232271
### Rendering Math

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()),
55-
before_closing_head_tag: (atom() -> String.t()),
54+
before_closing_body_tag: (atom() -> String.t()) | mfa(),
55+
before_closing_head_tag: (atom() -> String.t()) | mfa(),
5656
canonical: nil | String.t(),
5757
cover: nil | Path.t(),
5858
deps: [{ebin_path :: String.t(), doc_url :: String.t()}],

lib/ex_doc/formatter/epub/templates.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule ExDoc.Formatter.EPUB.Templates do
33

44
require EEx
55

6+
import ExDoc.Utils, only: [before_closing_body_tag: 2, before_closing_head_tag: 2]
7+
68
alias ExDoc.Formatter.HTML
79
alias ExDoc.Formatter.HTML.Templates, as: H
810

lib/ex_doc/formatter/epub/templates/extra_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
</h1>
55
<%= content %>
66
<%# Extra content specified by the user (e.g. custom Javascript) %>
7-
<%= config.before_closing_body_tag.(:epub) %>
7+
<%= before_closing_body_tag(config, :epub) %>
88
</body>
99
</html>

lib/ex_doc/formatter/epub/templates/head_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
href="<%= H.asset_rev "#{config.output}/OEBPS", "dist/epub-#{config.proglang}-*.css" %>" />
1010
<script src="<%= H.asset_rev "#{config.output}/OEBPS", "dist/epub-*.js" %>"></script>
1111
<%# Extra content specified by the user (e.g. custom CSS) %>
12-
<%= config.before_closing_head_tag.(:epub) %>
12+
<%= before_closing_head_tag(config, :epub) %>
1313
</head>
1414
<body class="content-inner">

lib/ex_doc/formatter/epub/templates/module_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
</section>
4444
<% end %>
4545
<%# Extra content specified by the user (e.g. custom Javascript) %>
46-
<%= config.before_closing_body_tag.(:epub) %>
46+
<%= before_closing_body_tag(config, :epub) %>
4747
</body>
4848
</html>

lib/ex_doc/formatter/epub/templates/nav_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
</ol>
2222
</nav>
2323
<%# Extra content specified by the user (e.g. custom Javascript) %>
24-
<%= config.before_closing_body_tag.(:epub) %>
24+
<%= before_closing_body_tag(config, :epub) %>
2525
</body>
2626
</html>

lib/ex_doc/formatter/epub/templates/title_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<% end %>
1212
</div>
1313
<%# Extra content specified by the user (e.g. custom Javascript) %>
14-
<%= config.before_closing_body_tag.(:epub) %>
14+
<%= before_closing_body_tag(config, :epub) %>
1515
</body>
1616
</html>

lib/ex_doc/formatter/html/templates.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ExDoc.Formatter.HTML.Templates do
22
@moduledoc false
33
require EEx
44

5-
import ExDoc.Utils, only: [h: 1]
5+
import ExDoc.Utils, only: [h: 1, before_closing_body_tag: 2, before_closing_head_tag: 2]
66

77
# TODO: It should not depend on the parent module. Move required HTML functions to Utils.
88
# TODO: Add tests that assert on the returned structured, not on JSON

lib/ex_doc/formatter/html/templates/footer_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
</section>
4141
</div>
4242
<%# Extra content specified by the user (e.g. custom Javascript) %>
43-
<%= config.before_closing_body_tag.(:html) %>
43+
<%= before_closing_body_tag(config, :html) %>
4444
</body>
4545
</html>

0 commit comments

Comments
 (0)