Class: Jam::Hpricot

Inherits:
Engine show all
Defined in:
lib/jam/hpricot.rb

Overview

Hpricot Adaptor

Instance Method Summary collapse

Methods inherited from Engine

#interpolate

Constructor Details

#initialize(*options) ⇒ Hpricot

Returns a new instance of Hpricot.



37
38
39
# File 'lib/jam/hpricot.rb', line 37

def initialize(*options)
  @options = options
end

Instance Method Details

#append(ns, child) ⇒ Object



67
68
69
# File 'lib/jam/hpricot.rb', line 67

def append(ns, child)
  ns.append(child.to_s)
end

#attribute(ns, att, val) ⇒ Object



97
98
99
# File 'lib/jam/hpricot.rb', line 97

def attribute(ns, att, val)
  ns.set(att, val)
end

#cleanup(node) ⇒ Object

Remove jam nodes that ask for it, and all jam attributes.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jam/hpricot.rb', line 103

def cleanup(node)
  #node = node.root if ::Nokogiri::XML::Document === node
  # remove unwanted tags
  node.search("//*[@jam='erase']").each do |n|
    unwrap(n)
  end
  # remove jam attributes

  #
  node
end

#copy(node) ⇒ Object

deep copy # TODO: works?



52
53
54
# File 'lib/jam/hpricot.rb', line 52

def copy(node)
  node.dup 
end

#document(source) ⇒ Object



42
43
44
# File 'lib/jam/hpricot.rb', line 42

def document(source)
  ::Hpricot.parse(source) #, *@options)
end

#each_node(nodes) ⇒ Object

Iterate over each node.



125
126
127
128
129
130
131
132
# File 'lib/jam/hpricot.rb', line 125

def each_node(nodes)
  unless ::Hpricot::Elements === nodes
    nodes = [nodes]
  end
  nodes.each do |node|
    yield(node)
  end
end

#empty(ns) ⇒ Object



62
63
64
# File 'lib/jam/hpricot.rb', line 62

def empty(ns)
  ns.empty
end

#remove(node) ⇒ Object



57
58
59
# File 'lib/jam/hpricot.rb', line 57

def remove(node)
  node.remove
end

#replace(ns, child) ⇒ Object



72
73
74
75
# File 'lib/jam/hpricot.rb', line 72

def replace(ns, child)
  empty(ns)
  append(ns, child)
end

#replace_content_with_text(node, text) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jam/hpricot.rb', line 83

def replace_content_with_text(node, text)
  case node
  when ::Hpricot::Elements
    node.inner_html = text
  when ::Array
    node.each do |n|
      n.inner_html = text
    end
  else

  end
end

#search(node, qry) ⇒ Object



47
48
49
# File 'lib/jam/hpricot.rb', line 47

def search(node, qry)
  node.search(qry)
end

#unwrap(node) ⇒ Object

Unwrap a node, such that the outer tag is removed, leaving only it’s own children.



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

def unwrap(node)
  node.each_child{ |n| node.parent.insert_before(n, node) }
  ::Hpricot::Elements[node].remove
end