Hpricot::Builder

Public Class Methods

set(option, value) click to toggle source
    # File lib/hpricot/builder.rb, line 38
38:     def self.set(option, value)
39:       @@default[option] = value
40:     end

Public Instance Methods

<<(string) click to toggle source
Alias for: text
add_child(ele) click to toggle source
    # File lib/hpricot/builder.rb, line 42
42:     def add_child ele
43:       ele.parent = self
44:       self.children ||= []
45:       self.children << ele
46:       ele
47:     end
build(*a, &b) click to toggle source
     # File lib/hpricot/builder.rb, line 121
121:     def build(*a, &b)
122:       Hpricot.build(*a, &b)
123:     end
concat(string) click to toggle source
Alias for: text
doctype(target, pub, sys) click to toggle source
     # File lib/hpricot/builder.rb, line 149
149:     def doctype(target, pub, sys)
150:       add_child DocType.new(target, pub, sys)
151:     end
head(*args, &block) click to toggle source

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

     # File lib/hpricot/builder.rb, line 157
157:     def head(*args, &block)
158:       tag!(:head, *args) do
159:         tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
160:         instance_eval(&block)
161:       end
162:     end
html_tag(sym, *args, &block) click to toggle source

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.

     # File lib/hpricot/builder.rb, line 131
131:     def html_tag(sym, *args, &block)
132:       if @auto_validation and @tagset.self_closing.include?(sym) and block
133:         raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
134:       elsif args.empty? and block.nil?
135:         CssProxy.new(self, sym)
136:       else
137:         tag!(sym, *args, &block)
138:       end
139:     end
tag!(tag, *args, &block) click to toggle source

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.

     # File lib/hpricot/builder.rb, line 64
 64:     def tag!(tag, *args, &block)
 65:       ele_id = nil
 66:       if @auto_validation and @tagset
 67:           if !@tagset.tagset.has_key?(tag)
 68:               raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
 69:           elsif args.last.respond_to?(:to_hash)
 70:               attrs = args.last.to_hash
 71: 
 72:               if @tagset.forms.include?(tag) and attrs[:id]
 73:                 attrs[:name] ||= attrs[:id]
 74:               end
 75: 
 76:               attrs.each do |k, v|
 77:                   atname = k.to_s.downcase.intern
 78:                   unless k =~ /:/ or @tagset.tagset[tag].include? atname
 79:                       raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
 80:                   end
 81:                   if atname == :id
 82:                       ele_id = v.to_s
 83:                       if @elements.has_key? ele_id
 84:                           raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
 85:                       end
 86:                   end
 87:               end
 88:           end
 89:       end
 90: 
 91:       # turn arguments into children or attributes
 92:       childs = []
 93:       attrs = args.grep(Hash)
 94:       childs.concat((args - attrs).flatten.map do |x|
 95:         if x.respond_to? :to_html
 96:           Hpricot.make(x.to_html)
 97:         elsif x
 98:           Text.new(x.fast_xs)
 99:         end
100:       end.flatten)
101:       attrs = attrs.inject({}) do |hsh, ath|
102:         ath.each do |k, v|
103:           hsh[k] = v.to_s.fast_xs if v
104:         end
105:         hsh
106:       end
107: 
108:       # create the element itself
109:       tag = tag.to_s
110:       f = Elem.new(tag, attrs, childs, ETag.new(tag))
111: 
112:       # build children from the block
113:       if block
114:         build(f, &block)
115:       end
116: 
117:       add_child f
118:       f
119:     end
text(string) click to toggle source

Write a string to the HTML stream without escaping it.

    # File lib/hpricot/builder.rb, line 55
55:     def text(string)
56:       add_child Text.new(string)
57:       nil
58:     end
Also aliased as: <<, concat
text!(string) click to toggle source

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

    # File lib/hpricot/builder.rb, line 50
50:     def text!(string)
51:       add_child Text.new(string.fast_xs)
52:     end
xhtml_strict(attrs = {}, &block) click to toggle source

Builds an html tag with XHTML 1.0 Strict doctype instead.

     # File lib/hpricot/builder.rb, line 173
173:     def xhtml_strict(attrs = {}, &block)
174:       # self.tagset = Hpricot::XHTMLStrict
175:       xhtml_html(attrs, &block)
176:     end
xhtml_transitional(attrs = {}, &block) click to toggle source

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

     # File lib/hpricot/builder.rb, line 167
167:     def xhtml_transitional(attrs = {}, &block)
168:       # self.tagset = Hpricot::XHTMLTransitional
169:       xhtml_html(attrs, &block)
170:     end

Private Instance Methods

xhtml_html(attrs = {}, &block) click to toggle source
     # File lib/hpricot/builder.rb, line 180
180:     def xhtml_html(attrs = {}, &block)
181:       instruct! if @output_xml_instruction
182:       doctype(:html, *@@default[:tagset].doctype)
183:       tag!(:html, @@default[:root_attributes].merge(attrs), &block)
184:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.