Class Hpricot::Elem
In: lib/haml/html.rb
Parent: Object
Haml::HTML::Node BaseEle Comment XMLDecl Doc CData Elem DocType lib/haml/html.rb Node Hpricot dot/m_66_0.png

@see Hpricot @private

Methods

Public Instance methods

@see Haml::HTML::Node#to_haml

[Source]

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

Private Instance methods

[Source]

     # File lib/haml/html.rb, line 407
407:       def dynamic_attribute?(name, options)
408:         options[:erb] and dynamic_attributes.key?(name)
409:       end

[Source]

     # File lib/haml/html.rb, line 370
370:       def dynamic_attributes
371:         @dynamic_attributes ||= begin
372:           Haml::Util.map_hash(attr_hash) do |name, value|
373:             next if value.empty?
374:             full_match = nil
375:             ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
376:               full_match = $`.empty? && $'.empty?
377:               CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
378:             end
379:             next if ruby_value == value
380:             [name, full_match ? ruby_value : %("#{ruby_value}")]
381:           end
382:         end
383:       end

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

[Source]

     # File lib/haml/html.rb, line 425
425:       def haml_attributes(options)
426:         attrs = attr_hash.sort.map do |name, value|
427:           value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
428:           name = name.index(/\W/) ? name.inspect : ":#{name}"
429:           "#{name} => #{value}"
430:         end
431:         "{#{attrs.join(', ')}}"
432:       end

[Source]

     # File lib/haml/html.rb, line 419
419:       def haml_css_attr?(attr)
420:         attr =~ /^[-:\w]+$/
421:       end

[Source]

     # File lib/haml/html.rb, line 364
364:       def render_children(so_far, tabs, options)
365:         (self.children || []).inject(so_far) do |output, child|
366:           output + child.to_haml(tabs + 1, options)
367:         end
368:       end

[Source]

     # File lib/haml/html.rb, line 403
403:       def static_attribute?(name, options)
404:         attr_hash[name] && !dynamic_attribute?(name, options)
405:       end

[Source]

     # File lib/haml/html.rb, line 415
415:       def static_classname?(options)
416:         static_attribute?('class', options)
417:       end

[Source]

     # File lib/haml/html.rb, line 411
411:       def static_id?(options)
412:         static_attribute?('id', options) && haml_css_attr?(attr_hash['id'])
413:       end

[Source]

     # File lib/haml/html.rb, line 385
385:       def to_haml_filter(filter, tabs, options)
386:         content =
387:           if children.first.is_a?(::Hpricot::CData)
388:             children.first.content
389:           else
390:             CGI.unescapeHTML(self.innerText)
391:           end
392:           
393:         content = erb_to_interpolation(content, options)
394:         content.gsub!(/\A\s*\n(\s*)/, '\1')
395:         original_indent = content[/\A(\s*)/, 1]
396:         if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
397:           content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
398:         end
399: 
400:         "#{tabulate(tabs)}:#{filter}\n#{content}"
401:       end

[Validate]