Skip to content

Commit bb203f2

Browse files
author
Simeon F. Willbanks
committed
Keep TableOfContentsFilter interface consistent with other filters by removing attr_reader. Also, reduce complexity by removing private methods and keep changes within #call. Finally, implement #build_toc to wrap result[:toc] in an unordered list.
1 parent e81d2ff commit bb203f2

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

lib/html/pipeline/toc_filter.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@ class Pipeline
2525
class TableOfContentsFilter < Filter
2626
PUNCTUATION_REGEXP = RUBY_VERSION > "1.9" ? /[^\p{Word}\- ]/u : /[^\w\- ]/
2727

28-
# Public: Gets the String value of the Table of Contents.
29-
attr_reader :toc
30-
3128
def call
3229
result[:toc] = ""
3330

34-
@toc = ""
35-
3631
headers = Hash.new(0)
3732
doc.css('h1, h2, h3, h4, h5, h6').each do |node|
3833
text = node.text
@@ -43,27 +38,17 @@ def call
4338
uniq = (headers[name] > 0) ? "-#{headers[name]}" : ''
4439
headers[name] += 1
4540
if header_content = node.children.first
46-
append_toc text, name, uniq
47-
add_header_anchor header_content, name, uniq
41+
result[:toc] << %Q{<li><a href="##{name}#{uniq}">#{text}</a></li>\n}
42+
header_content.add_previous_sibling(%Q{<a name="#{name}#{uniq}" class="anchor" href="##{name}#{uniq}"><span class="octicon octicon-link"></span></a>})
4843
end
4944
end
50-
finalize_toc unless toc.empty?
45+
build_toc
5146
doc
5247
end
5348

54-
# Private: Add header link to Table of Contents list.
55-
def append_toc(text, name, uniq)
56-
@toc << %Q{<li><a href="##{name}#{uniq}">#{text}</a></li>\n}
57-
end
58-
59-
# Private: Add an anchor with 'name' attribute to a header.
60-
def add_header_anchor(header_content, name, uniq)
61-
header_content.add_previous_sibling(%Q{<a name="#{name}#{uniq}" class="anchor" href="##{name}#{uniq}"><span class="octicon octicon-link"></span></a>})
62-
end
63-
6449
# Private: Wrap Table of Contents list items within a container.
65-
def finalize_toc
66-
result[:toc] = %Q{<ul class="section-nav">\n#{toc}</ul>}
50+
def build_toc
51+
result[:toc] = %Q{<ul class="section-nav">\n#{result[:toc]}</ul>} unless result[:toc].empty?
6752
end
6853
end
6954
end

0 commit comments

Comments
 (0)