Class: Rexle
Defined Under Namespace
Classes: CData, Comment, Element, Elements
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from XMLhelper
#doc_pretty_print, #doc_print, #inspect, #pretty_print, #processing_instructions, #scan_print, #scan_to_a
Constructor Details
#initialize(x = nil) ⇒ Rexle
Returns a new instance of Rexle.
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
# File 'lib/rexle.rb', line 307
def initialize(x=nil)
super()
@instructions = [["xml", "version='1.0' encoding='UTF-8'"]]
@doctype = :xml
if x then
procs = {
String: proc {|x| parse_string(x)},
Array: proc {|x| x}
}
doc_node = ['doc',Attributes.new]
@a = procs[x.class.to_s.to_sym].call(x)
@doc = scan_element(*(doc_node << @a))
@prefixes = []
if @doc.root.attributes then
xmlns = @doc.root.attributes.select {|k,v| k[/^xmlns:/]}
@prefixes = xmlns.keys.map{|x| x[/\w+$/]}
end
end
end
|
Instance Attribute Details
#doctype ⇒ Object
Returns the value of attribute doctype.
304
305
306
|
# File 'lib/rexle.rb', line 304
def doctype
@doctype
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
305
306
307
|
# File 'lib/rexle.rb', line 305
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
304
305
306
|
# File 'lib/rexle.rb', line 304
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
1281
|
# File 'lib/rexle.rb', line 1281
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
|
# File 'lib/rexle.rb', line 1285
def add_element(element)
if @doc then
raise 'attempted adding second root element to document' if @doc.root
@doc.root.add_element(element)
else
doc_node = ['doc', Attributes.new, element.to_a]
@doc = scan_element(*doc_node)
end
element
end
|
#add_text(s) ⇒ Object
1297
|
# File 'lib/rexle.rb', line 1297
def add_text(s) end
|
#at_css(selector) ⇒ Object
343
344
345
|
# File 'lib/rexle.rb', line 343
def at_css(selector)
@doc.root.element RexleCSS.new(selector).to_xpath
end
|
#attribute(key) ⇒ Object
1282
|
# File 'lib/rexle.rb', line 1282
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
1283
|
# File 'lib/rexle.rb', line 1283
def attributes() @doc.attributes end
|
#clone ⇒ Object
339
340
341
|
# File 'lib/rexle.rb', line 339
def clone()
Rexle.new self.to_a
end
|
#content(options = {}) ⇒ Object
1347
1348
1349
|
# File 'lib/rexle.rb', line 1347
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#css(selector) ⇒ Object
347
348
349
|
# File 'lib/rexle.rb', line 347
def css(selector)
selector.split(',').flat_map{|x| @doc.root.xpath RexleCSS.new(x).to_xpath}
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
1301
1302
1303
1304
1305
|
# File 'lib/rexle.rb', line 1301
def delete(xpath)
e = @doc.element(xpath)
e.delete if e
end
|
#element(xpath) ⇒ Object
1309
|
# File 'lib/rexle.rb', line 1309
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
1310
|
# File 'lib/rexle.rb', line 1310
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
1311
|
# File 'lib/rexle.rb', line 1311
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
|
# File 'lib/rexle.rb', line 1260
def parse(x=nil)
a = []
if x then
procs = {
String: proc {|x| parse_string(x)},
Array: proc {|x| x}
}
a = procs[x.class.to_s.to_sym].call(x)
else
a = yield
end
doc_node = ['doc',Attributes.new]
@a = procs[x.class.to_s.to_sym].call(x)
@doc = scan_element(*(doc_node << @a))
self
end
|
#root ⇒ Object
1320
1321
1322
|
# File 'lib/rexle.rb', line 1320
def root()
@doc.elements.first
end
|
#text(xpath) ⇒ Object
1319
|
# File 'lib/rexle.rb', line 1319
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
1312
|
# File 'lib/rexle.rb', line 1312
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
1314
1315
1316
1317
|
# File 'lib/rexle.rb', line 1314
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
1324
1325
1326
|
# File 'lib/rexle.rb', line 1324
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
|
# File 'lib/rexle.rb', line 1328
def xml(options={})
return '' unless @doc
o = {pretty: false, declaration: true}.merge(options)
msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
r = ''
if o[:declaration] == true then
unless @instructions.assoc 'xml' then
@instructions.unshift ["xml","version='1.0' encoding='UTF-8'"]
end
end
r << method(msg).call(self.root.children, o[:declaration]).strip
r
end
|
#xpath(path, &blk) ⇒ Object
351
352
353
|
# File 'lib/rexle.rb', line 351
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|