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- DEFAULT_CONFIG =
{ "min_level" => 1, "max_level" => 6, }
Instance Method Summary collapse
- #build_toc ⇒ Object
-
#initialize(html, options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #inject_anchors_into_html ⇒ Object
- #toc ⇒ Object
Constructor Details
#initialize(html, options = {}) ⇒ Parser
Returns a new instance of Parser.
12 13 14 15 16 17 |
# File 'lib/table_of_contents/parser.rb', line 12 def initialize(html, = {}) @doc = Nokogiri::HTML::DocumentFragment.parse(html) = DEFAULT_CONFIG.merge() @toc_levels = ["min_level"]..["max_level"] @entries = parse_content end |
Instance Method Details
#build_toc ⇒ Object
19 20 21 |
# File 'lib/table_of_contents/parser.rb', line 19 def build_toc %(<ul class="section-nav">\n#{build_toc_list(@entries, last_ul_used: true)}</ul>) end |
#inject_anchors_into_html ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/table_of_contents/parser.rb', line 23 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
31 32 33 |
# File 'lib/table_of_contents/parser.rb', line 31 def toc build_toc + inject_anchors_into_html end |