Class: MaRuKu::Section

Inherits:
Object show all
Includes:
REXML
Defined in:
lib/maruku/toc.rb,
lib/maruku/toc.rb

Overview

This represents a section in the TOC.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSection

Returns a new instance of Section.



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

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

Instance Attribute Details

#header_elementObject

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



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

def header_element
  @header_element
end

#immediate_childrenObject

Array of immediate children of this element



41
42
43
# File 'lib/maruku/toc.rb', line 41

def immediate_children
  @immediate_children
end

#section_childrenObject

Array of Section inside this section



44
45
46
# File 'lib/maruku/toc.rb', line 44

def section_children
  @section_children
end

#section_levelObject

a Fixnum, is == header_element.level



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

def section_level
  @section_level
end

#section_numberObject

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



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

def section_number
  @section_number
end

Instance Method Details

#create_tocObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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 = []) ⇒ Object

Numerate this section and its children



70
71
72
73
74
75
76
77
78
# 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_htmlObject

Creates an HTML toc. Call this on the root



83
84
85
86
87
88
# 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_latexObject

Creates a latex toc. Call this on the root



111
112
113
# File 'lib/maruku/toc.rb', line 111

def to_latex
	to_latex_rec + "\n\n"
end

#to_latex_recObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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