1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
defmodule ExDoc.Formatter.HTML.SearchData do
@moduledoc false
alias ExDoc.Utils
def create(nodes, extras, proglang) do
items = Enum.flat_map(nodes, &module/1) ++ Enum.flat_map(extras, &extra/1)
data = %{
items: items,
content_type: "text/markdown",
proglang: proglang,
producer: %{
name: "ex_doc",
version: to_string(Application.spec(:ex_doc)[:vsn])
}
}
["searchData=" | ExDoc.Utils.to_json(data)]
end
defp extra(map) do
{intro, sections} = extract_sections_from_markdown(map.source)
intro_json_item =
encode(
"#{map.id}.html",
map.title,
:extras,
intro
)
section_json_items =
for {header, body} <- sections do
encode(
"#{map.id}.html##{Utils.text_to_id(header)}",
header <> " - #{map.title}",
:extras,
body
)
end
[intro_json_item | section_json_items]
end
defp module(%ExDoc.ModuleNode{} = node) do
{intro, sections} = extract_sections(node.doc_format, node)
module =
encode(
"#{node.id}.html",
node.title,
node.type,
intro
)
module_sections =
for {header, body} <- sections do
encode(
"#{node.id}.html#module-#{Utils.text_to_id(header)}",
header <> " - #{node.title}",
node.type,
body
)
end
functions = Enum.flat_map(node.docs, &node_child(&1, node))
types = Enum.flat_map(node.typespecs, &node_child(&1, node))
[module] ++ module_sections ++ functions ++ types
end
defp node_child(node, module_node) do
{intro, sections} = extract_sections(module_node.doc_format, node)
child =
encode(
"#{module_node.id}.html##{node.id}",
"#{module_node.id}.#{node.name}/#{node.arity}",
node.type,
intro
)
child_sections =
for {header, body} <- sections do
encode(
"#{module_node.id}.html##{node.id}-#{Utils.text_to_id(header)}",
header <> " - #{module_node.id}.#{node.name}/#{node.arity}",
node.type,
body
)
end
[child] ++ child_sections
end
defp encode(ref, title, type, doc) do
%{
ref: URI.encode(ref),
title: title,
type: type,
doc: doc
}
end
defp extract_sections("text/markdown", %{source_doc: %{"en" => doc}}) do
extract_sections_from_markdown(doc)
end
defp extract_sections("application/erlang+html", %{rendered_doc: nil}) do
{nil, []}
end
defp extract_sections("application/erlang+html", %{rendered_doc: doc}) do
{clean_html(doc), []}
end
defp extract_sections(_format, _doc) do
{"", []}
end
defp extract_sections_from_markdown(string) do
[intro | sections] =
Regex.split(~r/(?<!#)###? (?<header>\b.+)/, string, include_captures: true)
sections =
for [header, section] <- Enum.chunk_every(sections, 2) do
header = String.trim_leading(header, "#")
section =
section
|> ExDoc.Utils.strip_tags(" ")
|> drop_ignorable_codeblocks()
|> String.trim()
{clean_markdown(header), section}
end
{clean_markdown(intro), sections}
end
defp clean_markdown(doc) do
doc
|> ExDoc.Utils.strip_tags(" ")
|> String.trim()
end
defp clean_html(doc) do
doc
|> ExDoc.Utils.strip_tags(" ")
|> String.replace(~r/\s+/, " ")
|> String.trim()
end
@ignored_codeblocks ~w[vega-lite]
defp drop_ignorable_codeblocks(section) do
block_names = Enum.join(@ignored_codeblocks, "|")
String.replace(section, ~r/^```(?:#{block_names})\n(?:[\s\S]*?)```$/m, "")
end
end
|