Class: MaRuKu::MDElement

Inherits:
Object show all
Includes:
MaRuKu, Errors, Helpers, Out::HTML, Out::Latex, Out::Markdown, Strings, REXML
Defined in:
lib/maruku/structures.rb,
lib/maruku.rb,
lib/maruku/ext/div.rb,
lib/maruku/helpers.rb,
lib/maruku/defaults.rb,
lib/maruku/output/to_s.rb,
lib/maruku/ext/math/elements.rb,
lib/maruku/structures_inspect.rb,
lib/maruku/structures_iterators.rb

Overview

I did not want to have a class for each possible element. Instead I opted to have only the class “MDElement” that represents eveything in the document (paragraphs, headers, etc).

You can tell what it is by the variable ‘node_type`.

In the instance-variable ‘children` there are the children. These can be of class 1) String or 2) MDElement.

The @doc variable points to the document to which the MDElement belongs (which is an instance of Maruku, subclass of MDElement).

Attributes are contained in the hash ‘attributes`. Keys are symbols (downcased, with spaces substituted by underscores)

For example, if you write in the source document.

Title: test document
My property: value

content content

You can access ‘value` by writing:

@doc.attributes[:my_property] # => 'value'

from whichever MDElement in the hierarchy.

Direct Known Subclasses

MDDocument

Constant Summary

Constants included from Strings

Strings::Abbreviation, Strings::AttributeDefinitionList, Strings::Definition, Strings::EMailAddress, Strings::FootnoteText, Strings::HeaderWithAttributes, Strings::HeaderWithId, Strings::IncompleteLink, Strings::InlineAttributeList, Strings::LinkRegex, Strings::MightBeTableHeader, Strings::Sep, Strings::TabSize, Strings::TableSeparator

Constants included from Out::Latex

Out::Latex::ENTITY_TABLE, Out::Latex::SAFE_CHARS, Out::Latex::TexHeaders, Out::Latex::XML_TABLE

Constants included from Out::HTML

Out::HTML::HTML4Attributes, Out::HTML::METAS, Out::HTML::Xhtml10strict, Out::HTML::Xhtml11_mathml2_svg11, Out::HTML::Xhtml11strict_mathml2

Constants included from Out::Markdown

Out::Markdown::DefaultLineLength

Constants included from MaRuKu

Globals, MagicChar, MarukuURL, S5_Fancy, S5_external, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#create_frame, #describe_error, #maruku_error, #maruku_recover, #raise_error, #tell_user

Methods included from Helpers

#md_abbr, #md_abbr_def, #md_ald, #md_br, #md_code, #md_codeblock, #md_el, #md_em, #md_email, #md_emstrong, #md_entity, #md_foot_ref, #md_footnote, #md_header, #md_hrule, #md_html, #md_ial, #md_im_image, #md_im_link, #md_image, #md_li, #md_link, #md_par, #md_quote, #md_ref_def, #md_strong, #md_url, #md_xml_instr

Methods included from Strings

#add_tabs, #dbg_describe_ary, #force_linebreak?, #line_md_type, #normalize_key_and_value, #num_leading_hashes, #number_of_leading_spaces, #parse_email_headers, #sanitize_ref_id, #spaces_before_first_char, #split_lines, #strip_hashes, #strip_indent, #unquote

Methods included from Out::Latex

#array_to_latex, #children_to_latex, init_entity_table, #latex_color, #latex_escape, need_entity_table, #to_latex_abbr, #to_latex_cell, #to_latex_code, #to_latex_definition, #to_latex_definition_list, #to_latex_div, #to_latex_divref, #to_latex_email_address, #to_latex_emphasis, #to_latex_entity, #to_latex_eqref, #to_latex_equation, #to_latex_footnote_reference, #to_latex_head_cell, #to_latex_header, #to_latex_hrule, #to_latex_im_link, #to_latex_image, #to_latex_immediate_link, #to_latex_inline_code, #to_latex_inline_math, #to_latex_li, #to_latex_li_span, #to_latex_linebreak, #to_latex_link, #to_latex_ol, #to_latex_paragraph, #to_latex_quote, #to_latex_raw_html, #to_latex_strong, #to_latex_table, #to_latex_ul, #wrap_as_environment, #wrap_as_span

Methods included from Out::HTML

