From dcad862c24f8bca320e369fb553b06049543d74b Mon Sep 17 00:00:00 2001 From: tmbb Date: Wed, 2 Aug 2017 10:40:42 +0100 Subject: [PATCH 1/4] Markdown modules are now 100% responsible for code Code blocks are now handled by the markdown modules, and ExDoc doesn't do any post-processing. The modules bundled with ExDoc were updated to reflect this. The goal of this change is to make it easier to use alternative Markdown implementations. This might break compatibility for people that are already usin Markdown extensions. --- lib/ex_doc/markdown.ex | 2 +- lib/ex_doc/markdown/cmark.ex | 2 +- lib/ex_doc/markdown/earmark.ex | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ex_doc/markdown.ex b/lib/ex_doc/markdown.ex index 62b457868..f747ab2c6 100644 --- a/lib/ex_doc/markdown.ex +++ b/lib/ex_doc/markdown.ex @@ -29,7 +29,7 @@ defmodule ExDoc.Markdown do Converts the given markdown document to HTML. """ def to_html(text, opts \\ []) when is_binary(text) do - pretty_codeblocks(get_markdown_processor().to_html(text, opts)) + get_markdown_processor().to_html(text, opts) end @doc """ diff --git a/lib/ex_doc/markdown/cmark.ex b/lib/ex_doc/markdown/cmark.ex index 5b92166f5..031810f55 100644 --- a/lib/ex_doc/markdown/cmark.ex +++ b/lib/ex_doc/markdown/cmark.ex @@ -15,6 +15,6 @@ defmodule ExDoc.Markdown.Cmark do Generate HTML output. Cmark takes no options. """ def to_html(text, _opts) do - Cmark.to_html(text) + Cmark.to_html(text) |> ExDoc.Markdown.pretty_codeblocks end end diff --git a/lib/ex_doc/markdown/earmark.ex b/lib/ex_doc/markdown/earmark.ex index 05a79b7c6..d9eb07458 100644 --- a/lib/ex_doc/markdown/earmark.ex +++ b/lib/ex_doc/markdown/earmark.ex @@ -32,6 +32,6 @@ defmodule ExDoc.Markdown.Earmark do file: Keyword.get(opts, :file), breaks: Keyword.get(opts, :breaks, false), smartypants: Keyword.get(opts, :smartypants, true)) - Earmark.as_html!(text, options) + Earmark.as_html!(text, options) |> ExDoc.Markdown.pretty_codeblocks end end From 34ae4f26c633e3139cff6293972caed71d6f52b2 Mon Sep 17 00:00:00 2001 From: tmbb Date: Wed, 2 Aug 2017 12:03:53 +0100 Subject: [PATCH 2/4] Added options to customize templates Docs still missing --- lib/ex_doc.ex | 4 +++ .../html/templates/footer_template.eex | 1 + .../html/templates/head_template.eex | 1 + test/ex_doc/formatter/html_test.exs | 25 ++++++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) 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/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index a4f5b6713..59e342e90 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 @@ -46,18 +51,30 @@ defmodule ExDoc.Formatter.HTMLTest do generate_docs doc_config(source_url: "#{scheme}://github.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://github.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L14" + # before_closing_*_tag comes is just before the respective tag + assert content =~ ~r[#{@before_closing_head_tag}\s*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://gitlab.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://gitlab.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L14" + # before_closing_*_tag comes is just before the respective tag + assert content =~ ~r[#{@before_closing_head_tag}\s*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://bitbucket.org/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://bitbucket.org/elixir-lang/ex_doc/src/master/test/fixtures/compiled_with_docs.ex#cl-14" + # before_closing_*_tag comes is just before the respective tag + assert content =~ ~r[#{@before_closing_head_tag}\s*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://example.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "#{scheme}://example.com/elixir-lang/ex_doc" + # before_closing_*_tag comes is just before the respective tag + assert content =~ ~r[#{@before_closing_head_tag}\s*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] end end @@ -186,6 +203,9 @@ defmodule ExDoc.Formatter.HTMLTest do assert content =~ ~r{

moduledoc

} assert content =~ ~r{CompiledWithDocs.Nested} assert content =~ ~r{task_with_docs} + # before_closing_*_tag comes is just before the respective tag + assert content =~ ~r[#{@before_closing_head_tag}\s*] + assert content =~ ~r[#{@before_closing_body_tag}\s*] end test "run generates pages" do @@ -202,6 +222,9 @@ defmodule ExDoc.Formatter.HTMLTest do assert content =~ ~r{RandomError} assert content =~ ~r{CustomBehaviourImpl.hello/1} assert content =~ ~r{TypesAndSpecs.Sub} + # before_closing_*_tag comes is just before the respective tag + 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 From 1559a654af42d95079f35b640af982404569708a Mon Sep 17 00:00:00 2001 From: tmbb Date: Wed, 2 Aug 2017 12:14:16 +0100 Subject: [PATCH 3/4] Added new options to `mix help docs` --- lib/mix/tasks/docs.ex | 8 ++++++++ 1 file changed, 8 insertions(+) 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. From feb2c781da486eebf65da9fa1a47a80b9861ad29 Mon Sep 17 00:00:00 2001 From: tmbb Date: Wed, 2 Aug 2017 12:49:22 +0100 Subject: [PATCH 4/4] before_closing_*_tags have their own test now --- test/ex_doc/formatter/html_test.exs | 31 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/ex_doc/formatter/html_test.exs b/test/ex_doc/formatter/html_test.exs index 59e342e90..cda64549b 100644 --- a/test/ex_doc/formatter/html_test.exs +++ b/test/ex_doc/formatter/html_test.exs @@ -51,30 +51,18 @@ defmodule ExDoc.Formatter.HTMLTest do generate_docs doc_config(source_url: "#{scheme}://github.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://github.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L14" - # before_closing_*_tag comes is just before the respective tag - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://gitlab.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://gitlab.com/elixir-lang/ex_doc/blob/master/test/fixtures/compiled_with_docs.ex#L14" - # before_closing_*_tag comes is just before the respective tag - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://bitbucket.org/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "https://bitbucket.org/elixir-lang/ex_doc/src/master/test/fixtures/compiled_with_docs.ex#cl-14" - # before_closing_*_tag comes is just before the respective tag - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] generate_docs doc_config(source_url: "#{scheme}://example.com/elixir-lang/ex_doc", source_root: File.cwd!) content = File.read!(file_path) assert content =~ "#{scheme}://example.com/elixir-lang/ex_doc" - # before_closing_*_tag comes is just before the respective tag - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] end end @@ -203,9 +191,6 @@ defmodule ExDoc.Formatter.HTMLTest do assert content =~ ~r{

moduledoc

} assert content =~ ~r{CompiledWithDocs.Nested} assert content =~ ~r{task_with_docs} - # before_closing_*_tag comes is just before the respective tag - assert content =~ ~r[#{@before_closing_head_tag}\s*] - assert content =~ ~r[#{@before_closing_body_tag}\s*] end test "run generates pages" do @@ -222,7 +207,21 @@ defmodule ExDoc.Formatter.HTMLTest do assert content =~ ~r{RandomError} assert content =~ ~r{CustomBehaviourImpl.hello/1} assert content =~ ~r{TypesAndSpecs.Sub} - # before_closing_*_tag comes is just before the respective tag + 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