diff --git a/lib/ex_doc.ex b/lib/ex_doc.ex index 26e86d2cc..fbe26f60f 100644 --- a/lib/ex_doc.ex +++ b/lib/ex_doc.ex @@ -25,6 +25,8 @@ defmodule ExDoc do defstruct [ assets: nil, + before_closing_head_tag: "", + before_closing_body_tag: "", canonical: nil, debug: false, deps: [], @@ -51,6 +53,8 @@ defmodule ExDoc do @type t :: %__MODULE__{ assets: nil | String.t, + before_closing_head_tag: String.t, + before_closing_body_tag: String.t, canonical: nil | String.t, debug: boolean(), deps: [{ebin_path :: String.t, doc_url :: String.t}], diff --git a/lib/ex_doc/formatter/html/templates/footer_template.eex b/lib/ex_doc/formatter/html/templates/footer_template.eex index 92c222e5f..2bdffd672 100644 --- a/lib/ex_doc/formatter/html/templates/footer_template.eex +++ b/lib/ex_doc/formatter/html/templates/footer_template.eex @@ -16,5 +16,6 @@ + <%= config.before_closing_body_tag %> diff --git a/lib/ex_doc/formatter/html/templates/head_template.eex b/lib/ex_doc/formatter/html/templates/head_template.eex index 9b61c4979..3ec59ac7a 100644 --- a/lib/ex_doc/formatter/html/templates/head_template.eex +++ b/lib/ex_doc/formatter/html/templates/head_template.eex @@ -11,6 +11,7 @@ <% end %> + <%= config.before_closing_head_tag %> diff --git a/lib/mix/tasks/docs.ex b/lib/mix/tasks/docs.ex index 46d14340f..24300fd34 100644 --- a/lib/mix/tasks/docs.ex +++ b/lib/mix/tasks/docs.ex @@ -54,6 +54,14 @@ 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. + * `:canonical` - String that defines the preferred URL with the rel="canonical" element; defaults to no canonical path. diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index a4f5b6713..cda64549b 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -20,6 +20,9 @@ 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" + defp doc_config do [project: "Elixir", version: "1.0.1", @@ -29,7 +32,9 @@ defmodule ExDoc.Formatter.HTMLTest do source_root: beam_dir(), source_beam: beam_dir(), logo: "test/fixtures/elixir.png", - extras: ["test/fixtures/README.md"]] + extras: ["test/fixtures/README.md"], + before_closing_head_tag: @before_closing_head_tag, + before_closing_body_tag: @before_closing_body_tag] end defp doc_config(config) do @@ -204,6 +209,23 @@ defmodule ExDoc.Formatter.HTMLTest do assert content =~ ~r{TypesAndSpecs.Sub} end + test "before_closing_*_tags are placed in the right place - api reference file" 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*] + 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*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] + end + test "run generates pages with custom names" do generate_docs(doc_config(extras: ["test/fixtures/README.md": [filename: "GETTING-STARTED"]])) refute File.regular?("#{output_dir()}/readme.html")