Skip to content

Commit

Permalink
feat(epub): add support for group_for_modules in toc (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
milmazz authored Jan 25, 2024
1 parent 3fbe0bc commit b4ac831
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
5 changes: 5 additions & 0 deletions lib/ex_doc/formatter/epub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ defmodule ExDoc.Formatter.EPUB do
end

defp generate_nav(config, nodes) do
nodes =
Map.update!(nodes, :modules, fn modules ->
modules |> Enum.chunk_by(& &1.group) |> Enum.map(&{hd(&1).group, &1})
end)

content = Templates.nav_template(config, nodes)
File.write("#{config.output}/OEBPS/nav.xhtml", content)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ex_doc/formatter/epub/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ defmodule ExDoc.Formatter.EPUB.Templates do
trim: true
)

EEx.function_from_file(
:defp,
:nav_grouped_item_template,
Path.expand("templates/nav_grouped_item_template.eex", __DIR__),
[:nodes],
trim: true
)

EEx.function_from_file(
:defp,
:toc_item_template,
Expand Down
13 changes: 13 additions & 0 deletions lib/ex_doc/formatter/epub/templates/nav_grouped_item_template.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= for {title, nodes} <- nodes do %>
<%= if title do %>
<li><span><%=h to_string(title) %></span>
<ol>
<% end %>
<%= for node <- nodes do %>
<li><a href="<%= URI.encode node.id %>.xhtml"><%=h node.title %></a></li>
<% end %>
<%= if title do %>
</ol>
</li>
<% end %>
<% end %>
24 changes: 10 additions & 14 deletions lib/ex_doc/formatter/epub/templates/nav_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
<h1>Table of contents</h1>
<nav epub:type="toc">
<ol>
<%= for {title, extras} <- config.extras do %>
<%= if title do %>
<li><span><%=h to_string(title) %></span>
<ol>
<% end %>
<%= for extra <- extras do %>
<li><a href="<%= URI.encode extra.id %>.xhtml"><%=h extra.title %></a></li>
<% end %>
<%= if title do %>
</ol>
</li>
<% end %>
<% end %>
<%= nav_grouped_item_template config.extras %>

<%= nav_item_template "Modules", nodes.modules %>
<%= unless Enum.empty?(nodes.modules) do %>
<ol>
<li><span>Modules</span>
<ol>
<%= nav_grouped_item_template nodes.modules %>
</ol>
</li>
</ol>
<% end %>
<%= nav_item_template "Mix Tasks", nodes.tasks %>
</ol>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ defmodule ExDoc.Formatter.HTMLTest do
generate_docs(doc_config(context, skip_undefined_reference_warnings_on: []))
end)

assert out =~ ~s| documentation references function \"Warnings.bar/0\" but|
assert out =~ ~s|documentation references function "Warnings.bar/0" but|

# TODO: remove check when we require Elixir v1.16
if Version.match?(System.version(), ">= 1.16.0-rc") do
Expand Down

0 comments on commit b4ac831

Please sign in to comment.