Class: Jam::Nokogiri

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

Overview

Nokogiri Adaptor

Instance Method Summary collapse

Methods inherited from Engine

#interpolate

Constructor Details

#initialize(*options) ⇒ Nokogiri

Returns a new instance of Nokogiri.



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

def initialize(*options)
  @options = options
end

Instance Method Details

#append(node_or_nodeset, child) ⇒ Object

Append child to node(s).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jam/nokogiri.rb', line 74

def append(node_or_nodeset, child)
  ns = node_or_nodeset
  case child
  when ::Nokogiri::XML::Node
    each_node(node_or_nodeset) do |node|
      node << child
    end
  when ::Nokogiri::XML::NodeSet
    child.each do |n|
      each_node(node_or_nodeset) do |node|
        node << n.dup
      end
    end
  else
    append_text(node_or_nodeset, child)        
  end
end

#append_text(node_or_nodeset, text) ⇒ Object

TODO



93
94
# File 'lib/jam/nokogiri.rb', line 93

def append_text(node_or_nodeset, text)
end

#attribute(ns, att, val) ⇒ Object

Set an attribute.



118
119
120
121
122
123
124
125
126
127
# File 'lib/jam/nokogiri.rb', line 118

def attribute(ns, att, val)
  case ns
  when ::Nokogiri::XML::Node
    ns.attr(att, val)
  when ::Nokogiri::XML::NodeSet
    ns.each do |n|
      ns.attr(att, val)
    end
  end
end

#cleanup(node) ⇒ Object

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jam/nokogiri.rb', line 131

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

  #
  node
end

#copy(node) ⇒ Object

Deep copy.



55
56
57
# File 'lib/jam/nokogiri.rb', line 55

def copy(node)
  node.dup 
end

#document(source) ⇒ Object

Contruct XML document given source text.



43
44
45
# File 'lib/jam/nokogiri.rb', line 43

def document(source)
  ::Nokogiri::XML(source, *@options)
end

#each_node(nodes) ⇒ Object

Iterate over each node.



145
146
147
148
149
150
151
152
# File 'lib/jam/nokogiri.rb', line 145

def each_node(nodes)
  unless ::Nokogiri::XML::NodeSet === nodes
    nodes = [nodes]
  end
  nodes.each do |node|
    yield(node)
  end
end

#empty(node_or_nodeset) ⇒ Object

Empty nodes.



61
62
63
64
65
66
67
68
69
70
# File 'lib/jam/nokogiri.rb', line 61

def empty(node_or_nodeset)
  case node_or_nodeset
  when ::Nokogiri::XML::Node
    node_or_nodeset.content = ''
  when ::Nokogiri::XML::NodeSet
    ns.each do |n|
      node_or_nodeset.content = ''
    end
  end
end

#remove(node) ⇒ Object

Remove node.



112
113
114
# File 'lib/jam/nokogiri.rb', line 112

def remove(node)
  node.remove
end

#replace_content_with_text(node_or_nodeset, text) ⇒ Object

Replace node content with text.



104
105
106
107
108
# File 'lib/jam/nokogiri.rb', line 104

def replace_content_with_text(node_or_nodeset, text)
  each_node(node_or_nodeset) do |node|
    node.content = text
  end
end

#search(node, qry) ⇒ Object

Use CSS or XPath to search node.



49
50
51
# File 'lib/jam/nokogiri.rb', line 49

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.



157
158
159
160
161
162
# File 'lib/jam/nokogiri.rb', line 157

def unwrap(node)
  node.children.each do |child|
   node.parent << child
  end
  node.remove
end