#add_class_to, #add_class_to_link, #add_css_to, #add_ws, #adjust_png, #array_to_html, #children_to_html, #convert_to_mathml_blahtex, #convert_to_mathml_itex2mml, #convert_to_mathml_none, #convert_to_mathml_ritex, #convert_to_png_blahtex, #convert_to_png_none, #create_html_element, #day_suffix, #maruku_html_signature, #nice_date, #obfuscate, #pixels_per_ex, #render_footnotes, #render_mathml, #render_png, #render_section_number, #section_number, #source2html, #to_html, #to_html_abbr, #to_html_cell, #to_html_code, #to_html_code_using_pre, #to_html_definition, #to_html_definition_data, #to_html_definition_list, #to_html_definition_term, #to_html_div, #to_html_divref, #to_html_document, #to_html_document_tree, #to_html_email_address, #to_html_emphasis, #to_html_entity, #to_html_eqref, #to_html_equation, #to_html_footnote_reference, #to_html_head_cell, #to_html_header, #to_html_hrule, #to_html_im_image, #to_html_im_link, #to_html_image, #to_html_immediate_link, #to_html_inline_code, #to_html_inline_math, #to_html_li, #to_html_li_span, #to_html_linebreak, #to_html_link, #to_html_ol, #to_html_paragraph, #to_html_quote, #to_html_raw_html, #to_html_ref_definition, #to_html_strong, #to_html_table, #to_html_tree, #to_html_ul, #to_html_xml_instr, #to_latex_ref_definition, #wrap_as_element, #xml_newline

Methods included from Out::Markdown

#add_indent, #array_to_md, #children_to_md, #to_md, #to_md_abbr_def, #to_md_li_span, #to_md_ol, #to_md_paragraph, #to_md_ul, #wrap

Methods included from MaRuKu

#markdown_extra?, #new_meta_data?, textile2

Constructor Details

#initialize(node_type = :unset, children = [], meta = {}, al = MaRuKu::AttributeList.new) ⇒ MDElement

Returns a new instance of MDElement.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/maruku/structures.rb', line 95

def initialize(node_type=:unset, children=[], meta={}, 
		al=MaRuKu::AttributeList.new )
	super(); 
	self.children = children
	self.node_type = node_type
	
	@attributes = {}
	
	meta.each do |symbol, value|
		self.instance_eval "
		  def #{symbol}; @#{symbol}; end
		  def #{symbol}=(val); @#{symbol}=val; end"
		self.send "#{symbol}=", value
	end
	
	self.al = al || AttributeList.new

	self.meta_priv = meta
end

Instance Attribute Details

#docObject

Reference of the document (which is of class Maruku)



93
94
95
# File 'lib/maruku/structures.rb', line 93

def doc
  @doc
end

#meta_privObject

Returns the value of attribute meta_priv.



115
116
117
# File 'lib/maruku/structures.rb', line 115

def meta_priv
  @meta_priv
end

Instance Method Details

#==(o) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/maruku/structures.rb', line 117

def ==(o)
	ok = o.kind_of?(MDElement) &&
	(self.node_type == o.node_type) &&
	(self.meta_priv == o.meta_priv) &&
	(self.children == o.children)
	
	if not ok
#			puts "This:\n"+self.inspect+"\nis different from\n"+o.inspect+"\n\n"
	end
	ok
end

#children_inspect(compact = true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/maruku/structures_inspect.rb', line 71

def children_inspect(compact=true)
	s = @children.inspect_more(compact,', ')
	if @children.empty?
		"[]"
	elsif s.size < 70
		s
	else
		"[\n"+
		add_tabs(@children.inspect_more(compact,",\n",false))+
		"\n]"
	end
end

#children_to_sObject



31
32
33
# File 'lib/maruku/output/to_s.rb', line 31

def children_to_s
	@children.join
end

#each_element(e_node_type = nil, &block) ⇒ Object

Yields to each element of specified node_type All elements if e_node_type is nil.



28
29
30
31
32
33
34
35
36
37
# File 'lib/maruku/structures_iterators.rb', line 28

def each_element(e_node_type=nil, &block) 
	@children.each do |c| 
		if c.kind_of? MDElement
			if (not e_node_type) || (e_node_type == c.node_type)
				block.call c
			end
			c.each_element(e_node_type, &block)
		end
	end
end

#generate_idObject

Generate an id for headers. Assumes @children is set.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/maruku/output/to_s.rb', line 36

def generate_id
	
	title = children_to_s
	title.gsub!(/ /,'_')
	title.downcase!
	title.gsub!(/[^\w_]/,'')
	title.strip!
	
	if title.size == 0
		$uid ||= 0
		$uid += 1
		title = "id#{$uid}"
	end

	# random is a very bad idea
#		title << "_" + rand(10000).to_s
	title
end

#get_setting(sym) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/maruku/defaults.rb', line 57

