In Files

Parent

Hpricot::Elem

@see Hpricot @private

Public Instance Methods

to_haml(tabs, options) click to toggle source

@see Haml::HTML::Node#to_haml

     # File lib/haml/html.rb, line 234
234:       def to_haml(tabs, options)
235:         return "" if converted_to_haml
236:         if name == "script" &&
237:             (attr_hash['type'].nil? || attr_hash['type'] == "text/javascript") &&
238:             (attr_hash.keys - ['type']).empty?
239:           return to_haml_filter(:javascript, tabs, options)
240:         elsif name == "style" &&
241:             (attr_hash['type'].nil? || attr_hash['type'] == "text/css") &&
242:             (attr_hash.keys - ['type']).empty?
243:           return to_haml_filter(:css, tabs, options)
244:         end
245: 
246:         output = tabulate(tabs)
247:         if options[:erb] && name[0...5] == 'haml:'
248:           case name[5..1]
249:           when "loud"
250:             lines = CGI.unescapeHTML(inner_text).split("\n").
251:               map {|s| s.rstrip}.reject {|s| s.strip.empty?}
252:             lines.first.gsub!(/^[ \t]*/, "= ")
253: 
254:             if lines.size > 1 # Multiline script block
255:               # Normalize the indentation so that the last line is the base
256:               indent_str = lines.last[/^[ \t]*/]
257:               indent_re = /^[ \t]{0,#{indent_str.count(" ") + 8 * indent_str.count("\t")}}/
258:               lines.map! {|s| s.gsub!(indent_re, '')}
259: 
260:               # Add an extra "  " to make it indented relative to "= "
261:               lines[1..1].each {|s| s.gsub!(/^/, "  ")}
262: 
263:               # Add | at the end, properly aligned
264:               length = lines.map {|s| s.size}.max + 1
265:               lines.map! {|s| "%#{-length}s|" % s}
266: 
267:               if next_sibling && next_sibling.is_a?(Hpricot::Elem) && next_sibling.name == "haml:loud" &&
268:                   next_sibling.inner_text.split("\n").reject {|s| s.strip.empty?}.size > 1
269:                 lines << "-#"
270:               end
271:             end
272:             return lines.map {|s| output + s + "\n"}.join
273:           when "silent"
274:             return CGI.unescapeHTML(inner_text).split("\n").map do |line|
275:               next "" if line.strip.empty?
276:               "#{output}- #{line.strip}\n"
277:             end.join
278:           when "block"
279:             return render_children("", tabs, options)
280:           end
281:         end
282: 
283:         if self.next && self.next.text? && self.next.content =~ /\A[^\s]/
284:           if self.previous.nil? || self.previous.text? &&
285:               (self.previous.content =~ /[^\s]\Z/ ||
286:                self.previous.content =~ /\A\s*\Z/ && self.previous.previous.nil?)
287:             nuke_outer_whitespace = true
288:           else
289:             output << "= succeed #{self.next.content.slice!(/\A[^\s]+/).dump} do\n"
290:             tabs += 1
291:             output << tabulate(tabs)
292:           end
293:         end
294: 
295:         output << "%#{name}" unless name == 'div' &&
296:           (static_id?(options) ||
297:            static_classname?(options) &&
298:            attr_hash['class'].split(' ').any?(&method(:haml_css_attr?)))
299: 
300:         if attr_hash
301:           if static_id?(options)
302:             output << "##{attr_hash['id']}"
303:             remove_attribute('id')
304:           end
305:           if static_classname?(options)
306:             leftover = attr_hash['class'].split(' ').reject do |c|
307:               next unless haml_css_attr?(c)
308:               output << ".#{c}"
309:             end
310:             remove_attribute('class')
311:             set_attribute('class', leftover.join(' ')) unless leftover.empty?
312:           end
313:           output << haml_attributes(options) if attr_hash.length > 0
314:         end
315: 
316:         output << ">" if nuke_outer_whitespace
317:         output << "/" if empty? && !etag
318: 
319:         if children && children.size == 1
320:           child = children.first
321:           if child.is_a?(::Hpricot::Text)
322:             if !child.to_s.include?("\n")
323:               text = child.to_haml(tabs + 1, options)
324:               return output + " " + text.lstrip.gsub(/^\\/, '') unless text.chomp.include?("\n")
325:               return output + "\n" + text
326:             elsif ["pre", "textarea"].include?(name) ||
327:                 (name == "code" && parent.is_a?(::Hpricot::Elem) && parent.name == "pre")
328:               return output + "\n#{tabulate(tabs + 1)}:preserve\n" +
329:                 innerText.gsub(/^/, tabulate(tabs + 2))
330:             end
331:           elsif child.is_a?(::Hpricot::Elem) && child.name == "haml:loud"
332:             return output + child.to_haml(tabs + 1, options).lstrip
333:           end
334:         end
335: 
336:         render_children(output + "\n", tabs, options)
337:       end

Private Instance Methods

dynamic_attribute?(name, options) click to toggle source
     # File lib/haml/html.rb, line 384
384:       def dynamic_attribute?(name, options)
385:         options[:erb] and dynamic_attributes.key?(name)
386:       end
dynamic_attributes() click to toggle source
     # File lib/haml/html.rb, line 347
347:       def dynamic_attributes
348:         @dynamic_attributes ||= begin
349:           Haml::Util.map_hash(attr_hash) do |name, value|
350:             next if value.empty?
351:             full_match = nil
352:             ruby_value = value.gsub(%{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
353:               full_match = $`.empty? && $'.empty?
354:               CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
355:             end
356:             next if ruby_value == value
357:             [name, full_match ? ruby_value : %("#{ruby_value}")]
358:           end
359:         end
360:       end
haml_attributes(options) click to toggle source

Returns a string representation of an attributes hash that’s prettier than that produced by Hash#inspect

     # File lib/haml/html.rb, line 402
402:       def haml_attributes(options)
403:         attrs = attr_hash.sort.map do |name, value|
404:           value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
405:           name = name.index(/\W/) ? name.inspect : ":#{name}"
406:           "#{name} => #{value}"
407:         end
408:         "{#{attrs.join(', ')}}"
409:       end
haml_css_attr?(attr) click to toggle source
     # File lib/haml/html.rb, line 396
396:       def haml_css_attr?(attr)
397:         attr =~ /^[-:\w]+$/
398:       end
render_children(so_far, tabs, options) click to toggle source
     # File lib/haml/html.rb, line 341
341:       def render_children(so_far, tabs, options)
342:         (self.children || []).inject(so_far) do |output, child|
343:           output + child.to_haml(tabs + 1, options)
344:         end
345:       end
static_attribute?(name, options) click to toggle source
     # File lib/haml/html.rb, line 380
380:       def static_attribute?(name, options)
381:         attr_hash[name] && !dynamic_attribute?(name, options)
382:       end
static_classname?(options) click to toggle source
     # File lib/haml/html.rb, line 392
392:       def static_classname?(options)
393:         static_attribute?('class', options)
394:       end
static_id?(options) click to toggle source
     # File lib/haml/html.rb, line 388
388:       def static_id?(options)
389:         static_attribute?('id', options) && haml_css_attr?(attr_hash['id'])
390:       end
to_haml_filter(filter, tabs, options) click to toggle source
     # File lib/haml/html.rb, line 362
362:       def to_haml_filter(filter, tabs, options)
363:         content =
364:           if children.first.is_a?(::Hpricot::CData)
365:             children.first.content
366:           else
367:             CGI.unescapeHTML(self.innerText)
368:           end
369:           
370:         content = erb_to_interpolation(content, options)
371:         content.gsub!(/\A\s*\n(\s*)/, '\1')
372:         original_indent = content[/\A(\s*)/, 1]
373:         if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
374:           content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
375:         end
376: 
377:         "#{tabulate(tabs)}:#{filter}\n#{content}"
378:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.