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.
Reference of the document (which is of class Maruku)
# 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
# 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
# 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
# File lib/maruku/output/to_s.rb, line 31 def children_to_s @children.join end
Yields to each element of specified node_type All elements if e_node_type is nil.
# 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 an id for headers. Assumes @children is set.
# File lib/maruku/output/to_s.rb, line 36 def generate_id title = children_to_s title.gsub!(%r /,'_') title.downcase! title.gsub!(%r[^\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
# 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
# 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
outputs abbreviated form (this should be eval()uable to get the document)
# 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
# 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 =~ %r^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
# File lib/maruku/ext/math/elements.rb, line 7 def md_equation(math, label, numerate) reglabel= %r\\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
# File lib/maruku/ext/math/elements.rb, line 3 def md_inline_math(math) self.md_el(:inline_math, [], meta={:math=>math}) end
Apply passed block to each String in the hierarchy.
# 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
Strips all formatting from the string
# File lib/maruku/output/to_s.rb, line 27 def to_s children_to_s end