Module: Hpricot::Builder

Defined in:
lib/hpricot/builder.rb

Constant Summary collapse

@@default =
{
  :indent => 0,
  :output_helpers => true,
  :output_xml_instruction => true,
  :output_meta_tag => true,
  :auto_validation => true,
  :tagset => Hpricot::XHTMLTransitional,
  :root_attributes => {
    :xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en'
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set(option, value) ⇒ Object



44
45
46
# File 'lib/hpricot/builder.rb', line 44

def self.set(option, value)
  @@default[option] = value
end

Instance Method Details

#build(*a, &b) ⇒ Object



119
120
121
# File 'lib/hpricot/builder.rb', line 119

def build(*a, &b)
  Hpricot.build(*a, &b)
end

#doctype(target, pub, sys) ⇒ Object



147
148
149
# File 'lib/hpricot/builder.rb', line 147

def doctype(target, pub, sys)
  @children << DocType.new(target, pub, sys)
end

#head(*args, &block) ⇒ Object

Builds a head tag. Adds a meta tag inside with Content-Type set to text/html; charset=utf-8.



155
156
157
158
159
160
# File 'lib/hpricot/builder.rb', line 155

def head(*args, &block)
  tag!(:head, *args) do
    tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
    instance_eval(&block)
  end
end

#html_tag(sym, *args, &block) ⇒ Object

Every HTML tag method goes through an html_tag call. So, calling div is equivalent to calling html_tag(:div). All HTML tags in Hpricot’s list are given generated wrappers for this method.

If the @auto_validation setting is on, this method will check for many common mistakes which could lead to invalid XHTML.



129
130
131
132
133
134
135
136
137
# File 'lib/hpricot/builder.rb', line 129

def html_tag(sym, *args, &block)
  if @auto_validation and @tagset.self_closing.include?(sym) and block
    raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
  elsif args.empty? and block.nil?
    CssProxy.new(self, sym)
  else
    tag!(sym, *args, &block)
  end
end

#tag!(tag, *args, &block) ⇒ Object

Create a tag named tag. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hpricot/builder.rb', line 63

def tag!(tag, *args, &block)
  ele_id = nil
  if @auto_validation and @tagset
      if !@tagset.tagset.has_key?(tag)
          raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
      elsif args.last.respond_to?(:to_hash)
          attrs = args.last.to_hash
          
          if @tagset.forms.include?(tag) and attrs[:id]
            attrs[:name] ||= attrs[:id]
          end
          
          attrs.each do |k, v|
              atname = k.to_s.downcase.intern
              unless k =~ /:/ or @tagset.tagset[tag].include? atname
                  raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
              end
              if atname == :id
                  ele_id = v.to_s
                  if @elements.has_key? ele_id
                      raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
                  end
              end
          end
      end
  end

  # turn arguments into children or attributes
  childs = []
  attrs = args.grep(Hash)
  childs.concat((args - attrs).flatten.map do |x|
    if x.respond_to? :to_html
      Hpricot.make(x.to_html)
    elsif x
      Text.new(x.fast_xs)
    end
  end.flatten)
  attrs = attrs.inject({}) do |hsh, ath|
    ath.each do |k, v|
      hsh[k] = v.to_s.fast_xs if v
    end
    hsh
  end

  # create the element itself
  f = Elem.new(STag.new(tag, attrs), childs, ETag.new(tag))

  # build children from the block
  if block
    build(f, &block)
  end

  @children << f
  f
end

#text(string) ⇒ Object Also known as: <<, concat

Write a string to the HTML stream without escaping it.



54
55
56
57
# File 'lib/hpricot/builder.rb', line 54

def text(string)
  @children << Text.new(string)
  nil
end

#text!(string) ⇒ Object

Write a string to the HTML stream, making sure to escape it.



49
50
51
# File 'lib/hpricot/builder.rb', line 49

def text!(string)
  @children << Text.new(string.fast_xs)
end

#xhtml_strict(attrs = {}, &block) ⇒ Object

Builds an html tag with XHTML 1.0 Strict doctype instead.



171
172
173
174
# File 'lib/hpricot/builder.rb', line 171

def xhtml_strict(attrs = {}, &block)
  # self.tagset = Hpricot::XHTMLStrict
  xhtml_html(attrs, &block)
end

#xhtml_transitional(attrs = {}, &block) ⇒ Object

Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype are prepended. Also assumes :xmlns => "http://www.w3.org/1999/xhtml", :lang => "en".



165
166
167
168
# File 'lib/hpricot/builder.rb', line 165

def xhtml_transitional(attrs = {}, &block)
  # self.tagset = Hpricot::XHTMLTransitional
  xhtml_html(attrs, &block)
end