nokogiri-plist 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -1,14 +1,20 @@
1
+ load_path = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(load_path) unless $LOAD_PATH.include?(load_path)
3
+
1
4
  begin
2
- require 'nokogiri'
5
+ require 'nokogiri'
3
6
  rescue LoadError
4
7
  require 'rubygems'
5
8
  retry
6
9
  end
7
10
  require 'date'
8
11
 
9
- # TODO: Do this a not so stupid way
10
- require File.join(File.dirname(__FILE__), 'string')
11
- require File.join(File.dirname(__FILE__), 'nokogiri', 'plist', 'generator')
12
- require File.join(File.dirname(__FILE__), 'nokogiri', 'plist', 'parser')
13
- require File.join(File.dirname(__FILE__), 'nokogiri', 'plist')
14
- require File.join(File.dirname(__FILE__), 'nokogiri', 'xml', 'node')
12
+ require 'nokogiri-plist/generator'
13
+ require 'nokogiri-plist/parser'
14
+ require 'nokogiri-plist/node'
15
+ require 'nokogiri-plist/plist'
16
+
17
+ require 'core_ext/string'
18
+
19
+ # Nokogiri::XML::Node.extend NokogiriPList::Node
20
+ Nokogiri::XML::Node.send :include, NokogiriPList::Node
@@ -0,0 +1,79 @@
1
+ module NokogiriPList
2
+
3
+ class Generator
4
+
5
+ def self.indent_size
6
+ 2
7
+ end
8
+
9
+ def self.to_s(value, current_indent = 0)
10
+ # Todo: make these procs and allow custom procs (for data for example)
11
+ case value
12
+ when Array
13
+ tag("array", nil, current_indent) do
14
+ value.inject("") do |result, item|
15
+ result + item.to_plist_xml_unchomped(current_indent + 1)
16
+ end
17
+ end
18
+ when Hash
19
+ tag("dict", nil, current_indent) do
20
+ value.inject("") do |result, (dict_key, dict_value)|
21
+ result + tag("key", dict_key, current_indent + 1).chomp +
22
+ dict_value.to_plist_xml_unchomped
23
+ end
24
+ end
25
+ when TrueClass
26
+ tag("true", nil, current_indent)
27
+ when FalseClass
28
+ tag("false", nil, current_indent)
29
+ when Time
30
+ tag("date", value.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
31
+ when Date # also catches DateTime
32
+ tag("date", value.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
33
+ when String, Symbol
34
+ tag("string", value, current_indent)
35
+ when Fixnum, Bignum, Integer
36
+ tag("integer", value, current_indent)
37
+ when Float
38
+ tag("real", value, current_indent)
39
+ end
40
+ end
41
+
42
+ def self.indent(number)
43
+ " " * (number * indent_size)
44
+ end
45
+
46
+ def self.array_or_hash?(item)
47
+ item.is_a?(Hash) or item.is_a?(Array)
48
+ end
49
+
50
+ def self.tag(name, content=nil, current_indent=0, &block)
51
+ if content or block_given?
52
+ new_line = (["array", "dict"].include? name) ? "\n" : ""
53
+ closing_tag_indent = (new_line != "\n") ? 0 : current_indent
54
+ indent(current_indent) + "<#{name}>" + new_line +
55
+ (block_given? ? yield : content).to_s +
56
+ indent(closing_tag_indent) + "</#{name}>\n"
57
+ else
58
+ indent(current_indent) + "<#{name}/>\n"
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ [String, Symbol, Integer, Float, Date, Time, Hash, Array, TrueClass, FalseClass].each do |klass|
66
+
67
+ klass.class_eval do
68
+
69
+ def to_plist_xml(current_indent = 0)
70
+ self.to_plist_xml_unchomped(current_indent).chomp
71
+ end
72
+
73
+ def to_plist_xml_unchomped(current_indent = 0)
74
+ NokogiriPList::Generator.to_s(self, current_indent)
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,11 @@
1
+ module NokogiriPList
2
+
3
+ module Node
4
+
5
+ def to_plist
6
+ NokogiriPList::Parser.parse(self)
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,62 @@
1
+ module NokogiriPList
2
+
3
+ class Parser
4
+
5
+ class << self
6
+
7
+ def parse(xml, options={})
8
+ @converters = {
9
+ 'integer' => Proc.new { |node| node.content.to_i },
10
+ 'real' => Proc.new { |node| node.content.to_f },
11
+ 'string' => Proc.new { |node| node.content.to_s },
12
+ 'date' => Proc.new { |node| DateTime.parse(node.content) },
13
+ 'true' => Proc.new { |node| true },
14
+ 'false' => Proc.new { |node| false },
15
+ 'dict' => Proc.new { |node| parse_dict node },
16
+ 'array' => Proc.new { |node| parse_array node },
17
+ 'data' => Proc.new { |node| node.content.to_s }
18
+ }.merge(options[:converters] || {})
19
+ @dict_class = options[:dict_class] || Hash
20
+ plist = xml.root
21
+ plist = plist.children.first if plist.name == "plist"
22
+ parse_value_node next_valid_sibling plist
23
+ end
24
+
25
+ def parse_value_node(value_node)
26
+ @converters[value_node.name].call(value_node)
27
+ end
28
+
29
+ def valid_type?(type)
30
+ not @converters[type].nil?
31
+ end
32
+
33
+ def valid_node?(node)
34
+ valid_type?(node.name) or node.name == "key"
35
+ end
36
+
37
+ def parse_dict(node)
38
+ node.xpath('./key').inject(@dict_class.new) do |return_value, key_node|
39
+ return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
40
+ return_value
41
+ end
42
+ end
43
+
44
+ def parse_array(node)
45
+ node.children.inject([]) do |return_value, node|
46
+ return_value << parse_value_node(node) if valid_node? node
47
+ return_value
48
+ end
49
+ end
50
+
51
+ def next_valid_sibling(node)
52
+ until node.nil? or valid_type? node.name
53
+ node = node.next_sibling
54
+ end
55
+ node
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,11 @@
1
+ module Nokogiri
2
+
3
+ class << self
4
+
5
+ def PList(xml)
6
+ ::NokogiriPList::Parser.parse(XML(xml))
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module NokogiriPList
2
+ VERSION = "0.4.0" unless defined? NokogiriPList::VERSION
3
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-plist
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
4
+ prerelease:
5
+ version: 0.4.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Casey Howard
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-08-08 00:00:00 -07:00
13
+ date: 2011-05-23 00:00:00 -07:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,26 +21,31 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
24
  version: "0"
33
25
  type: :development
34
26
  version_requirements: *id001
35
27
  - !ruby/object:Gem::Dependency
36
- name: nokogiri
28
+ name: mocha
37
29
  prerelease: false
38
30
  requirement: &id002 !ruby/object:Gem::Requirement
39
31
  none: false
40
32
  requirements:
41
33
  - - ">="
42
34
  - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
35
  version: "0"
47
- type: :runtime
36
+ type: :development
48
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: nokogiri
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
49
  description: Allows Nokogiri objects to be converted to PLists
50
50
  email:
51
51
  - caseyhoward@caseyhoward.com
@@ -57,11 +57,12 @@ extra_rdoc_files: []
57
57
 
58
58
  files:
59
59
  - lib/nokogiri-plist.rb
60
- - lib/string.rb
61
- - lib/nokogiri/plist/parser.rb
62
- - lib/nokogiri/plist/generator.rb
63
- - lib/nokogiri/xml/node.rb
64
- - lib/nokogiri/plist.rb
60
+ - lib/nokogiri-plist/version.rb
61
+ - lib/nokogiri-plist/node.rb
62
+ - lib/nokogiri-plist/plist.rb
63
+ - lib/nokogiri-plist/parser.rb
64
+ - lib/nokogiri-plist/generator.rb
65
+ - lib/core_ext/string.rb
65
66
  - LICENSE
66
67
  - README.rdoc
67
68
  has_rdoc: true
@@ -78,25 +79,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
80
  - - ">="
80
81
  - !ruby/object:Gem::Version
81
- hash: 3
82
- segments:
83
- - 0
84
82
  version: "0"
85
83
  required_rubygems_version: !ruby/object:Gem::Requirement
86
84
  none: false
87
85
  requirements:
88
86
  - - ">="
89
87
  - !ruby/object:Gem::Version
90
- hash: 23
91
- segments:
92
- - 1
93
- - 3
94
- - 6
95
88
  version: 1.3.6
96
89
  requirements: []
97
90
 
98
91
  rubyforge_project:
99
- rubygems_version: 1.3.7
92
+ rubygems_version: 1.6.2
100
93
  signing_key:
101
94
  specification_version: 3
102
95
  summary: PList parsing capabilities built using Nokogiri
@@ -1,11 +0,0 @@
1
- module Nokogiri
2
-
3
- class << self
4
-
5
- def PList(xml)
6
- Nokogiri::PList::Parser.parse(Nokogiri::XML(xml))
7
- end
8
-
9
- end
10
-
11
- end
@@ -1,83 +0,0 @@
1
- module Nokogiri
2
-
3
- module PList
4
-
5
- class Generator
6
-
7
- def self.indent_size
8
- 2
9
- end
10
-
11
- def self.to_s(value, current_indent = 0)
12
- # Todo: make these procs and allow custom procs (for data for example)
13
- case value
14
- when Array
15
- tag("array", nil, current_indent) do
16
- value.inject("") do |result, item|
17
- result + item.to_plist_xml_unchomped(current_indent + 1)
18
- end
19
- end
20
- when Hash
21
- tag("dict", nil, current_indent) do
22
- value.inject("") do |result, (dict_key, dict_value)|
23
- result + tag("key", dict_key, current_indent + 1).chomp +
24
- dict_value.to_plist_xml_unchomped
25
- end
26
- end
27
- when TrueClass
28
- tag("true", nil, current_indent)
29
- when FalseClass
30
- tag("false", nil, current_indent)
31
- when Time
32
- tag("date", value.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
33
- when Date # also catches DateTime
34
- tag("date", value.strftime('%Y-%m-%dT%H:%M:%SZ'), current_indent)
35
- when String, Symbol
36
- tag("string", value, current_indent)
37
- when Fixnum, Bignum, Integer
38
- tag("integer", value, current_indent)
39
- when Float
40
- tag("real", value, current_indent)
41
- end
42
- end
43
-
44
- def self.indent(number)
45
- " " * (number * indent_size)
46
- end
47
-
48
- def self.array_or_hash?(item)
49
- item.is_a?(Hash) or item.is_a?(Array)
50
- end
51
-
52
- def self.tag(name, content=nil, current_indent=0, &block)
53
- if content or block_given?
54
- new_line = (["array", "dict"].include? name) ? "\n" : ""
55
- closing_tag_indent = (new_line != "\n") ? 0 : current_indent
56
- indent(current_indent) + "<#{name}>" + new_line +
57
- (block_given? ? yield : content).to_s +
58
- indent(closing_tag_indent) + "</#{name}>\n"
59
- else
60
- indent(current_indent) + "<#{name}/>\n"
61
- end
62
- end
63
- end
64
-
65
- end
66
-
67
- end
68
-
69
- [String, Symbol, Integer, Float, Date, Time, Hash, Array, TrueClass, FalseClass].each do |klass|
70
-
71
- klass.class_eval do
72
-
73
- def to_plist_xml(current_indent = 0)
74
- self.to_plist_xml_unchomped(current_indent).chomp
75
- end
76
-
77
- def to_plist_xml_unchomped(current_indent = 0)
78
- Nokogiri::PList::Generator.to_s(self, current_indent)
79
- end
80
-
81
- end
82
-
83
- end
@@ -1,66 +0,0 @@
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
@@ -1,15 +0,0 @@
1
- module Nokogiri
2
-
3
- module XML
4
-
5
- class Node
6
-
7
- def to_plist
8
- Nokogiri::PList::Parser.parse(self)
9
- end
10
-
11
- end
12
-
13
- end
14
-
15
- end