Class: MaRuKu::Section

Inherits:
Object
  • Object
show all
Includes:
REXML
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.



53
54
55
56
# File 'lib/maruku/toc.rb', line 53

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:



39
40
41
# File 'lib/maruku/toc.rb', line 39

def header_element
  @header_element
end

#immediate_childrenArray<MDElement>

TODO:

Why does this never contain Strings?

The immediate child nodes of this section.

Returns:



46
47
48
# File 'lib/maruku/toc.rb', line 46

def immediate_children
  @immediate_children
end

#section_childrenArray<Section>

The subsections of this section.

Returns:



51
52
53
# File 'lib/maruku/toc.rb', line 51

def section_children
  @section_children
end

#section_levelFixnum

The depth of the section (0 for toplevel).

Equivalent to ‘header_element.level`.

Returns:

  • (Fixnum)


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

def section_level
  @section_level
end

#section_numberArray<Fixnum>

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

Returns:

  • (Array<Fixnum>)


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

def section_number
  @section_number
end

Instance Method Details

#inspect(indent = 1) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/maruku/toc.rb', line 58

def inspect(indent = 1)
  s = ""

  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.



82
83
84
85
86
87
88
# File 'lib/maruku/toc.rb', line 82

def numerate(a = [])
  self.section_number = a
  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.



95
96
97
98
99
100
# File 'lib/maruku/toc.rb', line 95

def to_html
  div = Element.new 'div'
  div.attributes['class'] = 'maruku_toc'
  div << _to_html
  div
end

#to_latexObject

Returns a LaTeX representation of the table of contents.

This should only be called on the root section.



105
106
107
# File 'lib/maruku/toc.rb', line 105

def to_latex
  _to_latex + "\n\n"
end