Module | Hpricot::Builder |
In: |
lib/hpricot/builder.rb
lib/hpricot/builder.rb |
# File lib/hpricot/builder.rb, line 132 def doctype(target, pub, sys) @children << DocType.new(target, pub, sys) end
# File lib/hpricot/builder.rb, line 132 def doctype(target, pub, sys) @children << DocType.new(target, pub, sys) end
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 140 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
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 140 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
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 114 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
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 114 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
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 48 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).map do |x| if x.respond_to? :to_html Hpricot.make(x.to_html) elsif x Text.new(Hpricot.xs(x)) end end.flatten) attrs = attrs.inject({}) do |hsh, ath| ath.each do |k, v| hsh[k] = Hpricot.xs(v.to_s) 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
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 48 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).map do |x| if x.respond_to? :to_html Hpricot.make(x.to_html) elsif x Text.new(Hpricot.xs(x)) end end.flatten) attrs = attrs.inject({}) do |hsh, ath| ath.each do |k, v| hsh[k] = Hpricot.xs(v.to_s) 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
Write a string to the HTML stream without escaping it.
# File lib/hpricot/builder.rb, line 39 def text(string) @children << Text.new(string) nil end
Write a string to the HTML stream without escaping it.
# File lib/hpricot/builder.rb, line 39 def text(string) @children << Text.new(string) nil end
Write a string to the HTML stream, making sure to escape it.
# File lib/hpricot/builder.rb, line 34 def text!(string) @children << Text.new(Hpricot.xs(string)) end
Write a string to the HTML stream, making sure to escape it.
# File lib/hpricot/builder.rb, line 34 def text!(string) @children << Text.new(Hpricot.xs(string)) end
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 150 def xhtml_transitional(attrs = {}, &block) # self.tagset = Hpricot::XHTMLTransitional xhtml_html(attrs, &block) end
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 150 def xhtml_transitional(attrs = {}, &block) # self.tagset = Hpricot::XHTMLTransitional xhtml_html(attrs, &block) end