Class Hpricot::Elem
In: lib/haml/html.rb
Parent: Object

@see Hpricot @private

Methods

to_haml  

Public Instance methods

@see Haml::HTML::Node#to_haml

[Source]

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

[Validate]