class MaRuKu::Section

Attributes

header_element[RW]

reference to header (header has h.meta to self)

immediate_children[RW]

Array of immediate children of this element

section_children[RW]

Array of Section inside this section

section_level[RW]

a Fixnum, is == header_element.level

section_number[RW]

An array of fixnum, like [1,2,5] for Section 1.2.5

Public Class Methods

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

Public Instance Methods

create_toc() click to toggle source
# File lib/maruku/toc.rb, line 90
def create_toc
        ul = Element.new 'ul'
        # let's remove the bullets
        ul.attributes['style'] = 'list-style: none;' 
        @section_children.each do |c|
                li = Element.new 'li'
                if span = c.header_element.render_section_number
                        li << span
                end
                a = c.header_element.wrap_as_element('a')
                        a.delete_attribute 'id'
                        a.attributes['href'] = "##{c.header_element.attributes[:id]}"
                li << a
                li << c.create_toc if c.section_children.size>0
                ul << li
        end
        ul
end
inspect(indent=1) click to toggle source
# File lib/maruku/toc.rb, line 53
def inspect(indent=1)
        s = ""
        if @header_element
                s +=  "\_"*indent +  "(#{@section_level})>\t #{@section_number.join('.')} : "
                s +=  @header_element.children_to_s +
                 " (id: '#{@header_element.attributes[:id]}')\n"
        else
                s += "Master\n"
        end
        
        @section_children.each do |c|
                s+=c.inspect(indent+1)
        end
        s
end
numerate(a=[]) click to toggle source

Numerate this section and its children

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

Creates an HTML toc. Call this on the root

# File lib/maruku/toc.rb, line 83
def to_html
        div = Element.new 'div'
        div.attributes['class'] = 'maruku_toc'
        div << create_toc
        div
end
to_latex() click to toggle source

Creates a latex toc. Call this on the root

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

        end
        s
end