Class: Erector::Doc

Inherits:
Object show all
Defined in:
lib/erector/doc.rb

Overview

A proxy to an IO object that adds methods to add xml.

Constant Summary collapse

NON_NEWLINEY =
{'i' => true, 'b' => true, 'small' => true,
  'img' => true, 'span' => true, 'a' => true,
  'input' => true, 'textarea' => true, 'button' => true, 'select' => true
}
SPACES_PER_INDENT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ Doc

Returns a new instance of Doc.



14
15
16
17
18
# File 'lib/erector/doc.rb', line 14

def initialize(output, options = {})
  @output = output
  @at_start_of_line = true
  @indent = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object (protected)



96
97
98
99
100
# File 'lib/erector/doc.rb', line 96

def method_missing(method_name, *args, &blk)
  output.__send__(method_name, *args, &blk)
rescue NoMethodError => e
  raise NoMethodError, "undefined method `#{method_name}' for #{inspect}"
end

Instance Attribute Details

#enable_prettyprintObject

Returns the value of attribute enable_prettyprint.



12
13
14
# File 'lib/erector/doc.rb', line 12

def enable_prettyprint
  @enable_prettyprint
end

Instance Method Details

#close_tag(tag_name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/erector/doc.rb', line 49

def close_tag(tag_name)
  @indent -= SPACES_PER_INDENT
  indent()

  output.print("</#{tag_name}>")

  if newliney(tag_name)
    output.print "\n"
    @at_start_of_line = true
  end
end

#empty_element(tag_name, attributes = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/erector/doc.rb', line 76

def empty_element(tag_name, attributes={})
  indent_for_open_tag(tag_name)

  output.print "<#{tag_name}#{format_attributes(attributes)} />"

  if newliney(tag_name)
    output.print "\n"
    @at_start_of_line = true
  end
end

#indentObject



70
71
72
73
74
# File 'lib/erector/doc.rb', line 70

def indent()
  if @at_start_of_line
    output.print " " * @indent
  end
end

#indent_for_open_tag(tag_name) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/erector/doc.rb', line 61

def indent_for_open_tag(tag_name)
  if !@at_start_of_line && newliney(tag_name)
    output.print "\n"
    @at_start_of_line = true
  end

  indent()
end

#instruct(attributes = {:version => "1.0", :encoding => "UTF-8"}) ⇒ Object



87
88
89
# File 'lib/erector/doc.rb', line 87

def instruct(attributes={:version => "1.0", :encoding => "UTF-8"})
  output.print "<?xml#{format_sorted(sort_for_xml_declaration(attributes))}?>"
end

#newliney(tag_name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/erector/doc.rb', line 27

def newliney(tag_name)
  if @enable_prettyprint
    !NON_NEWLINEY.include?(tag_name)
  else
    false
  end
end

#open_tag(tag_name, attributes = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/erector/doc.rb', line 35

def open_tag(tag_name, attributes={})
  indent_for_open_tag(tag_name)
  @indent += SPACES_PER_INDENT

  output.print "<#{tag_name}#{format_attributes(attributes)}>"
  @at_start_of_line = false
end

#outputObject



20
21
22
23
24
25
# File 'lib/erector/doc.rb', line 20

def output
  unless @output.eof?
    @output.seek(0, IO::SEEK_END)
  end
  @output
end

#text(value) ⇒ Object



43
44
45
46
47
# File 'lib/erector/doc.rb', line 43

def text(value)
  output.print(value.html_escape)
  @at_start_of_line = false
  nil
end

#to_sObject



91
92
93
# File 'lib/erector/doc.rb', line 91

def to_s
  output.string
end