Class: Haml2Erb::ErbWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/haml2erb/erb_writer.rb

Instance Method Summary collapse

Constructor Details

#initializeErbWriter

Returns a new instance of ErbWriter.



4
5
6
7
# File 'lib/haml2erb/erb_writer.rb', line 4

def initialize
  @processed = ''
  @tag_stack = [ ]
end

Instance Method Details

#<<(line_options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/haml2erb/erb_writer.rb', line 9

def <<(line_options)

  close_tags(line_options[:indent])
  @tag_stack.push(line_options[:element_type]) if line_options[:element_type]

  @processed << ("  " * line_options[:indent]) if line_options[:indent]
  @processed << "<#{line_options[:element_type].to_s}" if line_options[:element_type]
  @processed << " id='#{line_options[:element_id].to_s}'" if line_options[:element_id]
  @processed << " class='#{[*line_options[:element_class]].join(' ')}'" if line_options[:element_class]
  line_options[:element_attributes] && line_options[:element_attributes].keys.each do |attribute_key|
    @processed << " #{attribute_key}='#{line_options[:element_attributes][attribute_key]}'"
  end
  @processed << ">" if line_options[:element_type]

  case(line_options[:content_type])
  when :text
    @processed << (line_options[:contents] || "")
  when :ruby
    @processed << ("<%= " + line_options[:contents] + " %>")
  when :mixed
    @processed << ('<%= "' + line_options[:contents] + '" %>')
  end

  close_tags(line_options[:indent], :separate_line => false) if line_options[:contents]
  @processed << "\n"
end

#output_to_stringObject



36
37
38
39
# File 'lib/haml2erb/erb_writer.rb', line 36

def output_to_string
  close_tags(0)
  @processed
end