Packages
ex_doc
0.7.3
0.40.3
0.40.2
0.40.1
0.40.0
0.39.3
0.39.2
0.39.1
0.39.0
0.38.4
0.38.3
0.38.2
0.38.1
0.38.0
0.37.3
0.37.2
0.37.1
0.37.0
0.37.0-rc.2
0.37.0-rc.1
0.37.0-rc.0
0.36.1
0.36.0
0.35.1
0.35.0
0.34.2
0.34.1
0.34.0
0.33.0
0.32.2
0.32.1
0.32.0
0.31.2
0.31.1
0.31.0
0.30.9
0.30.8
0.30.7
0.30.6
0.30.5
0.30.4
0.30.3
0.30.2
0.30.1
0.30.0
0.29.4
0.29.3
0.29.2
0.29.1
0.29.0
0.28.6
0.28.5
0.28.4
0.28.3
0.28.2
0.28.1
0.28.0
0.27.3
0.27.2
0.27.1
0.27.0
0.26.0
0.25.5
0.25.4
0.25.3
0.25.2
0.25.1
0.25.0
0.24.2
0.24.1
0.24.0
0.23.0
0.22.6
0.22.5
0.22.4
0.22.2
0.22.1
0.22.0
0.21.3
0.21.2
0.21.1
0.21.0
0.20.2
0.20.1
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.19.0-rc
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
retired
0.17.1
0.17.0
retired
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
ExDoc is a documentation generation tool for Elixir
Current section
Files
Jump to
Current section
Files
lib/ex_doc/formatter/html/templates.ex
defmodule ExDoc.Formatter.HTML.Templates do
@moduledoc """
Handle all template interfaces for the HTML formatter.
"""
require EEx
@doc """
Generate content from the module template for a given `node`
"""
def module_page(node, config, all) do
types = node.typespecs
functions = Enum.filter node.docs, &match?(%ExDoc.FunctionNode{type: :def}, &1)
macros = Enum.filter node.docs, &match?(%ExDoc.FunctionNode{type: :defmacro}, &1)
callbacks = Enum.filter node.docs, &match?(%ExDoc.FunctionNode{type: :defcallback}, &1)
module_template(config, node, types, functions, macros, callbacks, all)
end
@doc """
Generates the listing.
"""
def list_page(scope, nodes, config, has_readme) do
list_template(scope, nodes, config, has_readme)
end
# Get the full specs from a function, already in HTML form.
defp get_specs(%ExDoc.FunctionNode{specs: specs}) when is_list(specs) do
presence specs
end
defp get_specs(_node), do: nil
# Convert markdown to HTML.
defp to_html(nil), do: nil
defp to_html(bin) when is_binary(bin), do: ExDoc.Markdown.to_html(bin)
# Get the pretty name of a function node
defp pretty_type(%ExDoc.FunctionNode{type: t}) do
case t do
:def -> "function"
:defmacro -> "macro"
:defcallback -> "callback"
:type -> "type"
end
end
# Generate a link id
defp link_id(node), do: link_id(node.id, node.type)
defp link_id(id, type) do
case type do
:defcallback -> "c:#{id}"
:type -> "t:#{id}"
_ -> "#{id}"
end
end
# Get the first paragraph of the documentation of a node, if any.
defp synopsis(nil), do: nil
defp synopsis(doc) do
String.split(doc, ~r/\n\s*\n/) |> hd |> String.strip() |> String.rstrip(?.)
end
# A bit of standard HTML to insert the to-top arrow.
defp to_top_link() do
"<a class=\"to_top_link\" href=\"#content\" title=\"To the top of the page\">↑</a>"
end
defp presence([]), do: nil
defp presence(other), do: other
defp h(binary) do
escape_map = [{"&", "&"}, {"<", "<"}, {">", ">"}, {"\"", """}]
Enum.reduce escape_map, binary, fn({pattern, escape}, acc) ->
String.replace(acc, pattern, escape)
end
end
# Get the breadcrumbs HTML.
#
# If module is :overview generates the breadcrumbs for the overview.
defp module_breadcrumbs(config, modules, module) do
parts = [root_breadcrumbs(config), { "Overview", "overview.html" }]
aliases = Module.split(module.module)
modules = Enum.map(modules, &(&1.module))
{ crumbs, _ } =
Enum.map_reduce(aliases, [], fn item, parents ->
path = parents ++ [item]
mod = Module.concat(path)
page = if mod in modules, do: inspect(mod) <> ".html"
{ { item, page }, path }
end)
generate_breadcrumbs(parts ++ crumbs)
end
defp page_breadcrumbs(config, title, link) do
generate_breadcrumbs [root_breadcrumbs(config), { title, link }]
end
defp root_breadcrumbs(config) do
{ "#{config.project} v#{config.version}", nil }
end
defp generate_breadcrumbs(crumbs) do
Enum.map_join(crumbs, " → ", fn { name, ref } ->
if ref, do: "<a href=\"#{h(ref)}\">#{h(name)}</a>", else: h(name)
end)
end
templates = [
index_template: [:config],
list_template: [:scope, :nodes, :config, :has_readme],
overview_template: [:config, :modules, :exceptions, :protocols],
module_template: [:config, :module, :types, :functions, :macros, :callbacks, :all],
readme_template: [:config, :content],
list_item_template: [:node],
overview_entry_template: [:node],
summary_template: [:node],
detail_template: [:node, :_module],
type_detail_template: [:node, :_module],
]
Enum.each templates, fn({ name, args }) ->
filename = Path.expand("templates/#{name}.eex", __DIR__)
EEx.function_from_file :def, name, filename, args
end
end