Class: Jekyll::TableOfContents::Parser
- Inherits:
-
Object
- Object
- Jekyll::TableOfContents::Parser
- Defined in:
- lib/jekyll-toc.rb
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.
10 11 12 13 |
# File 'lib/jekyll-toc.rb', line 10 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.
8 9 10 |
# File 'lib/jekyll-toc.rb', line 8 def doc @doc end |
Instance Method Details
#build_toc ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/jekyll-toc.rb', line 15 def build_toc toc = %Q{<ul class="section-nav">\n} @entries.each do |entry| toc << %Q{<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a></li>\n} end toc << '</ul>' end |
#inject_anchors_into_html ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/jekyll-toc.rb', line 25 def inject_anchors_into_html @entries.each do |entry| entry[:content_node].add_previous_sibling(%Q{<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
33 34 35 |
# File 'lib/jekyll-toc.rb', line 33 def toc build_toc + inject_anchors_into_html end |