Class: Kitabu::TOC::HTML
- Inherits:
-
Object
- Object
- Kitabu::TOC::HTML
- Defined in:
- lib/kitabu/toc/html.rb,
lib/kitabu/toc/html/stream.rb
Defined Under Namespace
Classes: Stream
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#content ⇒ Object
Returns the value of attribute content.
-
#toc ⇒ Object
readonly
Return the table of contents in hash format.
Class Method Summary collapse
-
.generate(content) ⇒ Object
Traverse every title normalizing its content as a permalink.
-
.normalize(content) ⇒ Object
Traverse every title and add an
id
attribute.
Instance Method Summary collapse
-
#initialize ⇒ HTML
constructor
:nodoc:.
-
#tag(node) ⇒ Object
:nodoc:.
-
#to_hash ⇒ Object
Return a hash with all normalized attributes.
-
#to_html ⇒ Object
Return the table of contents in HTML format.
Constructor Details
#initialize ⇒ HTML
:nodoc:
47 48 49 50 |
# File 'lib/kitabu/toc/html.rb', line 47 def initialize # :nodoc: @toc = [] @counters = {} end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
11 12 13 |
# File 'lib/kitabu/toc/html.rb', line 11 def attrs @attrs end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
11 12 13 |
# File 'lib/kitabu/toc/html.rb', line 11 def buffer @buffer end |
#content ⇒ Object
Returns the value of attribute content.
12 13 14 |
# File 'lib/kitabu/toc/html.rb', line 12 def content @content end |
#toc ⇒ Object (readonly)
Return the table of contents in hash format.
8 9 10 |
# File 'lib/kitabu/toc/html.rb', line 8 def toc @toc end |
Class Method Details
.generate(content) ⇒ Object
Traverse every title normalizing its content as a permalink.
39 40 41 42 43 44 45 |
# File 'lib/kitabu/toc/html.rb', line 39 def self.generate(content) content = normalize(content) listener = new listener.content = content Stream.new(content, listener).parse listener end |
.normalize(content) ⇒ Object
Traverse every title and add an id
attribute. Return the modified content.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kitabu/toc/html.rb', line 17 def self.normalize(content) counter = {} html = Nokogiri::HTML.parse(content) html.search("h1, h2, h3, h4, h5, h6").each do |tag| title = tag.inner_text permalink = title.to_permalink counter[permalink] ||= 0 counter[permalink] += 1 if counter[permalink] > 1 permalink = "#{permalink}-#{counter[permalink]}" end tag.set_attribute("id", permalink) end html.css("body").first.inner_html end |
Instance Method Details
#tag(node) ⇒ Object
:nodoc:
52 53 54 55 56 57 58 |
# File 'lib/kitabu/toc/html.rb', line 52 def tag(node) # :nodoc: toc << { level: node.name.gsub(/[^\d]/, "").to_i, text: node.text, permalink: node["id"] } end |
#to_hash ⇒ Object
Return a hash with all normalized attributes.
62 63 64 65 66 67 68 |
# File 'lib/kitabu/toc/html.rb', line 62 def to_hash { content:, html: to_html, toc: } end |
#to_html ⇒ Object
Return the table of contents in HTML format.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kitabu/toc/html.rb', line 72 def to_html buffer = toc.each_with_object([]) do |, html| html << %[ <div class="level#{[:level]} #{[:permalink]}"> <a href="##{[:permalink]}"> <span>#{CGI.escape_html([:text])}</span> </a> </div> ] end buffer.join end |