Module: XMLhelper

Included in:
Rexle, Rexle::Element
Defined in:
lib/rexle.rb

Instance Method Summary collapse

Instance Method Details

#doc_pretty_print(children, declaration = true) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rexle.rb', line 118

def doc_pretty_print(children, declaration=true)

  body = pretty_print(children,2).join

  a = self.root.attributes.to_a.map do |k,v| 
    "%s='%s'" % [k,(v.is_a?(Array) ? v.join(' ') : v)]
  end
  
  ind = "\n  "   
  xml = "<%s%s>%s%s%s</%s>" % [self.root.name, a.empty? ? '' : \
    ' ' + a.join(' '), ind, body, "\n", self.root.name]

  if self.instructions and declaration then
    processing_instructions("") + xml
  else 
    xml
  end
end

#doc_print(children, declaration = true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rexle.rb', line 98

def doc_print(children, declaration=true)

  body = (children.nil? or children.empty? \
         or children.is_an_empty_string? ) ? '' : \
                        scan_print(children).join.force_encoding("utf-8")

  a = self.root.attributes.to_a.map do |k,v|
    "%s='%s'" % [k,(v.is_a?(Array) ? v.join(' ') : v.to_s)]
  end

  xml = "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : \
    ' ' + a.join(' '), body, self.root.name]

  if self.instructions and declaration then
    processing_instructions() + xml
  else 
    xml
  end
end

#inspectObject



137
138
139
# File 'lib/rexle.rb', line 137

def inspect()    
  "#<Rexle:%s>" % [self.object_id]
end

#pretty_print(nodes, indent = '0') ⇒ Object



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
250
251
# File 'lib/rexle.rb', line 207

def pretty_print(nodes, indent='0')

  indent = indent.to_i
  return '' unless nodes

  nodes.select(){|x| x.to_s.strip.length > 0}
      .map.with_index do |x, i|

    if x.is_a? Rexle::Element then

      a = x.attributes.to_a.map do |k,v| 
        "%s='%s'" % [k,(v.is_a?(Array) ? v.join(' ') : v)]
      end
      a ||= []

      tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
      start = i > 0 ? ("\n" + '  ' * (indent - 1)) : ''          

      if (x.value and x.value.length > 0) \
          or (x.children and x.children.length > 0 \
          and not x.children.is_an_empty_string?) or \
            x.name == 'script' or x.name == 'textarea' or \
                                                x.name == 'iframe' then

        ind1 = (x.children and x.children.grep(Rexle::Element).length > 0) ? 
          ("\n" + '  ' * indent) : ''
          
        out = ["%s<%s>%s" % [start, tag, ind1]]
        out << pretty_print(x.children, (indent + 1).to_s.clone) 
        ind2 = (ind1 and ind1.length > 0) ? ("\n" + '  ' * (indent - 1)) : ''
        out << "%s</%s>" % [ind2, x.name]            
      else

        out = ["%s<%s/>" % [start, tag]]
      end


    elsif x.is_a? String then  x.sub(/^[\n\s]+$/,'')
    elsif x.is_a? Rexle::CData then x.print        
    elsif x.is_a? Rexle::Comment then x.print           

    end
  end

end

#processing_instructions(s = '') ⇒ Object



141
142
143
144
145
# File 'lib/rexle.rb', line 141

def processing_instructions(s='')
  self.instructions.map do |instruction|
    "<?%s?>\n" % instruction.join(' ') 
  end.join s
end

#scan_print(nodes) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rexle.rb', line 147

def scan_print(nodes)

  r2 = nodes.map do |x|
    
    r = if x.is_a? Rexle::Element then

      a = x.attributes.to_a.map do |k,v| 
        "%s='%s'" % [k,(v.is_a?(Array) ? v.join(' ') : v)]
      end

      tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
      
      non_self_closing_tags = %w(script textarea iframe div)

      if  (x.children and x.children.length > 0 \
          and not x.children.is_an_empty_string?) or \
            non_self_closing_tags.include? x.name then

        out = ["<%s>" % tag]
        out << scan_print(x.children)

        out << "</%s>" % x.name
      else
        out = ["<%s/>" % tag]
      end
    
    elsif x.is_a? String then  x
    elsif x.is_a? Rexle::CData then x.print        
    elsif x.is_a? Rexle::Comment then x.print        
      
    end

    r
  end
  
  r2

end

#scan_to_a(nodes) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rexle.rb', line 186

def scan_to_a(nodes)

  nodes.inject([]) do |r,x|

    if x.is_a? Rexle::Element then

      a = [String.new(x.name), Hash.new(x.attributes), x.value.to_s]

      (a.concat(scan_to_a(x.children))) if x.children.length > 1
      r << a
    elsif x.is_a? String then

      r << String.new(x)
    end

  end

end