Class: Inkmark::Toc

Inherits:
Object
  • Object
show all
Defined in:
lib/inkmark/toc.rb

Overview

A rendered table of contents, carrying both Markdown and HTML renderings. Returned by #toc when the toc: true, statistics: true, or extract: { headings: true } option is set; nil otherwise.

Immutable value object: the instance is frozen at construction, and == / eql? / hash implement value-equality over the two fields.

Examples:

g = Inkmark.new(source, options: { toc: true })
g.toc.to_markdown  # => "- [Intro](#intro)\n  - [Goals](#goals)\n"
g.toc.to_html      # => "<ul>\n<li><a href=\"#intro\">...</a>..."
g.toc.to_s         # => same as to_markdown (String coercion)
puts g.toc         # prints the markdown form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown:, html:) ⇒ Toc

Returns a new instance of Toc.



23
24
25
26
27
# File 'lib/inkmark/toc.rb', line 23

def initialize(markdown:, html:)
  @markdown = markdown
  @html = html
  freeze
end

Instance Attribute Details

#htmlObject (readonly) Also known as: to_html

Returns the value of attribute html.



19
20
21
# File 'lib/inkmark/toc.rb', line 19

def html
  @html
end

#markdownObject (readonly) Also known as: to_markdown

Returns the value of attribute markdown.



19
20
21
# File 'lib/inkmark/toc.rb', line 19

def markdown
  @markdown
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



31
32
33
# File 'lib/inkmark/toc.rb', line 31

def ==(other)
  other.is_a?(Toc) && other.markdown == @markdown && other.html == @html
end

#hashObject



36
37
38
# File 'lib/inkmark/toc.rb', line 36

def hash
  [self.class, @markdown, @html].hash
end

#to_sObject



29
# File 'lib/inkmark/toc.rb', line 29

def to_s = @markdown