Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/ex_doc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ defmodule ExDoc do
Map.fetch!(@default, field)
end

def before_closing_head_tag(_), do: ""
def before_closing_body_tag(_), do: ""

defstruct [
assets: nil,
before_closing_head_tag: "",
before_closing_body_tag: "",
before_closing_head_tag: &__MODULE__.before_closing_head_tag/1,
before_closing_body_tag: &__MODULE__.before_closing_body_tag/1,
canonical: nil,
debug: false,
deps: [],
Expand All @@ -53,8 +56,8 @@ defmodule ExDoc do

@type t :: %__MODULE__{
assets: nil | String.t,
before_closing_head_tag: String.t,
before_closing_body_tag: String.t,
before_closing_head_tag: (atom() -> String.t),
before_closing_body_tag: (atom() -> String.t),
canonical: nil | String.t,
debug: boolean(),
deps: [{ebin_path :: String.t, doc_url :: String.t}],
Expand Down
1 change: 1 addition & 0 deletions lib/ex_doc/formatter/epub/templates/extra_template.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= head_template(config, %{title: title}) %>
<%= content %>
<%= config.before_closing_body_tag.(:epub) %>
</body>
</html>
1 change: 1 addition & 0 deletions lib/ex_doc/formatter/epub/templates/head_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<meta name="generator" content="ExDoc v<%= ExDoc.version %>" />
<link type="text/css" rel="stylesheet" href="<%= H.asset_rev "#{config.output}/OEBPS", "dist/epub*.css" %>" />
<script src="<%= H.asset_rev "#{config.output}/OEBPS", "dist/app*.js" %>"></script>
<%= config.before_closing_head_tag.(:epub) %>
</head>
<body>
1 change: 1 addition & 0 deletions lib/ex_doc/formatter/epub/templates/module_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@
end %>
</section>
<% end %>
<%= config.before_closing_body_tag.(:epub) %>
</body>
</html>
1 change: 1 addition & 0 deletions lib/ex_doc/formatter/epub/templates/nav_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<%= nav_item_template "Mix Tasks", nodes.tasks %>
</ol>
</nav>
<%= config.before_closing_body_tag.(:epub) %>
</body>
</html>
1 change: 1 addition & 0 deletions lib/ex_doc/formatter/epub/templates/title_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<div><img src="assets/logo<%= Path.extname(logo) %>" alt="Logo"/></div>
<% end %>
</div>
<%= config.before_closing_body_tag.(:epub) %>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/html/templates/footer_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</section>
</div>
<script src="<%= asset_rev config.output, "dist/app*.js" %>"></script>
<%= config.before_closing_body_tag %>
<%= config.before_closing_body_tag.(:html) %>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/html/templates/head_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link rel="canonical" href="<%= config.canonical %>" />
<% end %>
<script src="<%= asset_rev config.output, "dist/sidebar_items*.js" %>"></script>
<%= config.before_closing_head_tag %>
<%= config.before_closing_head_tag.(:html) %>
</head>
<body data-type="<%= sidebar_type(page.type) %>">
<script>try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { }</script>
18 changes: 11 additions & 7 deletions lib/mix/tasks/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ defmodule Mix.Tasks.Docs do
directory in the output path. Its entries may be referenced in your docs
under "assets/ASSET.EXTENSION"; defaults to no assets directory.

* `:before_closing_body_tag` - Literal HTML to be included just before the closing body tag (`</body>`)
Useful to inject custom assets, such as Javascript.
Only works with the HTML formatter.

* `:before_closing_head_tag` - Literal HTML to be included just before the closing head tag (`</head>`);
Useful to inject custom assets, such as CSS.
Only works with the HTML formatter.
* `:before_closing_body_tag` - a function that takes as argument an atom specifying
the formatter being used (`:html` or `:epub`) and returns a literal HTML string
to be included just before the closing body tag (`</body>`).
The atom given as argument can be used to include different content in both formats.
Useful to inject custom assets, such as Javascript.

* `:before_closing_head_tag` - a function that takes as argument an atom specifying
the formatter being used (`:html` or `:epub`) and returns a literal HTML string
to be included just before the closing head tag (`</head>`).
The atom given as argument can be used to include different content in both formats.
Useful to inject custom assets, such as CSS stylesheets.

* `:canonical` - String that defines the preferred URL with the rel="canonical"
element; defaults to no canonical path.
Expand Down
47 changes: 46 additions & 1 deletion test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@ defmodule ExDoc.Formatter.EPUBTest do
Path.expand("../../tmp/beam", __DIR__)
end

@before_closing_head_tag_content_html "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-HTML</dont-escape>"
@before_closing_body_tag_content_html "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-HTML</dont-escape>"
@before_closing_head_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-EPUB</dont-escape>"
@before_closing_body_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-EPUB</dont-escape>"

defp before_closing_head_tag(:html), do: @before_closing_head_tag_content_html
defp before_closing_head_tag(:epub), do: @before_closing_head_tag_content_epub

defp before_closing_body_tag(:html), do: @before_closing_body_tag_content_html
defp before_closing_body_tag(:epub), do: @before_closing_body_tag_content_epub

defp doc_config do
[project: "Elixir",
version: "1.0.1",
formatter: "epub",
output: output_dir(),
source_root: beam_dir(),
source_beam: beam_dir(),
extras: ["test/fixtures/README.md"]]
extras: ["test/fixtures/README.md"],
before_closing_head_tag: &before_closing_head_tag/1,
before_closing_body_tag: &before_closing_body_tag/1]
end

defp doc_config(config) do
Expand Down Expand Up @@ -150,4 +163,36 @@ defmodule ExDoc.Formatter.EPUBTest do
content = File.read!("#{output_dir()}/OEBPS/nav.xhtml")
refute content =~ ~r{<li><a href="README.html">README</a></li>}
end

test "before_closing_*_tags are in the right place" do
generate_docs_and_unzip(doc_config())

oebps_dir = "#{output_dir()}/OEBPS"

# "structural pages"
content = File.read!("#{oebps_dir}/nav.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]

content = File.read!("#{oebps_dir}/title.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]

content = File.read!("#{oebps_dir}/readme.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]

content = File.read!("#{oebps_dir}/CompiledWithDocs.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]

content = File.read!("#{oebps_dir}/CompiledWithDocs.Nested.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]

# Example of a "module page"
content = File.read!("#{oebps_dir}/MultipleSpecs.xhtml")
assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*</body>]
end
end
24 changes: 16 additions & 8 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ defmodule ExDoc.Formatter.HTMLTest do
File.read!(file)
end

@before_closing_head_tag "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG</dont-escape>"
@before_closing_body_tag "UNIQUE:<dont-escape>&copyBEFORE-CLOSING-BODY-TAG</dont-escape>"
@before_closing_head_tag_content_html "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-HTML</dont-escape>"
@before_closing_body_tag_content_html "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-HTML</dont-escape>"
@before_closing_head_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-EPUB</dont-escape>"
@before_closing_body_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-EPUB</dont-escape>"

defp before_closing_head_tag(:html), do: @before_closing_head_tag_content_html
defp before_closing_head_tag(:epub), do: @before_closing_head_tag_content_epub

defp before_closing_body_tag(:html), do: @before_closing_body_tag_content_html
defp before_closing_body_tag(:epub), do: @before_closing_body_tag_content_epub

defp doc_config do
[project: "Elixir",
Expand All @@ -33,8 +41,8 @@ defmodule ExDoc.Formatter.HTMLTest do
source_beam: beam_dir(),
logo: "test/fixtures/elixir.png",
extras: ["test/fixtures/README.md"],
before_closing_head_tag: @before_closing_head_tag,
before_closing_body_tag: @before_closing_body_tag]
before_closing_head_tag: &before_closing_head_tag/1,
before_closing_body_tag: &before_closing_body_tag/1]
end

defp doc_config(config) do
Expand Down Expand Up @@ -213,17 +221,17 @@ defmodule ExDoc.Formatter.HTMLTest do
generate_docs(doc_config())

content = File.read!("#{output_dir()}/api-reference.html")
assert content =~ ~r[#{@before_closing_head_tag}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag}\s*</body>]
assert content =~ ~r[#{@before_closing_head_tag_content_html}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_html}\s*</body>]
end

test "before_closing_*_tags are placed in the right place - generated pages" do
config = doc_config([main: "readme"])
generate_docs(config)

content = File.read!("#{output_dir()}/readme.html")
assert content =~ ~r[#{@before_closing_head_tag}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag}\s*</body>]
assert content =~ ~r[#{@before_closing_head_tag_content_html}\s*</head>]
assert content =~ ~r[#{@before_closing_body_tag_content_html}\s*</body>]
end

test "run generates pages with custom names" do
Expand Down