Class: MaRuKu::MDElement

Inherits:
Object show all
Includes:
MaRuKu, Errors, Helpers, Out::Ansi, Out::HTML, Out::Markdown, Strings, REXML
Defined in:
lib/amp-front/third_party/maruku/structures.rb,
lib/amp-front/third_party/maruku.rb,
lib/amp-front/third_party/maruku/helpers.rb,
lib/amp-front/third_party/maruku/defaults.rb,
lib/amp-front/third_party/maruku/output/to_s.rb,
lib/amp-front/third_party/maruku/structures_inspect.rb,
lib/amp-front/third_party/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::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, 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::Ansi

#array_to_ansi, #children_to_ansi, #latex_color, #to_ansi_abbr, #to_ansi_code, #to_ansi_email_address, #to_ansi_emphasis, #to_ansi_header, #to_ansi_hrule, #to_ansi_im_link, #to_ansi_immediate_link, #to_ansi_inline_code, #to_ansi_li, #to_ansi_li_span, #to_ansi_linebreak, #to_ansi_link, #to_ansi_ol, #to_ansi_paragraph, #to_ansi_quote, #to_ansi_raw_html, #to_ansi_strong, #to_ansi_ul, #wrap_as_span

Methods included from Out::HTML

#add_class_to, #add_class_to_link, #add_css_to, #add_ws, #array_to_html, #children_to_html, #create_html_element, #day_suffix, #maruku_html_signature, #nice_date, #obfuscate, #render_footnotes, #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_document, #to_html_document_tree, #to_html_email_address, #to_html_emphasis, #to_html_entity, #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_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/amp-front/third_party/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/amp-front/third_party/maruku/structures.rb', line 93

def doc
  @doc
end

#meta_privObject

Returns the value of attribute meta_priv.



115
116
117
# File 'lib/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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/amp-front/third_party/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

#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/amp-front/third_party/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/amp-front/third_party/maruku/output/to_s.rb', line 27

def to_s
	 children_to_s
end