Class: Rexle
Defined Under Namespace
Classes: CData, Comment, Element, Elements, Recordset
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.
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/rexle.rb', line 261
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},
RexleParser: ->(x){ parse_rexle(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.
258
259
260
|
# File 'lib/rexle.rb', line 258
def doctype
@doctype
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
259
260
261
|
# File 'lib/rexle.rb', line 259
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
258
259
260
|
# File 'lib/rexle.rb', line 258
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
1380
|
# File 'lib/rexle.rb', line 1380
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
|
# File 'lib/rexle.rb', line 1384
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
1396
|
# File 'lib/rexle.rb', line 1396
def add_text(s) end
|
#at_css(selector) ⇒ Object
299
300
301
|
# File 'lib/rexle.rb', line 299
def at_css(selector)
@doc.root.element RexleCSS.new(selector).to_xpath
end
|
#attribute(key) ⇒ Object
1381
|
# File 'lib/rexle.rb', line 1381
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
1382
|
# File 'lib/rexle.rb', line 1382
def attributes() @doc.attributes end
|
#clone ⇒ Object
295
296
297
|
# File 'lib/rexle.rb', line 295
def clone()
Rexle.new self.to_a
end
|
#content(options = {}) ⇒ Object
1446
1447
1448
|
# File 'lib/rexle.rb', line 1446
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#css(selector) ⇒ Object
303
304
305
|
# File 'lib/rexle.rb', line 303
def css(selector)
selector.split(',').flat_map{|x| @doc.root.xpath RexleCSS.new(x).to_xpath}
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
1400
1401
1402
1403
1404
|
# File 'lib/rexle.rb', line 1400
def delete(xpath)
@doc.xpath(xpath).each {|e| e.delete }
end
|
#element(xpath) ⇒ Object
1408
|
# File 'lib/rexle.rb', line 1408
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
1409
|
# File 'lib/rexle.rb', line 1409
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
1410
|
# File 'lib/rexle.rb', line 1410
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
|
# File 'lib/rexle.rb', line 1359
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
1419
1420
1421
|
# File 'lib/rexle.rb', line 1419
def root()
@doc.elements.first
end
|
#text(xpath) ⇒ Object
1418
|
# File 'lib/rexle.rb', line 1418
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
1411
|
# File 'lib/rexle.rb', line 1411
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
1413
1414
1415
1416
|
# File 'lib/rexle.rb', line 1413
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
1423
1424
1425
|
# File 'lib/rexle.rb', line 1423
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
|
# File 'lib/rexle.rb', line 1427
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
307
308
309
|
# File 'lib/rexle.rb', line 307
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|