Hpricot::Builder

Public Class Methods

set(option, value) click to toggle source
    # File lib/hpricot/builder.rb, line 37
37:     def self.set(option, value)
38:       @@default[option] = value
39:     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 41
41:     def add_child ele
42:       ele.parent = self
43:       self.children ||= []
44:       self.children << ele
45:       ele
46:     end
build(*a, &b) click to toggle source
     # File lib/hpricot/builder.rb, line 120
120:     def build(*a, &b)
121:       Hpricot.build(*a, &b)
122:     end
concat(string) click to toggle source
Alias for: text
doctype(target, pub, sys) click to toggle source
     # File lib/hpricot/builder.rb, line 148
148:     def doctype(target, pub, sys)
149:       add_child DocType.new(target, pub, sys)
150:     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 156
156:     def head(*args, &block)
157:       tag!(:head, *args) do
158:         tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
159:         instance_eval(&block)
160:       end
161:     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 130
130:     def html_tag(sym, *args, &block)
131:       if @auto_validation and @tagset.self_closing.include?(sym) and block
132:         raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
133:       elsif args.empty? and block.nil?
134:         CssProxy.new(self, sym)
135:       else
136:         tag!(sym, *args, &block)
137:       end
138:     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 63
 63:     def tag!(tag, *args, &block)
 64:       ele_id = nil
 65:       if @auto_validation and @tagset
 66:           if !@tagset.tagset.has_key?(tag)
 67:               raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
 68:           elsif args.last.respond_to?(:to_hash)
 69:               attrs = args.last.to_hash
 70: 
 71:               if @tagset.forms.include?(tag) and attrs[:id]
 72:                 attrs[:name] ||= attrs[:id]
 73:               end
 74: 
 75:               attrs.each do |k, v|
 76:                   atname = k.to_s.downcase.intern
 77:                   unless k =~ /:/ or @tagset.tagset[tag].include? atname
 78:                       raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
 79:                   end
 80:                   if atname == :id
 81:                       ele_id = v.to_s
 82:                       if @elements.has_key? ele_id
 83:                           raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
 84:                       end
 85:                   end
 86:               end
 87:           end
 88:       end
 89: 
 90:       # turn arguments into children or attributes
 91:       childs = []
 92:       attrs = args.grep(Hash)
 93:       childs.concat((args - attrs).flatten.map do |x|
 94:         if x.respond_to? :to_html
 95:           Hpricot.make(x.to_html)
 96:         elsif x
 97:           Text.new(x.fast_xs)
 98:         end
 99:       end.flatten)
100:       attrs = attrs.inject({}) do |hsh, ath|
101:         ath.each do |k, v|
102:           hsh[k] = v.to_s.fast_xs if v
103:         end
104:         hsh
105:       end
106: 
107:       # create the element itself
108:       tag = tag.to_s
109:       f = Elem.new(tag, attrs, childs, ETag.new(tag))
110: 
111:       # build children from the block
112:       if block
113:         build(f, &block)
114:       end
115: 
116:       add_child f
117:       f
118:     end
text(string) click to toggle source

Write a string to the HTML stream without escaping it.

    # File lib/hpricot/builder.rb, line 54
54:     def text(string)
55:       add_child Text.new(string)
56:       nil
57:     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 49
49:     def text!(string)
50:       add_child Text.new(string.fast_xs)
51:     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 172
172:     def xhtml_strict(attrs = {}, &block)
173:       # self.tagset = Hpricot::XHTMLStrict
174:       xhtml_html(attrs, &block)
175:     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 166
166:     def xhtml_transitional(attrs = {}, &block)
167:       # self.tagset = Hpricot::XHTMLTransitional
168:       xhtml_html(attrs, &block)
169:     end

Private Instance Methods

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.