Class: Jekyll::TableOfContents::Parser
- Inherits:
-
Object
- Object
- Jekyll::TableOfContents::Parser
- Includes:
- Helper
- Defined in:
- lib/table_of_contents/parser.rb
Overview
Parse html contents and generate table of contents
Constant Summary
Constants included from Helper
Instance Method Summary collapse
- #build_toc ⇒ Object
-
#initialize(html, options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #inject_anchors_into_html ⇒ Object
- #toc ⇒ Object
Methods included from Helper
#extract_text, #generate_toc_id
Constructor Details
#initialize(html, options = {}) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 15 |
# File 'lib/table_of_contents/parser.rb', line 11 def initialize(html, = {}) @doc = Nokogiri::HTML::DocumentFragment.parse(html) @configuration = Configuration.new() @entries = parse_content end |
Instance Method Details
#build_toc ⇒ Object
21 22 23 |
# File 'lib/table_of_contents/parser.rb', line 21 def build_toc %(<#{list_tag} id="#{@configuration.list_id}" class="#{@configuration.list_class}">\n#{build_toc_list(@entries)}</#{list_tag}>) end |
#inject_anchors_into_html ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/table_of_contents/parser.rb', line 25 def inject_anchors_into_html @entries.each do |entry| # NOTE: `entry[:id]` is automatically URL encoded by Nokogiri 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 |
#toc ⇒ Object
17 18 19 |
# File 'lib/table_of_contents/parser.rb', line 17 def toc build_toc + inject_anchors_into_html end |