module Hpricot

def self.build(ele = Doc.new, assigns = {}, &blk)

def self.build(ele = Doc.new, assigns = {}, &blk)
  ele.extend Builder
  assigns.each do |k, v|
    ele.instance_variable_set("@#{k}", v)
  end
  ele.instance_eval(&blk)
  ele
end

def self.uxs(str)

XML unescape
def self.uxs(str)
  str.to_s.
      gsub(/\&(\w+);/) { [NamedCharacters[$1] || 63].pack("U*") }. # 63 = ?? (query char)
      gsub(/\&\#(\d+);/) { [$1.to_i].pack("U*") }.
      gsub(/\&\#x([0-9a-fA-F]+);/) { [$1.to_i(16)].pack("U*") }
end

def uxs(str)

XML unescape
def uxs(str)
  str.to_s.
      gsub(/\&\w+;/) { |x| (XChar::PREDEFINED_U[x] || 63).chr }. # 63 = ?? (query char)
      gsub(/\&\#(\d+);/) { [$1.to_i].pack("U*") }.
      gsub(/\&\#x([0-9a-fA-F]+);/) { [$1.to_i(16)].pack("U*") }
end

def xchr(str)

XML escaped version of chr
def xchr(str)
  n = XChar::CP1252[str] || str
  case n when *XChar::VALID
    XChar::PREDEFINED[n] or (n<128 ? n.chr : "&##{n};")
  else
    '*'
  end
end

def xs(str)

XML escaped version of to_s
def xs(str)
  str.to_s.unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-8
rescue
  str.to_s.unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-1252
end