Class: Wunderbar::CDATANode

Inherits:
PreformattedNode show all
Defined in:
lib/wunderbar/node.rb

Direct Known Subclasses

ScriptNode, StyleNode

Constant Summary

Constants inherited from Node

Node::ESCAPE, Node::VOID

Instance Attribute Summary

Attributes inherited from Node

#attrs, #children, #name, #node, #parent, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PreformattedNode

#preserve_spaces?

Methods inherited from Node

#add_child, #initialize, #method_missing, #preserve_spaces?, #walk

Constructor Details

This class inherits a constructor from Wunderbar::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wunderbar::Node

Class Method Details

.normalize(data, indent = '') ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/wunderbar/node.rb', line 181

def self.normalize(data, indent='')
  data = data.sub(/\n\s*\Z/, '').sub(/\A\s*\n/, '')

  unindent = data.sub(/s+\Z/,'').scan(/^ *\S/).map(&:length).min || 0

  before  = ::Regexp.new('^'.ljust(unindent))
  node = @node
  data.gsub! before, indent
  data.gsub! /^#{indent}$/, '' if unindent == 0
  data
end

Instance Method Details

#serialize(options = {}, result = [], indent = '') ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/wunderbar/node.rb', line 193

def serialize(options = {}, result = [], indent='')
  if @text and @text.include? "\n"
    tindent = (indent ? "#{indent}#{options[:indent]}" : indent)
    children.unshift CDATANode.normalize(@text, tindent).rstrip
    @text = nil
  end

  if @text and @text =~ /[<^>]/
    indent += options[:indent] if indent
    children.unshift @text.gsub(/^/, indent).gsub(/^ +$/,'').rstrip
    @text = nil
    super(options.merge(pre: pre, post: post), result, indent)
  elsif children && children.any? {|node| String===node && node =~ /[<^>]/}
    super(options.merge(pre: pre, post: post), result, indent)
  else
    super
  end
end