Class: Jekyll::TableOfContents::Parser
- Inherits:
-
Object
- Object
- Jekyll::TableOfContents::Parser
- 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
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Instance Method Summary collapse
- #build_toc ⇒ Object
-
#initialize(html) ⇒ Parser
constructor
A new instance of Parser.
- #inject_anchors_into_html ⇒ Object
- #toc ⇒ Object
Constructor Details
#initialize(html) ⇒ Parser
Returns a new instance of Parser.
9 10 11 12 |
# File 'lib/table_of_contents/parser.rb', line 9 def initialize(html) @doc = Nokogiri::HTML::DocumentFragment.parse(html) @entries = parse_content end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
7 8 9 |
# File 'lib/table_of_contents/parser.rb', line 7 def doc @doc end |
Instance Method Details
#build_toc ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/table_of_contents/parser.rb', line 14 def build_toc toc = %(<ul class="section-nav">\n) min_h_num = 6 @entries.each do |entry| h_num = entry[:node_name].delete('h').to_i min_h_num = [min_h_num, h_num].min end toc << build_lis(@entries, min_h_num) toc << '</ul>' end |
#inject_anchors_into_html ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/table_of_contents/parser.rb', line 27 def inject_anchors_into_html @entries.each do |entry| entry[:content_node].add_previous_sibling(%(<a id="#{entry[:id]}#{entry[:uniq]}" class="anchor" href="##{entry[:id]}#{entry[:uniq]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>)) end @doc.inner_html end |
#toc ⇒ Object
35 36 37 |
# File 'lib/table_of_contents/parser.rb', line 35 def toc build_toc + inject_anchors_into_html end |