diff --git a/lib/ex_doc.ex b/lib/ex_doc.ex index fbe26f60f..28b919b4d 100644 --- a/lib/ex_doc.ex +++ b/lib/ex_doc.ex @@ -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: [], @@ -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}], diff --git a/lib/ex_doc/formatter/epub/templates/extra_template.eex b/lib/ex_doc/formatter/epub/templates/extra_template.eex index 95b94fd41..e8ae6f3f6 100644 --- a/lib/ex_doc/formatter/epub/templates/extra_template.eex +++ b/lib/ex_doc/formatter/epub/templates/extra_template.eex @@ -1,4 +1,5 @@ <%= head_template(config, %{title: title}) %> <%= content %> + <%= config.before_closing_body_tag.(:epub) %> diff --git a/lib/ex_doc/formatter/epub/templates/head_template.eex b/lib/ex_doc/formatter/epub/templates/head_template.eex index 7ae5eb853..1198e8938 100644 --- a/lib/ex_doc/formatter/epub/templates/head_template.eex +++ b/lib/ex_doc/formatter/epub/templates/head_template.eex @@ -7,5 +7,6 @@ " /> + <%= config.before_closing_head_tag.(:epub) %> diff --git a/lib/ex_doc/formatter/epub/templates/module_template.eex b/lib/ex_doc/formatter/epub/templates/module_template.eex index 160337d49..3c6dd3116 100644 --- a/lib/ex_doc/formatter/epub/templates/module_template.eex +++ b/lib/ex_doc/formatter/epub/templates/module_template.eex @@ -70,5 +70,6 @@ end %> <% end %> + <%= config.before_closing_body_tag.(:epub) %> diff --git a/lib/ex_doc/formatter/epub/templates/nav_template.eex b/lib/ex_doc/formatter/epub/templates/nav_template.eex index 989ac5eb5..3e0ee6ec6 100644 --- a/lib/ex_doc/formatter/epub/templates/nav_template.eex +++ b/lib/ex_doc/formatter/epub/templates/nav_template.eex @@ -18,5 +18,6 @@ <%= nav_item_template "Mix Tasks", nodes.tasks %> + <%= config.before_closing_body_tag.(:epub) %> diff --git a/lib/ex_doc/formatter/epub/templates/title_template.eex b/lib/ex_doc/formatter/epub/templates/title_template.eex index cf8a33223..8200e9277 100644 --- a/lib/ex_doc/formatter/epub/templates/title_template.eex +++ b/lib/ex_doc/formatter/epub/templates/title_template.eex @@ -6,5 +6,6 @@
Logo
<% end %> + <%= config.before_closing_body_tag.(:epub) %> diff --git a/lib/ex_doc/formatter/html/templates/footer_template.eex b/lib/ex_doc/formatter/html/templates/footer_template.eex index 2bdffd672..6e9649bde 100644 --- a/lib/ex_doc/formatter/html/templates/footer_template.eex +++ b/lib/ex_doc/formatter/html/templates/footer_template.eex @@ -16,6 +16,6 @@ - <%= config.before_closing_body_tag %> + <%= config.before_closing_body_tag.(:html) %> diff --git a/lib/ex_doc/formatter/html/templates/head_template.eex b/lib/ex_doc/formatter/html/templates/head_template.eex index 3ec59ac7a..4b27f5d6d 100644 --- a/lib/ex_doc/formatter/html/templates/head_template.eex +++ b/lib/ex_doc/formatter/html/templates/head_template.eex @@ -11,7 +11,7 @@ <% end %> - <%= config.before_closing_head_tag %> + <%= config.before_closing_head_tag.(:html) %> diff --git a/lib/mix/tasks/docs.ex b/lib/mix/tasks/docs.ex index 24300fd34..5d8188d42 100644 --- a/lib/mix/tasks/docs.ex +++ b/lib/mix/tasks/docs.ex @@ -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 (``) - 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 (``); - 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 (``). + 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 (``). + 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. diff --git a/test/ex_doc/formatter/epub_test.exs b/test/ex_doc/formatter/epub_test.exs index e904a8749..925e7169b 100644 --- a/test/ex_doc/formatter/epub_test.exs +++ b/test/ex_doc/formatter/epub_test.exs @@ -14,6 +14,17 @@ defmodule ExDoc.Formatter.EPUBTest do Path.expand("../../tmp/beam", __DIR__) end + @before_closing_head_tag_content_html "UNIQUE:©BEFORE-CLOSING-HEAD-TAG-HTML" + @before_closing_body_tag_content_html "UNIQUE:©BEFORE-CLOSING-BODY-TAG-HTML" + @before_closing_head_tag_content_epub "UNIQUE:©BEFORE-CLOSING-HEAD-TAG-EPUB" + @before_closing_body_tag_content_epub "UNIQUE:©BEFORE-CLOSING-BODY-TAG-EPUB" + + 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", @@ -21,7 +32,9 @@ defmodule ExDoc.Formatter.EPUBTest do 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 @@ -150,4 +163,36 @@ defmodule ExDoc.Formatter.EPUBTest do content = File.read!("#{output_dir()}/OEBPS/nav.xhtml") refute content =~ ~r{
  • README
  • } 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*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + + content = File.read!("#{oebps_dir}/title.xhtml") + assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + + content = File.read!("#{oebps_dir}/readme.xhtml") + assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + + content = File.read!("#{oebps_dir}/CompiledWithDocs.xhtml") + assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + + content = File.read!("#{oebps_dir}/CompiledWithDocs.Nested.xhtml") + assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + + # Example of a "module page" + content = File.read!("#{oebps_dir}/MultipleSpecs.xhtml") + assert content =~ ~r[#{@before_closing_head_tag_content_epub}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_epub}\s*] + end end diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index cda64549b..585e38913 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -20,8 +20,16 @@ defmodule ExDoc.Formatter.HTMLTest do File.read!(file) end - @before_closing_head_tag "UNIQUE:©BEFORE-CLOSING-HEAD-TAG" - @before_closing_body_tag "UNIQUE:©BEFORE-CLOSING-BODY-TAG" + @before_closing_head_tag_content_html "UNIQUE:©BEFORE-CLOSING-HEAD-TAG-HTML" + @before_closing_body_tag_content_html "UNIQUE:©BEFORE-CLOSING-BODY-TAG-HTML" + @before_closing_head_tag_content_epub "UNIQUE:©BEFORE-CLOSING-HEAD-TAG-EPUB" + @before_closing_body_tag_content_epub "UNIQUE:©BEFORE-CLOSING-BODY-TAG-EPUB" + + 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", @@ -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 @@ -213,8 +221,8 @@ 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*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] + assert content =~ ~r[#{@before_closing_head_tag_content_html}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_html}\s*] end test "before_closing_*_tags are placed in the right place - generated pages" do @@ -222,8 +230,8 @@ defmodule ExDoc.Formatter.HTMLTest do generate_docs(config) content = File.read!("#{output_dir()}/readme.html") - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] + assert content =~ ~r[#{@before_closing_head_tag_content_html}\s*] + assert content =~ ~r[#{@before_closing_body_tag_content_html}\s*] end test "run generates pages with custom names" do