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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
defmodule ExDoc.Utils do
@moduledoc false
@doc """
Emits a warning.
"""
if Version.match?(System.version(), ">= 1.14.0") do
def warn(message, stacktrace_info) do
IO.warn(message, stacktrace_info)
end
else
def warn(message, _stacktrace_info) do
IO.warn(message, [])
end
end
@doc """
Runs the `before_closing_head_tag` callback.
"""
def before_closing_head_tag(%{before_closing_head_tag: {m, f, a}}, module) do
apply(m, f, [module | a])
end
def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module)
when is_map(before_closing_head_tag) do
Map.get(before_closing_head_tag, module, "")
end
def before_closing_head_tag(%{before_closing_head_tag: before_closing_head_tag}, module) do
before_closing_head_tag.(module)
end
@doc """
Runs the `before_closing_footer_tag` callback.
"""
def before_closing_footer_tag(%{before_closing_footer_tag: {m, f, a}}, module) do
apply(m, f, [module | a])
end
def before_closing_footer_tag(%{before_closing_footer_tag: before_closing_footer_tag}, module)
when is_map(before_closing_footer_tag) do
Map.get(before_closing_footer_tag, module, "")
end
def before_closing_footer_tag(%{before_closing_footer_tag: before_closing_footer_tag}, module) do
before_closing_footer_tag.(module)
end
@doc """
Runs the `before_closing_body_tag` callback.
"""
def before_closing_body_tag(%{before_closing_body_tag: {m, f, a}}, module) do
apply(m, f, [module | a])
end
def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module)
when is_map(before_closing_body_tag) do
Map.get(before_closing_body_tag, module, "")
end
def before_closing_body_tag(%{before_closing_body_tag: before_closing_body_tag}, module) do
before_closing_body_tag.(module)
end
@doc """
HTML escapes the given string.
iex> ExDoc.Utils.h("<foo>")
"<foo>"
"""
def h(string) do
String.replace(string, ~w|& < > "|, fn
"&" -> "&"
"<" -> "<"
">" -> ">"
~S(") -> """
end)
end
@clean_html_regex ~r/<\/?\s*[a-zA-Z]+(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>/
@doc """
Strips HTML tags from text leaving their text content
"""
def strip_tags(text, replace_with \\ "") when is_binary(text) do
String.replace(text, @clean_html_regex, replace_with)
end
@doc """
Generates an ID from some text.
Used primarily with titles, headings, and functions group names.
"""
def text_to_id(atom) when is_atom(atom), do: text_to_id(Atom.to_string(atom))
def text_to_id(text) when is_binary(text) do
text
|> strip_tags()
|> String.replace(~r/&#\d+;/, "")
|> String.replace(~r/&[A-Za-z0-9]+;/, "")
|> String.replace(~r/\W+/u, "-")
|> String.trim("-")
|> String.downcase()
end
@doc """
Sorts mapped strings by natural order.
"""
def natural_sort_by(enumerable, mapper) when is_function(mapper, 1) do
Enum.sort_by(enumerable, fn elem ->
elem
|> mapper.()
|> to_sortable_charlist()
end)
end
defp to_sortable_charlist(string) do
string
|> :unicode.characters_to_nfkd_list()
|> make_sortable()
end
@offset -1_000_000_000
# Numbers come first, so group and pad them with offset
defp make_sortable([digit | chars]) when digit in ?0..?9 do
{digits, chars} = Enum.split_while(chars, &(&1 in ?0..?9))
[@offset + List.to_integer([digit | digits]) | make_sortable(chars)]
end
# Then Elixir special punctuation - trailing bang `!`
defp make_sortable([?! | chars]), do: [?0 | make_sortable(chars)]
# Then Elixir special punctuation - question mark `?`
defp make_sortable([?? | chars]), do: [?1 | make_sortable(chars)]
# Then underscore
defp make_sortable([?_ | chars]), do: [?2 | make_sortable(chars)]
# Then uppercased letters and lowercased letters
defp make_sortable([char | chars]) when char in ?a..?z do
[char - 31.5 | make_sortable(chars)]
end
defp make_sortable([char | chars]), do: [char | make_sortable(chars)]
defp make_sortable([]), do: []
@doc """
A very simple JSON encoder.
We want to minimize the number of dependencies ExDoc has,
because we don't want someone to be allowed to not upgrade
their app due to an ExDoc restriction, so we ship with a
simple JSON implementation.
"""
def to_json(nil), do: "null"
def to_json(true), do: "true"
def to_json(false), do: "false"
def to_json(map) when is_map(map) do
mapped =
Enum.map_intersperse(Map.to_list(map), ?,, fn {key, value} ->
[key |> Atom.to_string() |> inspect(), ?:, to_json(value)]
end)
[?{, mapped, ?}]
end
def to_json(list) when is_list(list) do
mapped = Enum.map_intersperse(list, ?,, &to_json/1)
[?[, mapped, ?]]
end
def to_json(atom) when is_atom(atom) do
atom |> Atom.to_string() |> inspect()
end
def to_json(binary) when is_binary(binary) do
to_json_string(binary, "\"")
end
def to_json(integer) when is_integer(integer) do
Integer.to_string(integer)
end
defp to_json_string(<<?\b, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\b">>)
defp to_json_string(<<?\t, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\t">>)
defp to_json_string(<<?\n, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\n">>)
defp to_json_string(<<?\f, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\f">>)
defp to_json_string(<<?\r, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\r">>)
defp to_json_string(<<?\\, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\\\">>)
defp to_json_string(<<?", rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, "\\\"">>)
defp to_json_string(<<x, rest::binary>>, acc) when x <= 0x000F,
do: to_json_string(rest, <<acc::binary, "\\u000", Integer.to_string(x, 16)::binary>>)
defp to_json_string(<<x, rest::binary>>, acc) when x <= 0x001F,
do: to_json_string(rest, <<acc::binary, "\\u00", Integer.to_string(x, 16)::binary>>)
defp to_json_string(<<x, rest::binary>>, acc),
do: to_json_string(rest, <<acc::binary, x>>)
defp to_json_string(<<>>, acc), do: <<acc::binary, "\"">>
@doc """
Generates a url based on the given pattern.
"""
def source_url_pattern(source_url_pattern, path, line)
when is_binary(path) and is_integer(line) do
cond do
is_function(source_url_pattern) ->
source_url_pattern.(path, line)
source_url_pattern ->
source_url_pattern
|> String.replace("%{path}", path)
|> String.replace("%{line}", Integer.to_string(line))
true ->
nil
end
end
end
|