def get_setting(sym)
	if self.attributes.has_key?(sym) then
		return self.attributes[sym]
	elsif self.doc && self.doc.attributes.has_key?(sym) then
		return self.doc.attributes[sym]
	elsif MaRuKu::Globals.has_key?(sym)
		return MaRuKu::Globals[sym]
	else
		$stderr.puts "Bug: no default for #{sym.inspect}"
		nil
	end
end

#inspect(compact = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maruku/structures_inspect.rb', line 56

def inspect(compact=true)
	if compact
		i2 = inspect2
		return i2 if i2
	end
	
	"md_el(:%s,%s,%s,%s)" %
	[
		self.node_type,
		children_inspect(compact), 
		@meta_priv.inspect_ordered,
		self.al.inspect
	]
end

#inspect2Object

outputs abbreviated form (this should be eval()uable to get the document)



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/maruku/helpers.rb', line 195

def inspect2 
	s = 
	case @node_type
	when :paragraph
		"md_par(%s)" % children_inspect
	when :footnote_reference
		"md_foot_ref(%s)" % self.footnote_id.inspect
	when :entity
		"md_entity(%s)" % self.entity_name.inspect
	when :email_address
		"md_email(%s)" % self.email.inspect
	when :inline_code
		"md_code(%s)" % self.raw_code.inspect
	when :raw_html
		"md_html(%s)" % self.raw_html.inspect
	when :emphasis 
		"md_em(%s)" % children_inspect
	when :strong
		"md_strong(%s)" % children_inspect
	when :immediate_link
		"md_url(%s)" % self.url.inspect
	when :image
		"md_image(%s, %s)" % [
			children_inspect, 
			self.ref_id.inspect]
	when :im_image
		"md_im_image(%s, %s, %s)" % [
			children_inspect, 
			self.url.inspect,
			self.title.inspect]
	when :link
			"md_link(%s,%s)" % [
				children_inspect, self.ref_id.inspect]
	when :im_link
			"md_im_link(%s, %s, %s)" % [
				children_inspect, 
				self.url.inspect,
				self.title.inspect,
			]
	when :ref_definition
		"md_ref_def(%s, %s, %s)" % [
				self.ref_id.inspect, 
				self.url.inspect,
				self.title.inspect
			]
	when :ial
		"md_ial(%s)" % self.ial.inspect
	else
		return nil
	end
	if @al and not @al.empty? then 
		s = s.chop + ", #{@al.inspect})"
	end
	s
end

#md_div(children, al = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/maruku/ext/div.rb', line 100

def md_div(children, al=nil)
	type = label = num = nil
	doc.refid2ref ||= {}
	if al
		al.each do |k, v|
			case k
			when :class
				type = $1 if v =~ /^num_(\w*)/
       	                when :id
				label = v
			end
		end
	end
	if type
		doc.refid2ref[type] ||= {}
		num = doc.refid2ref[type].length + 1 || 1
	end
	e = self.md_el(:div, children, meta={:label => label, :type => type, :num => num}, al)
	if type && label
		doc.refid2ref[type].update({label => e})
	end
	e
end

#md_equation(math, label, numerate) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/maruku/ext/math/elements.rb', line 7

def md_equation(math, label, numerate)
	reglabel= /\\label\{(\w+)\}/
	if math =~ reglabel
		label = $1
		math.gsub!(reglabel,'')
	end
#		puts "Found label = #{label} math #{math.inspect} "
	num = nil
	if (label || numerate) && @doc #take number
		@doc.eqid2eq ||= {}	
		num = @doc.eqid2eq.size + 1
		label = "eq#{num}" if not label      # FIXME do id for document
	end
	e = self.md_el(:equation, [], meta={:math=>math, :label=>label,:num=>num})
	if label && @doc #take number
		@doc.eqid2eq[label] = e
	end
	e
end

#md_inline_math(math) ⇒ Object



3
4
5
# File 'lib/maruku/ext/math/elements.rb', line 3

def md_inline_math(math)
	self.md_el(:inline_math, [], meta={:math=>math})
end

#replace_each_string(&block) ⇒ Object

Apply passed block to each String in the hierarchy.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/maruku/structures_iterators.rb', line 40

def replace_each_string(&block)
	for c in @children
		if c.kind_of? MDElement
			c.replace_each_string(&block)
		end
	end

	processed = []
	until @children.empty?
		c = @children.shift
		if c.kind_of? String
			result = block.call(c)
			[*result].each do |e| processed << e end
		else
			processed << c
		end
	end
	@children = processed
end

#to_sObject

Strips all formatting from the string



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

def to_s
	 children_to_s
end