Skip to content

Commit d9d8fff

Browse files
committed
Explicitly pass headers as arguments
1 parent 91c3304 commit d9d8fff

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

lib/ex_doc/doc_ast.ex

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ defmodule ExDoc.DocAST do
115115
116116
Returns the header text.
117117
"""
118-
def extract_headers(doc_ast) do
119-
for {:h2, _, _, _} = node <- doc_ast,
118+
def extract_headers(doc_ast, headers) do
119+
for {tag, _, _, _} = node <- doc_ast,
120+
tag in headers,
120121
text = ExDoc.DocAST.text(node),
121122
text != "",
122123
do: text
@@ -168,17 +169,17 @@ defmodule ExDoc.DocAST do
168169
defp do_text({_tag, _attr, ast, _meta}), do: text(ast)
169170

170171
@doc """
171-
Wraps a list of HTML nodes into `<section>` tags whenever `matcher` returns true.
172+
Wraps a list of HTML nodes into `<section>` tags whenever `headers` returns true.
172173
"""
173-
def sectionize(list, matcher), do: sectionize(list, matcher, [])
174+
def sectionize(list, headers), do: sectionize(list, headers, [])
174175

175-
defp sectionize(list, matcher, acc) do
176-
case pivot(list, acc, matcher) do
176+
defp sectionize(list, headers, acc) do
177+
case pivot(list, acc, headers) do
177178
{acc, {header_tag, header_attrs, _, _} = header, rest} ->
178179
{inner, rest} = Enum.split_while(rest, &not_tag?(&1, header_tag))
179180
class = String.trim_trailing("#{header_tag} #{header_attrs[:class]}")
180-
section = {:section, [class: class], [header | sectionize(inner, matcher, [])], %{}}
181-
sectionize(rest, matcher, [section | acc])
181+
section = {:section, [class: class], [header | sectionize(inner, headers, [])], %{}}
182+
sectionize(rest, headers, [section | acc])
182183

183184
acc ->
184185
acc
@@ -188,14 +189,16 @@ defmodule ExDoc.DocAST do
188189
defp not_tag?({tag, _, _, _}, tag), do: false
189190
defp not_tag?(_, _tag), do: true
190191

191-
defp pivot([head | tail], acc, fun) do
192-
case fun.(head) do
193-
true -> {acc, head, tail}
194-
false -> pivot(tail, [head | acc], fun)
192+
defp pivot([{tag, _, _, _} = head | tail], acc, headers) do
193+
if tag in headers do
194+
{acc, head, tail}
195+
else
196+
pivot(tail, [head | acc], headers)
195197
end
196198
end
197199

198-
defp pivot([], acc, _fun), do: Enum.reverse(acc)
200+
defp pivot([head | tail], acc, headers), do: pivot(tail, [head | acc], headers)
201+
defp pivot([], acc, _headers), do: Enum.reverse(acc)
199202

200203
@doc """
201204
Highlights a DocAST converted to string.

lib/ex_doc/formatter/html.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ defmodule ExDoc.Formatter.HTML do
460460
search_data: search_data,
461461
title: title,
462462
title_content: title_html || title,
463-
headers: ExDoc.DocAST.extract_headers(ast)
463+
headers: ExDoc.DocAST.extract_headers(ast, [:h2])
464464
}
465465
end
466466

@@ -491,11 +491,7 @@ defmodule ExDoc.Formatter.HTML do
491491
end
492492

493493
defp sectionize(ast, ".cheatmd") do
494-
ExDoc.DocAST.sectionize(ast, fn
495-
{:h2, _, _, _} -> true
496-
{:h3, _, _, _} -> true
497-
_ -> false
498-
end)
494+
ExDoc.DocAST.sectionize(ast, [:h2, :h3])
499495
end
500496

501497
defp sectionize(ast, _), do: ast

lib/ex_doc/formatter/html/templates.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ defmodule ExDoc.Formatter.HTML.Templates do
140140
defp module_sections(module) do
141141
{sections, _} =
142142
module.doc
143-
|> ExDoc.DocAST.extract_headers()
143+
|> ExDoc.DocAST.extract_headers([:h2])
144144
|> headers_to_id_and_anchors()
145145
|> Enum.map_reduce(%{}, fn header, acc ->
146146
# TODO Duplicates some of the logic of link_headings/3

test/ex_doc/doc_ast_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ defmodule ExDoc.DocASTTest do
171171
defp extract_headers(markdown) do
172172
markdown
173173
|> ExDoc.DocAST.parse!("text/markdown")
174-
|> ExDoc.DocAST.extract_headers()
174+
|> ExDoc.DocAST.extract_headers([:h2])
175175
end
176176
end
177177

@@ -246,7 +246,7 @@ defmodule ExDoc.DocASTTest do
246246
{:p, [], ["p6"], %{}}
247247
]
248248

249-
assert DocAST.sectionize(list, &h2_or_h3?/1) ==
249+
assert DocAST.sectionize(list, [:h2, :h3]) ==
250250
[
251251
{:h1, [], ["H1"], %{}},
252252
{:section, [class: "h2 example"],
@@ -281,9 +281,5 @@ defmodule ExDoc.DocASTTest do
281281
], %{}}
282282
]
283283
end
284-
285-
defp h2_or_h3?({:h2, _, _, _}), do: true
286-
defp h2_or_h3?({:h3, _, _, _}), do: true
287-
defp h2_or_h3?(_), do: false
288284
end
289285
end

0 commit comments

Comments
 (0)