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: self, debug: false) ⇒ Rexle
Returns a new instance of Rexle.
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/rexle.rb', line 206
def initialize(x=nil, rexle: self, debug: false)
@rexle, @debug = rexle, debug
puts 'inside Rexle'.debug if debug
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.
203
204
205
|
# File 'lib/rexle.rb', line 203
def doctype
@doctype
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
204
205
206
|
# File 'lib/rexle.rb', line 204
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
203
204
205
|
# File 'lib/rexle.rb', line 203
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
1422
|
# File 'lib/rexle.rb', line 1422
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
|
# File 'lib/rexle.rb', line 1426
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
1438
|
# File 'lib/rexle.rb', line 1438
def add_text(s) end
|
#at_css(selector) ⇒ Object
248
249
250
|
# File 'lib/rexle.rb', line 248
def at_css(selector)
@doc.root.element RexleCSS.new(selector).to_xpath
end
|
#attribute(key) ⇒ Object
1423
|
# File 'lib/rexle.rb', line 1423
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
1424
|
# File 'lib/rexle.rb', line 1424
def attributes() @doc.attributes end
|
#clone ⇒ Object
244
245
246
|
# File 'lib/rexle.rb', line 244
def clone()
Rexle.new self.to_a
end
|
#content(options = {}) ⇒ Object
1488
1489
1490
|
# File 'lib/rexle.rb', line 1488
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#css(selector) ⇒ Object
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/rexle.rb', line 252
def css(selector)
a = selector.split(',').flat_map do |x|
@doc.root.xpath RexleCSS.new(x).to_xpath
end
a.shift if self.kind_of? Rexle
return a
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
1442
1443
1444
1445
1446
|
# File 'lib/rexle.rb', line 1442
def delete(xpath)
@doc.xpath(xpath).each {|e| e.delete; e = nil }
end
|
#element(xpath) ⇒ Object
1450
|
# File 'lib/rexle.rb', line 1450
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
1451
|
# File 'lib/rexle.rb', line 1451
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
1452
|
# File 'lib/rexle.rb', line 1452
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
|
# File 'lib/rexle.rb', line 1401
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
1461
1462
1463
|
# File 'lib/rexle.rb', line 1461
def root()
@doc.elements.first
end
|
#text(xpath) ⇒ Object
1460
|
# File 'lib/rexle.rb', line 1460
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
1453
|
# File 'lib/rexle.rb', line 1453
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
1455
1456
1457
1458
|
# File 'lib/rexle.rb', line 1455
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
1465
1466
1467
|
# File 'lib/rexle.rb', line 1465
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
|
# File 'lib/rexle.rb', line 1469
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
263
264
265
|
# File 'lib/rexle.rb', line 263
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|