Class: Jekyll::TableOfContents::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/table_of_contents/parser.rb

Overview

Parse html contents and generate table of contents

Constant Summary collapse

PUNCTUATION_REGEXP =
/[^\p{Word}\- ]/u.freeze

Instance Method Summary collapse

Constructor Details

#initialize(html, options = {}) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
# File 'lib/table_of_contents/parser.rb', line 9

def initialize(html, options = {})
  @doc = Nokogiri::HTML::DocumentFragment.parse(html)
  @configuration = Configuration.new(options)
  @entries = parse_content
end

Instance Method Details

#build_tocObject



19
20
21
# File 'lib/table_of_contents/parser.rb', line 19

def build_toc
  %(<ul class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}</ul>)
end

#inject_anchors_into_htmlObject



23
24
25
26
27
28
29
30
31
# File 'lib/table_of_contents/parser.rb', line 23

def inject_anchors_into_html
  @entries.each do |entry|
    entry[:header_content].add_previous_sibling(
      %(<a class="anchor" href="##{entry[:id]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)
    )
  end

  @doc.inner_html
end

#tocObject



15
16
17
# File 'lib/table_of_contents/parser.rb', line 15

def toc
  build_toc + inject_anchors_into_html
end