In Files

Parent

Class Index [+]

Quicksearch

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 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

Private Instance Methods

dynamic_attribute?(name, options) click to toggle source
     # File lib/haml/html.rb, line 400
400:       def dynamic_attribute?(name, options)
401:         options[:erb] and dynamic_attributes.key?(name)
402:       end
dynamic_attributes() click to toggle source
     # File lib/haml/html.rb, line 363
363:       def dynamic_attributes
364:         @dynamic_attributes ||= begin
365:           Haml::Util.map_hash(attr_hash) do |name, value|
366:             next if value.empty?
367:             full_match = nil
368:             ruby_value = value.gsub(%{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
369:               full_match = $`.empty? && $'.empty?
370:               CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
371:             end
372:             next if ruby_value == value
373:             [name, full_match ? ruby_value : %("#{ruby_value}")]
374:           end
375:         end
376:       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 418
418:       def haml_attributes(options)
419:         attrs = attr_hash.sort.map do |name, value|
420:           value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
421:           name = name.index(/\W/) ? name.inspect : ":#{name}"
422:           "#{name} => #{value}"
423:         end
424:         "{#{attrs.join(', ')}}"
425:       end
haml_css_attr?(attr) click to toggle source
     # File lib/haml/html.rb, line 412
412:       def haml_css_attr?(attr)
413:         attr =~ /^[-:\w]+$/
414:       end
render_children(so_far, tabs, options) click to toggle source
     # File lib/haml/html.rb, line 357
357:       def render_children(so_far, tabs, options)
358:         (self.children || []).inject(so_far) do |output, child|
359:           output + child.to_haml(tabs + 1, options)
360:         end
361:       end
static_attribute?(name, options) click to toggle source
     # File lib/haml/html.rb, line 396
396:       def static_attribute?(name, options)
397:         attr_hash[name] && !dynamic_attribute?(name, options)
398:       end
static_classname?(options) click to toggle source
     # File lib/haml/html.rb, line 408
408:       def static_classname?(options)
409:         static_attribute?('class', options)
410:       end
static_id?(options) click to toggle source
     # File lib/haml/html.rb, line 404
404:       def static_id?(options)
405:         static_attribute?('id', options) && haml_css_attr?(attr_hash['id'])
406:       end
to_haml_filter(filter, tabs, options) click to toggle source
     # File lib/haml/html.rb, line 378
378:       def to_haml_filter(filter, tabs, options)
379:         content =
380:           if children.first.is_a?(::Hpricot::CData)
381:             children.first.content
382:           else
383:             CGI.unescapeHTML(self.innerText)
384:           end
385:           
386:         content = erb_to_interpolation(content, options)
387:         content.gsub!(/\A\s*\n(\s*)/, '\1')
388:         original_indent = content[/\A(\s*)/, 1]
389:         if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
390:           content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
391:         end
392: 
393:         "#{tabulate(tabs)}:#{filter}\n#{content}"
394:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.