Class: MaRuKu::Section

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

Overview

A section in the table of contents of a document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSection

Returns a new instance of Section.



34
35
36
37
# File 'lib/maruku/toc.rb', line 34

def initialize
  @immediate_children = []
  @section_children = []
end

Instance Attribute Details

#header_elementMDElement

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

Returns:



20
21
22
# File 'lib/maruku/toc.rb', line 20

def header_element
  @header_element
end

#immediate_childrenArray<MDElement>

TODO:

Why does this never contain Strings?

The immediate child nodes of this section.

Returns:



27
28
29
# File 'lib/maruku/toc.rb', line 27

def immediate_children
  @immediate_children
end

#section_childrenArray<Section>

The subsections of this section.

Returns:



32
33
34
# File 'lib/maruku/toc.rb', line 32

def section_children
  @section_children
end

#section_levelFixnum

The depth of the section (0 for toplevel).

Equivalent to ‘header_element.level`.

Returns:

  • (Fixnum)


9
10
11
# File 'lib/maruku/toc.rb', line 9

def section_level
  @section_level
end

#section_numberArray<Fixnum>

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

Returns:



14
15
16
# File 'lib/maruku/toc.rb', line 14

def section_number
  @section_number
end

Instance Method Details

#inspect(indent = 1) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#numerateObject

Assign 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.



61
62
63
64
65
66
67
# 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_htmlObject

Returns an HTML representation of the table of contents.

This should only be called on the root section.



73
74
75
# File 'lib/maruku/toc.rb', line 73

def to_html
  MaRuKu::Out::HTML::HTMLElement.new('div', { 'class' => 'maruku_toc' }, _to_html)
end

#to_latexObject

Returns a LaTeX representation of the table of contents.

This should only be called on the root section.



80
81
82
# File 'lib/maruku/toc.rb', line 80

def to_latex
  _to_latex + "\n\n"
end