class MaRuKu::Section

A section in the table of contents of a document.

Attributes

header_element[RW]

The ‘:header` node for this section. The value of `meta` for the header will be this node.

@return [MDElement]

immediate_children[RW]

The immediate child nodes of this section.

@todo Why does this never contain Strings?

@return [Array<MDElement>]

section_children[RW]

The subsections of this section.

@return [Array<Section>]

section_level[RW]

The depth of the section (0 for toplevel).

Equivalent to ‘header_element.level`.

@return [Fixnum]

section_number[RW]

The nested section number, e.g. ‘[1, 2, 5]` for Section 1.2.5.

@return [Array<Fixnum>]

Public Class Methods

new() click to toggle source
# File lib/maruku/toc.rb, line 34
def initialize
  @immediate_children = []
  @section_children = []
end

Public Instance Methods

inspect(indent = 1) click to toggle source
# File lib/maruku/toc.rb, line 39
def inspect(indent = 1)
  if @header_element
    s = "\_" * indent <<
      "(#{@section_level})>\t #{@section_number.join('.')} : " <<
      @header_element.children_to_s <<
      " (id: '#{@header_element.attributes[:id]}')\n"
  else
    s = "Master\n"
  end
  @section_children.each {|c| s << c.inspect(indent + 1) }

  s
end
numerate(a = []) click to toggle source

Assign {#section_number section numbers} to this section and its children. This also assigns the section number attribute to the sections’ headers.

This should only be called on the root section.

@overload def numerate

# File lib/maruku/toc.rb, line 61
def numerate(a = [])
  self.section_number = a
  self.section_children.each_with_index {|c, i| c.numerate(a + [i + 1])}
  if h = self.header_element
    h.attributes[:section_number] = self.section_number
  end
end
to_html() click to toggle source

Returns an HTML representation of the table of contents.

This should only be called on the root section.

# File lib/maruku/toc.rb, line 73
def to_html
  MaRuKu::Out::HTML::HTMLElement.new('div', { 'class' => 'maruku_toc' }, _to_html)
end
to_latex() click to toggle source

Returns a LaTeX representation of the table of contents.

This should only be called on the root section.

# File lib/maruku/toc.rb, line 80
def to_latex
  _to_latex + "\n\n"
end

Protected Instance Methods

_to_html() click to toggle source
# File lib/maruku/toc.rb, line 86
def _to_html
  ul = MaRuKu::Out::HTML::HTMLElement.new('ul')
  @section_children.each do |c|
    li = MaRuKu::Out::HTML::HTMLElement.new('li')
    if span = c.header_element.render_section_number
      li << span
    end

    a = c.header_element.wrap_as_element('a')
    a.attributes.delete('id')
    a['href'] = "##{c.header_element.attributes[:id]}"

    li << a
    li << c._to_html if c.section_children.size > 0
    ul << li
  end
  ul
end
_to_latex() click to toggle source
# File lib/maruku/toc.rb, line 105
def _to_latex
  s = ""
  @section_children.each do |c|
    s << "\\noindent"
    if number = c.header_element.section_number
      s << number
    end
    id = c.header_element.attributes[:id]
    text = c.header_element.children_to_latex
    s << "\\hyperlink{#{id}}{#{text}}"
    s << "\\dotfill \\pageref*{#{id}} \\linebreak\n"
    s << c._to_latex if c.section_children.size > 0
  end
  s
end