Class: TracWiki::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/trac-wiki/tree.rb

Constant Summary collapse

TAGS_APPEND_NL =
[:div, :p, :li, :ol, :ul, :dl, :table, :tr, :td , :th]
TAGS_FORCE_PAIR =
[:a, :td, :h1, :h2, :h3, :h4, :h5, :h6, :div, :script, :i]
TAGS_ALLOVED =
[:a,
                :button,
                :h1, :h2, :h3, :h4, :h5, :h6,
                :div, :span, :p, :pre,
                :li, :ul, :ol, :dl, :dt, :dd,
                :b, :tt, :u, :del, :blockquote, :strong, :em, :sup, :sub, :i,
                :table,  :tr, :td, :th,
                :br , :img, :hr,
                :form, :textarea, :input, :select, :option, :label,
]
TAGS_SKIP_EMPTY =
[ :p , :ol, :li, :strong, :em  ]
ATTRIBUTES_ALLOWED =
{ :form  =>  [:action, :method],
 :input =>  [:size, :type, :value, :name],
 :textarea => [:name, :rows, :cols],
 :select => [:multiple, :name],
 :option => [:disabled, :selected, :label, :value, :name],
 :a     =>  [:name, :href],
 :img   =>  [:src, :width, :height, :align, :valign, :style, :alt, :title],
 :td    =>  [:colspan, :rowspan, :style],
 :th    =>  [:colspan, :rowspan, :style],
 :ol    =>  [:type ],
 :label =>  [:for],
 :_all  =>  [:class, :title, :id, :style],
}
ATTRIBUTE_DATA_REX =
/\Adata-[-a-z]+\Z/i
ATTRIBUTE_STYLE_REX =
/\A( text-align:(center|right|left) |
   margin:    \d+(px|em)? |
   (background|color):    [-_#a-zA-Z0-9]+ |
   [\s;]
)+\Z/x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTree

Returns a new instance of Tree.



34
35
36
37
# File 'lib/trac-wiki/tree.rb', line 34

def initialize
  @root = Node.new(nil)
  @cur = @root
end

Instance Attribute Details

#curObject

Returns the value of attribute cur.



32
33
34
# File 'lib/trac-wiki/tree.rb', line 32

def cur
  @cur
end

Instance Method Details

#add(cont) ⇒ Object



89
90
91
92
# File 'lib/trac-wiki/tree.rb', line 89

def add(cont)
  @cur.add(cont)
  self
end

#add_raw(cont) ⇒ Object



94
95
96
97
98
# File 'lib/trac-wiki/tree.rb', line 94

def add_raw(cont)
  #cont_san = Sanitize.clean(cont, san_conf)
  @cur.add(RawHtml.new(cont))
  self
end

#add_spcObject

add space if needed



79
80
81
82
83
84
85
86
87
# File 'lib/trac-wiki/tree.rb', line 79

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) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/trac-wiki/tree.rb', line 176

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) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/trac-wiki/tree.rb', line 191

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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/trac-wiki/tree.rb', line 101

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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/trac-wiki/tree.rb', line 39

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) ⇒ Object



50
51
52
53
54
55
# File 'lib/trac-wiki/tree.rb', line 50

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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/trac-wiki/tree.rb', line 57

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_htmlObject



112
113
114
115
# File 'lib/trac-wiki/tree.rb', line 112

def to_html
  ret = tree_to_html(@root)
  ret
end

#tree_to_html(node) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/trac-wiki/tree.rb', line 116

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