nokogiri-plist 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -4
- data/lib/nokogiri/plist/generator.rb +1 -0
- data/lib/nokogiri/plist/parser.rb +50 -44
- data/lib/nokogiri/plist/parser.rb~ +66 -0
- data/lib/nokogiri/plist.rb +1 -1
- data/lib/nokogiri/plist.rb~ +11 -0
- data/lib/nokogiri/xml/node.rb +1 -1
- data/lib/nokogiri/xml/node.rb~ +15 -0
- data/lib/nokogiri-plist.rb +6 -2
- metadata +10 -25
data/README.rdoc
CHANGED
@@ -34,15 +34,23 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
|
|
34
34
|
|
35
35
|
> require 'nokogiri-plist'
|
36
36
|
|
37
|
+
Parse the plist XML
|
38
|
+
|
37
39
|
> plist = Nokogiri::PList(open('test.plist'))
|
38
40
|
=> { "Beers" => ["Black Butte", "Steel Reserve", "Bass Pale Ale"], "Beers Drank" => 4123, "Beer Drinker" => "Casey"}
|
39
41
|
|
42
|
+
Read a dict property thats value is an array
|
43
|
+
|
40
44
|
> plist["Beers"]
|
41
45
|
=> ["Black Butte", "Steel Reserve", "Bass Pale Ale"]
|
42
46
|
|
47
|
+
Read another value
|
48
|
+
|
43
49
|
> plist["Beer Drinker"]
|
44
50
|
=> "Casey"
|
45
51
|
|
52
|
+
Turn it into a string containing XML
|
53
|
+
|
46
54
|
> puts plist.to_plist_xml
|
47
55
|
<dict>
|
48
56
|
<key>Beers</key>
|
@@ -55,9 +63,13 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
|
|
55
63
|
<key>Beer Drinker</key><string>Casey</string>
|
56
64
|
</dict>
|
57
65
|
|
66
|
+
Convert a string into a plist XML string
|
67
|
+
|
58
68
|
> "beer".to_plist_xml
|
59
69
|
=> "<string>beer</string>"
|
60
70
|
|
71
|
+
Convert an array to a plist XML string
|
72
|
+
|
61
73
|
> puts (1..3).to_a.to_plist_xml
|
62
74
|
<array>
|
63
75
|
<integer>1</integer>
|
@@ -65,16 +77,15 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
|
|
65
77
|
<integer>3</integer>
|
66
78
|
</array>
|
67
79
|
|
80
|
+
|
68
81
|
== REQUIREMENTS:
|
69
82
|
|
70
|
-
*
|
83
|
+
* Ruby (tested with 1.9.1 and 1.8.7)
|
71
84
|
* nokogiri
|
72
85
|
* shoulda and mocha (only if you want to run the tests)
|
73
86
|
|
74
87
|
|
75
88
|
== INSTALL:
|
76
89
|
|
77
|
-
|
78
|
-
|
79
|
-
* gem install nokogiri-plist
|
90
|
+
* [sudo] gem install nokogiri-plist
|
80
91
|
|
@@ -4,53 +4,59 @@ module Nokogiri
|
|
4
4
|
|
5
5
|
class Parser
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
def valid_type?(type)
|
28
|
-
not @converters[type].nil?
|
29
|
-
end
|
30
|
-
|
31
|
-
def valid_node?(node)
|
32
|
-
valid_type?(node.name) or node.name == "key"
|
33
|
-
end
|
34
|
-
|
35
|
-
def parse_dict(node)
|
36
|
-
node.xpath('./key').inject({}) do |return_value, key_node|
|
37
|
-
return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
|
38
|
-
return_value
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def parse(xml, options={})
|
10
|
+
@converters = {
|
11
|
+
'integer' => Proc.new { |node| node.content.to_i },
|
12
|
+
'real' => Proc.new { |node| node.content.to_f },
|
13
|
+
'string' => Proc.new { |node| node.content.to_s },
|
14
|
+
'date' => Proc.new { |node| DateTime.parse(node.content) },
|
15
|
+
'true' => Proc.new { |node| true },
|
16
|
+
'false' => Proc.new { |node| false },
|
17
|
+
'dict' => Proc.new { |node| parse_dict node },
|
18
|
+
'array' => Proc.new { |node| parse_array node },
|
19
|
+
'data' => Proc.new { |node| node.content.to_s }
|
20
|
+
}.merge(options[:converters] || {})
|
21
|
+
@dict_class = options[:dict_class] || Hash
|
22
|
+
plist = xml.root
|
23
|
+
plist = plist.children.first if plist.name == "plist"
|
24
|
+
parse_value_node next_valid_sibling plist
|
39
25
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
node.children.inject([]) do |return_value, node|
|
44
|
-
return_value << parse_value_node(node) if valid_node? node
|
45
|
-
return_value
|
26
|
+
|
27
|
+
def parse_value_node(value_node)
|
28
|
+
@converters[value_node.name].call(value_node)
|
46
29
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
30
|
+
|
31
|
+
def valid_type?(type)
|
32
|
+
not @converters[type].nil?
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid_node?(node)
|
36
|
+
valid_type?(node.name) or node.name == "key"
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_dict(node)
|
40
|
+
node.xpath('./key').inject(@dict_class.new) do |return_value, key_node|
|
41
|
+
return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
|
42
|
+
return_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_array(node)
|
47
|
+
node.children.inject([]) do |return_value, node|
|
48
|
+
return_value << parse_value_node(node) if valid_node? node
|
49
|
+
return_value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def next_valid_sibling(node)
|
54
|
+
until node.nil? or valid_type? node.name
|
55
|
+
node = node.next_sibling
|
56
|
+
end
|
57
|
+
node
|
52
58
|
end
|
53
|
-
|
59
|
+
|
54
60
|
end
|
55
61
|
|
56
62
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
|
3
|
+
module PList
|
4
|
+
|
5
|
+
class Parser
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def parse(xml, options={})
|
10
|
+
@converters = {
|
11
|
+
'integer' => Proc.new { |node| node.content.to_i },
|
12
|
+
'real' => Proc.new { |node| node.content.to_f },
|
13
|
+
'string' => Proc.new { |node| node.content.to_s },
|
14
|
+
'date' => Proc.new { |node| DateTime.parse(node.content) },
|
15
|
+
'true' => Proc.new { |node| true },
|
16
|
+
'false' => Proc.new { |node| false },
|
17
|
+
'dict' => Proc.new { |node| parse_dict node },
|
18
|
+
'array' => Proc.new { |node| parse_array node },
|
19
|
+
'data' => Proc.new { |node| node.content.to_s }
|
20
|
+
}.merge(options[:converters] || {})
|
21
|
+
@dict_class = options[:dict_class] || Hash
|
22
|
+
plist = xml.root
|
23
|
+
plist = plist.children.first if plist.name == "plist"
|
24
|
+
parse_value_node next_valid_sibling plist
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_value_node(value_node)
|
28
|
+
@converters[value_node.name].call(value_node)
|
29
|
+
end
|
30
|
+
|
31
|
+
def valid_type?(type)
|
32
|
+
not @converters[type].nil?
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid_node?(node)
|
36
|
+
valid_type?(node.name) or node.name == "key"
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_dict(node)
|
40
|
+
node.xpath('./key').inject(@dict_class.new) do |return_value, key_node|
|
41
|
+
return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
|
42
|
+
return_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_array(node)
|
47
|
+
node.children.inject([]) do |return_value, node|
|
48
|
+
return_value << parse_value_node(node) if valid_node? node
|
49
|
+
return_value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def next_valid_sibling(node)
|
54
|
+
until node.nil? or valid_type? node.name
|
55
|
+
node = node.next_sibling
|
56
|
+
end
|
57
|
+
node
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/nokogiri/plist.rb
CHANGED
data/lib/nokogiri/xml/node.rb
CHANGED
data/lib/nokogiri-plist.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nokogiri-plist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 23
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 3
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Casey Howard
|
@@ -15,23 +14,10 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-30 00:00:00 -07:00
|
19
18
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
22
|
-
name: rspec
|
23
|
-
prerelease: false
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ">="
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
33
|
-
type: :development
|
34
|
-
version_requirements: *id001
|
19
|
+
dependencies: []
|
20
|
+
|
35
21
|
description: Allows Nokogiri objects to be converted to PLists
|
36
22
|
email:
|
37
23
|
- caseyhoward@caseyhoward.com
|
@@ -45,7 +31,10 @@ files:
|
|
45
31
|
- lib/nokogiri-plist.rb
|
46
32
|
- lib/string.rb
|
47
33
|
- lib/nokogiri/plist/parser.rb
|
34
|
+
- lib/nokogiri/plist/parser.rb~
|
48
35
|
- lib/nokogiri/plist/generator.rb
|
36
|
+
- lib/nokogiri/plist.rb~
|
37
|
+
- lib/nokogiri/xml/node.rb~
|
49
38
|
- lib/nokogiri/xml/node.rb
|
50
39
|
- lib/nokogiri/plist.rb
|
51
40
|
- LICENSE
|
@@ -60,20 +49,16 @@ rdoc_options: []
|
|
60
49
|
require_paths:
|
61
50
|
- lib
|
62
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
52
|
requirements:
|
65
53
|
- - ">="
|
66
54
|
- !ruby/object:Gem::Version
|
67
|
-
hash: 3
|
68
55
|
segments:
|
69
56
|
- 0
|
70
57
|
version: "0"
|
71
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
59
|
requirements:
|
74
60
|
- - ">="
|
75
61
|
- !ruby/object:Gem::Version
|
76
|
-
hash: 23
|
77
62
|
segments:
|
78
63
|
- 1
|
79
64
|
- 3
|
@@ -81,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
66
|
version: 1.3.6
|
82
67
|
requirements: []
|
83
68
|
|
84
|
-
rubyforge_project:
|
85
|
-
rubygems_version: 1.3.
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.6
|
86
71
|
signing_key:
|
87
72
|
specification_version: 3
|
88
73
|
summary: PList parsing capabilities built using Nokogiri
|