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.
254
255
256
257
258
259
260
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
|
# File 'lib/rexle.rb', line 254
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.
251
252
253
|
# File 'lib/rexle.rb', line 251
def doctype
@doctype
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
252
253
254
|
# File 'lib/rexle.rb', line 252
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
251
252
253
|
# File 'lib/rexle.rb', line 251
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
1347
|
# File 'lib/rexle.rb', line 1347
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
|
# File 'lib/rexle.rb', line 1351
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
1363
|
# File 'lib/rexle.rb', line 1363
def add_text(s) end
|
#at_css(selector) ⇒ Object
292
293
294
|
# File 'lib/rexle.rb', line 292
def at_css(selector)
@doc.root.element RexleCSS.new(selector).to_xpath
end
|
#attribute(key) ⇒ Object
1348
|
# File 'lib/rexle.rb', line 1348
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
1349
|
# File 'lib/rexle.rb', line 1349
def attributes() @doc.attributes end
|
#clone ⇒ Object
288
289
290
|
# File 'lib/rexle.rb', line 288
def clone()
Rexle.new self.to_a
end
|
#content(options = {}) ⇒ Object
1413
1414
1415
|
# File 'lib/rexle.rb', line 1413
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#css(selector) ⇒ Object
296
297
298
|
# File 'lib/rexle.rb', line 296
def css(selector)
selector.split(',').flat_map{|x| @doc.root.xpath RexleCSS.new(x).to_xpath}
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
1367
1368
1369
1370
1371
|
# File 'lib/rexle.rb', line 1367
def delete(xpath)
@doc.xpath(xpath).each {|e| e.delete }
end
|
#element(xpath) ⇒ Object
1375
|
# File 'lib/rexle.rb', line 1375
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
1376
|
# File 'lib/rexle.rb', line 1376
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
1377
|
# File 'lib/rexle.rb', line 1377
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
|
# File 'lib/rexle.rb', line 1326
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
1386
1387
1388
|
# File 'lib/rexle.rb', line 1386
def root()
@doc.elements.first
end
|
#text(xpath) ⇒ Object
1385
|
# File 'lib/rexle.rb', line 1385
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
1378
|
# File 'lib/rexle.rb', line 1378
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
1380
1381
1382
1383
|
# File 'lib/rexle.rb', line 1380
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
1390
1391
1392
|
# File 'lib/rexle.rb', line 1390
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
|
# File 'lib/rexle.rb', line 1394
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
300
301
302
|
# File 'lib/rexle.rb', line 300
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|