Class: Wunderbar::XmlMarkup

Inherits:
Object
  • Object
show all
Defined in:
lib/wunderbar/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ XmlMarkup

Returns a new instance of XmlMarkup.



82
83
84
85
# File 'lib/wunderbar/builder.rb', line 82

def initialize(args)
  @_scope = args.delete(:scope)
  @_builder = SpacedMarkup.new(args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

forward to Wunderbar, XmlMarkup, or @_scope



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wunderbar/builder.rb', line 88

def method_missing(method, *args, &block)
  if Wunderbar.respond_to? method
    Wunderbar.send method, *args, &block
  elsif SpacedMarkup.public_instance_methods.include? method
    @_builder.__send__ method, *args, &block
  elsif SpacedMarkup.public_instance_methods.include? method.to_s
    @_builder.__send__ method, *args, &block
  elsif @_scope and @_scope.respond_to? method
    @_scope.send method, *args, &block
  else
    super
  end
end

Instance Method Details

#<<(data) ⇒ Object

insert verbatim



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/wunderbar/builder.rb', line 195

def <<(data)
  if not String === data or data.include? '<' or data.include? '&'
    require 'nokogiri'
    data = Nokogiri::HTML::fragment(data.to_s).to_xml
  end

  # fix CDATA in most cases (notably scripts)
  data.gsub!(/<!\[CDATA\[(.*?)\]\]>/m) do |cdata|
    if $1.include? '<' or $1.include? '&'
      "//<![CDATA[\n#{$1}\n//]]>"
    else
      $1
    end
  end

  # fix CDATA for style elements
  data.gsub!(/<style([^>])*>\/\/<!\[CDATA\[\n(.*?)\s+\/\/\]\]>/m) do |cdata|
    if $2.include? '<' or $2.include? '&'
      "<style#{$1}>/*<![CDATA[*/\n#{$2.gsub("\n\Z",'')}\n/*]]>*/"
    else
      $1
    end
  end

  @_builder << data
rescue LoadError
  @_builder << data
end

#comment(*args) ⇒ Object

comment



190
191
192
# File 'lib/wunderbar/builder.rb', line 190

def comment(*args)
  @_builder.comment! *args
end

#declare(*args) ⇒ Object

declaration (DOCTYPE, etc)



185
186
187
# File 'lib/wunderbar/builder.rb', line 185

def declare(*args)
  @_builder.declare!(*args)
end

#methodsObject



102
103
104
105
106
107
# File 'lib/wunderbar/builder.rb', line 102

def methods
  result = super + Wunderbar.methods
  result += SpacedMarkup.public_instance_methods
  result += @_scope.methods if @_scope
  result.uniq
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/wunderbar/builder.rb', line 109

def respond_to?(method)
  respond true if Wunderbar.respond_to? method
  respond true if SpacedMarkup.public_instance_methods.include? method
  respond true if SpacedMarkup.public_instance_methods.include?  method.to_s
  respond true if @_scope and @_scope.respond_to? method?
  super
end

#system(command, opts = {}) ⇒ Object

execute a system command, echoing stdin, stdout, and stderr



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
# File 'lib/wunderbar/builder.rb', line 127

def system(command, opts={})
  if command.respond_to? :join
    begin
      # if available, use escape as it does prettier quoting
      require 'escape'
      command = Escape.shell_command(command).untaint
    rescue LoadError
      # std-lib function that gets the job done
      require 'shellwords'
      command = Shellwords.join(command).untaint
    end
  end

  require 'open3'
  tag  = opts[:tag]  || 'pre'
  output_class = opts[:class] || {}
  stdin  = output_class[:stdin]  || '_stdin'
  stdout = output_class[:stdout] || '_stdout'
  stderr = output_class[:stderr] || '_stderr'

  @_builder.tag! tag, command, :class=>stdin unless opts[:echo] == false

  require 'thread'
  semaphore = Mutex.new
  Open3.popen3(command) do |pin, pout, perr|
    [
      Thread.new do
        until pout.eof?
          out_line = pout.readline.chomp
          semaphore.synchronize do
            @_builder.tag! tag, out_line, :class=>stdout
          end
        end
      end,

      Thread.new do
        until perr.eof?
          err_line = perr.readline.chomp
          semaphore.synchronize do 
            @_builder.tag! tag, err_line, :class=>stderr
          end
        end
      end,

      Thread.new do
        if opts[:stdin].respond_to? :read
          require 'fileutils'
          FileUtils.copy_stream opts[:stdin], pin
        elsif opts[:stdin]
          pin.write opts[:stdin].to_s
        end
        pin.close
      end
    ].each {|thread| thread.join}
  end
end

#tag!(sym, *args, &block) ⇒ Object

avoid method_missing overhead for the most common case



118
119
120
121
122
123
124
# File 'lib/wunderbar/builder.rb', line 118

def tag!(sym, *args, &block)
  if !block and (args.empty? or args == [''])
    CssProxy.new(@_builder, @_builder.target!, sym, args)
  else
    @_builder.tag! sym, *args, &block
  end
end