Class: HTMLPipeline::NodeFilter::TableOfContentsFilter

Inherits:
HTMLPipeline::NodeFilter show all
Defined in:
lib/html_pipeline/node_filter/table_of_contents_filter.rb

Overview

Examples

TocPipeline = HTMLPipeline.new [ HTMLPipeline::TableOfContentsFilter ]

=> #HTMLPipeline:0x007fc13c4528d8...

orig = %(

Ice cube

is not for the pop chart

)

=> "

Ice cube

is not for the pop chart

"

result = {}

=> {}

TocPipeline.call(orig, {}, result)

=> ...

result

=> ":text=>"Ice cube""

result.to_s

=> "

\n<a id="ice-cube" class="anchor" href="#ice-cube">..."

Constant Summary collapse

SELECTOR =
Selma::Selector.new(
  match_element: "h1 a[href], h2 a[href], h3 a[href], h4 a[href], h5 a[href], h6 a[href]",
  match_text_within: "h1, h2, h3, h4, h5, h6",
)

Instance Attribute Summary

Attributes inherited from HTMLPipeline::NodeFilter

#context

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from HTMLPipeline::NodeFilter

call, #html, #initialize, #reset!

Methods inherited from Filter

#base_url, #call, call, #has_ancestor?, #initialize, #needs, #validate

Constructor Details

This class inherits a constructor from HTMLPipeline::NodeFilter

Instance Method Details

#after_initializeObject



46
47
48
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 46

def after_initialize
  result[:toc] = []
end

#anchor_htmlObject

The icon that will be placed next to an anchored rendered markdown header



37
38
39
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 37

def anchor_html
  @context[:anchor_html] || %(<span aria-hidden="true" class="anchor"></span>)
end

#classesObject

The class that will be attached on the anchored rendered markdown header



42
43
44
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 42

def classes
  context[:classes] || "anchor"
end

#handle_element(element) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 50

def handle_element(element)
  header_href = element["href"]

  return unless header_href.start_with?("#")

  header_id = header_href[1..-1]

  element["id"] = header_id
  element["class"] = classes

  element.set_inner_content(anchor_html, as: :html)

  result[:toc] << { href: header_href }
end

#handle_text_chunk(text) ⇒ Object



65
66
67
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 65

def handle_text_chunk(text)
  result[:toc].last[:text] = text.to_s
end

#selectorObject



32
33
34
# File 'lib/html_pipeline/node_filter/table_of_contents_filter.rb', line 32

def selector
  SELECTOR
end