class TracWiki::Tree

Constants

ATTRIBUTES_ALLOWED
ATTRIBUTE_DATA_REX
ATTRIBUTE_STYLE_REX
TAGS_ALLOVED
TAGS_APPEND_NL
TAGS_FORCE_PAIR
TAGS_SKIP_EMPTY

Public Class Methods

new() click to toggle source
# File lib/trac-wiki/tree.rb, line 33
def initialize
  @root = Node.new(nil)
  @cur = @root
end

Public Instance Methods

add(cont) click to toggle source
# File lib/trac-wiki/tree.rb, line 88
def add(cont)
  @cur.add(cont)
  self
end
add_raw(cont) click to toggle source
# File lib/trac-wiki/tree.rb, line 93
def add_raw(cont)
  #cont_san = Sanitize.clean(cont, san_conf)
  @cur.add(RawHtml.new(cont))
  self
end
add_spc() click to toggle source

add space if needed

# File lib/trac-wiki/tree.rb, line 78
def add_spc
  if @cur.cont.size > 0
    last = @cur.cont.last
    if last.is_a?(String) && last[-1] == ?\s
      return
    end
  end
  add(' ')
end
attrs_to_s(tag, attrs) click to toggle source
# File lib/trac-wiki/tree.rb, line 174
def attrs_to_s(tag, attrs)
  return '' if attrs.nil? || attrs.size == 0
  ret = ['']
  tag_attrs = ATTRIBUTES_ALLOWED[tag] || []
  attrs.each_pair do |k,v|
    #print "a: #{k} #{v}\n"
    next if v.nil?
    k_sym = k.to_sym
    next if ! ( ATTRIBUTES_ALLOWED[:_all].include?(k_sym) || tag_attrs.include?(k_sym) || k =~ ATTRIBUTE_DATA_REX )
    next if k_sym == :style && v !~ ATTRIBUTE_STYLE_REX
    ret.push "#{TracWiki::Parser.escapeHTML(k.to_s)}=\"#{TracWiki::Parser.escapeHTML(v.to_s)}\""
  end
  return ret.sort.join(' ')
end
cont_to_s(cont) click to toggle source
# File lib/trac-wiki/tree.rb, line 189
def cont_to_s(cont)
  cont = [cont] if cont.is_a? String
  cont.map do |c|
     if c.is_a? Node
        tree_to_html(c)
     elsif c.is_a? RawHtml
        c.to_s
     else
        TracWiki::Parser.escapeHTML(c.to_s)
     end
   end.join('')
 end
find_par(tag_name, node = nil) click to toggle source
# File lib/trac-wiki/tree.rb, line 100
def find_par(tag_name, node = nil)
  node = @cur if node.nil?
  while ! node.par.nil?
    if node.tag == tag_name
       return node.par
    end
    node = node.par
  end
  nil
end
tag(tag, attrs = nil, cont = nil) click to toggle source
# File lib/trac-wiki/tree.rb, line 38
def tag(tag, attrs = nil, cont = nil)
  if cont.nil? && ! attrs.is_a?(Hash)
    # tag(:b, "ahoj") -> tag(:b, {}, "ahoj")
    cont = attrs
    attrs = nil
  end
  cont = [ cont ] if cont.is_a? String
  @cur.add(Node.new(tag, @cur, attrs, cont))
  self
end
tag_beg(tag_name, attrs = nil, cont = nil) click to toggle source
# File lib/trac-wiki/tree.rb, line 49
def tag_beg(tag_name, attrs = nil, cont = nil)
  node = Node.new(tag_name, @cur, attrs, cont)
  @cur.add(node)
  @cur = node
  self
end
tag_end(tag_name) click to toggle source
# File lib/trac-wiki/tree.rb, line 56
      def tag_end(tag_name)
        c = @cur
        ts = tag_name.to_sym
        while c.tag != ts
          c = c.par
          if c.nil?
            return "no such tag in stack, ingoring "
          end
        end
        @cur = c.par
        self

#        if @cur.tag == tag_name.to_sym
#          @cur = @cur.par
#        else
#          #pp(@root)
#          raise "tag_end: cur tag is not <#{tag_name}>, but <#{@cur.tag}>"
#        end
#        self
      end
to_html() click to toggle source
# File lib/trac-wiki/tree.rb, line 111
def to_html
  ret = tree_to_html(@root)
  ret
end
tree_to_html(node) click to toggle source
# File lib/trac-wiki/tree.rb, line 115
def tree_to_html(node)
   tag = node.tag
   if tag.nil?
     return cont_to_s(node.cont)
   end

   nl = ''
   nl = "\n"  if TAGS_APPEND_NL.include? tag

   if ! TAGS_ALLOVED.include? tag
     return '' if node.cont.size == 0
     return cont_to_s(node.cont)
   end
   if node.cont.size == 0
     if TAGS_SKIP_EMPTY.include? tag
        return ''
     end
     if TAGS_FORCE_PAIR.include? tag
       return "<#{tag}#{attrs_to_s(tag, node.attrs)}></#{tag}>#{nl}"
     end
     return "<#{tag}#{attrs_to_s(tag, node.attrs)}/>#{nl}"
   end

   return "<#{tag}#{attrs_to_s(tag, node.attrs)}>#{cont_to_s(node.cont)}</#{tag}>#{nl}"
end