plist.newline 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed4d6f700b002cacfed0bbad6c2030fac4e88f38
4
+ data.tar.gz: 5390812b66f121a78c3bced5d8cc8380ecdf0514
5
+ SHA512:
6
+ metadata.gz: 151ff81767f020c2fc5db1d13b3f7f68bfb042fa7086f0e1dd791e9f0057939c9343115610dbf7b44e20c4ff630ee8eed6db023a5fa014868bbdf8eb380a022c
7
+ data.tar.gz: f398bd3464ac90be712b14514a9ad5feb28607ba9b465c051643fc07e81aa99e4a0b02412930f0859166d3cd50bf6b1f71b3be4eb7f5a9b0d7c24363f1949e67
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ rdoc
2
+ coverage
3
+ pkg
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - ruby-head
10
+ - jruby-head
11
+ - 1.8.7
12
+ - ree
data/CHANGELOG ADDED
@@ -0,0 +1,103 @@
1
+ = plist - All-purpose Property List manipulation library
2
+
3
+ === Release version 3.0.0!
4
+
5
+ 2010-02-23:
6
+ * Ruby 1.9.x compatibility!
7
+
8
+ 2010-02-16:
9
+ * excise a bunch of unnecessary @@ variables
10
+ * fix up some tests for cross-version compatibility
11
+
12
+ 2010-02-14:
13
+ * generalized cleanup:
14
+ * fix old file headers
15
+ * modernize rakefile
16
+ * clean up rdoc
17
+
18
+ 2010-01-08:
19
+ * move from RubyForge Subversion to GitHub
20
+
21
+ 2007-02-22 (r81):
22
+ * make the plist parser accept strings contain XML or any object that responds to #read (File and StringIO being the intended targets here). Test and idea contributed by Chuck Remes.
23
+
24
+ 2006-09-20 (r80):
25
+ * tweak a comment in generator.rb to make it clear that we're not using Base64.b64encode because it's broken.
26
+
27
+ === Release version 3.0.0!
28
+
29
+ 2006-09-20 (r77 - r79):
30
+ * move IndentedString inside Plist::Emit and :nodoc: it
31
+ * Tag 3.0.0! (from rev 78)
32
+
33
+ 2006-09-19 (r73 - r75):
34
+ * Really fix the rakefile this time (apparently I deleted some code that I needed...)
35
+ * alter the fix_whitespace rake task to ignore the assets directory
36
+ * cleanup whitespace
37
+
38
+ 2006-09-18 (r70 - r72):
39
+ * Update this file ;)
40
+ * Fix Rakefile
41
+ * gem install -t now works correctly
42
+ * Remove super-sekr1t rdoc staging area from rdoc publishing task
43
+
44
+ 2006-09-15 (r64 - r69):
45
+ * Change behavior of empty collection elements to match What Apple Does
46
+ * Fix some gem packaging infrastructure
47
+
48
+ 2006-09-13 (r61 - r63):
49
+ * Merge generator injection removal branch into trunk!
50
+
51
+ 2006-09-13 (r52 - r60):
52
+ * Fix indentation/newlines in generator (finally!)
53
+ * Refix indentation to be more faithful to the way Apple emits their plists
54
+ * Remove horrific regex and replace it with proper comment parsing
55
+ * Empty plists return nil when parsed
56
+ * Sort hash keys before emitting (now we can test multi-element hashes!)
57
+ * Inject #<=> into Symbol so that sorting Symbol-keyed hashes won't freak out
58
+
59
+ 2006-09-12 (r47 - r51):
60
+ * More test rejiggering
61
+ * New tests to expose some bugs
62
+
63
+ 2006-09-10 (r33 - r46):
64
+ * Update tests for new generator code
65
+ * Rejigger some tests
66
+ * Make the generator try to call #to_plist_node on any object it tries to serialize, thus allowing class authors to define how their objects will be serialized
67
+ * Marshal.dump unrecognized objects into <data> elements
68
+ * Make the parser strip out comments and Marshal.load <data> elements if possible
69
+ * Update some rdoc
70
+
71
+ === Release version 2.1.1!
72
+
73
+ 2006-09-10 (r31 - r32):
74
+ * Added encoding / decoding for entities (&amp; etc)
75
+ * Changed parsing of <data> elements to return StringIO objects
76
+ * Fixed bug with empty <key> tags
77
+
78
+ 2006-08-24 (r25 - r30):
79
+ * Invert ownership of methods in the generator, allowing us to remove the self.extend(self)
80
+ * New branch to remove method inject from parser
81
+
82
+ 2006-08-23 (r22 - r24):
83
+ * Add rcov task to Rakefile
84
+ * Add some tests
85
+
86
+ 2006-08-20 (r9 - r21):
87
+ * Add a bunch of rdoc and rdoc infrastructure
88
+ * Add rake task to clean up errant whitespace
89
+ * Spin off a branch to remove a bunch of method injection in the generator code
90
+ * Rename some tests for clarity's sake
91
+ * Replace NARF generation code with Ben's generation code
92
+ * Update tests
93
+ * This broke indentation (will be fixed later)
94
+ * Add Plist::Emit.dump, so you can dump objects which don't include Plist::Emit, update tests to match
95
+ * Fix a bug with the method that wraps output in the plist header/footer
96
+
97
+ 2006-08-19 (r1 - r8):
98
+ * The beginnings of merging the plist project into the NARF plist library (under the plist project's name)
99
+ * fancier project infrastructure (more tests, Rakefile, the like)
100
+ * Add/update copyright notices in the source files
101
+ * Move a bunch of documentation out to README
102
+ * Split library into chunks
103
+ * Properly delete files when cleaning up from tests
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2010, Ben Bleything and Patrick May
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
15
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
16
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,164 @@
1
+ = All-purpose Property List manipulation library
2
+
3
+ Plist is a library to manipulate Property List files, also known as plists. It can parse plist files into native Ruby data structures as well as generating new plist files from your Ruby objects.
4
+
5
+ == Info
6
+
7
+ This version of the plist gem fixes the additional whitespace in front of every line of multiline text values.
8
+
9
+ == Usage
10
+
11
+ === Install
12
+ gem install plist.newline
13
+
14
+ === Parsing
15
+
16
+ result = Plist::parse_xml('path/to/example.plist')
17
+
18
+ result.class
19
+ => Hash
20
+
21
+ "#{result['FirstName']} #{result['LastName']}"
22
+ => "John Public"
23
+
24
+ result['ZipPostal']
25
+ => "12345"
26
+
27
+ ==== Example Property List
28
+
29
+ <?xml version="1.0" encoding="UTF-8"?>
30
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
31
+ <plist version="1.0">
32
+ <dict>
33
+ <key>FirstName</key>
34
+ <string>John</string>
35
+
36
+ <key>LastName</key>
37
+ <string>Public</string>
38
+
39
+ <key>StreetAddr1</key>
40
+ <string>123 Anywhere St.</string>
41
+
42
+ <key>StateProv</key>
43
+ <string>CA</string>
44
+
45
+ <key>City</key>
46
+ <string>Some Town</string>
47
+
48
+ <key>CountryName</key>
49
+ <string>United States</string>
50
+
51
+ <key>AreaCode</key>
52
+ <string>555</string>
53
+
54
+ <key>LocalPhoneNumber</key>
55
+ <string>5551212</string>
56
+
57
+ <key>ZipPostal</key>
58
+ <string>12345</string>
59
+ </dict>
60
+ </plist>
61
+
62
+ === Generation
63
+
64
+ plist also provides the ability to generate plists from Ruby objects. The following Ruby classes are converted into native plist types:
65
+ Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false
66
+
67
+ * +Array+ and +Hash+ are both recursive; their elements will be converted into plist nodes inside the <array> and <dict> containers (respectively).
68
+ * +IO+ (and its descendants) and +StringIO+ objects are read from and their contents placed in a <data> element.
69
+ * User classes may implement +to_plist_node+ to dictate how they should be serialized; otherwise the object will be passed to <tt>Marshal.dump</tt> and the result placed in a <data> element. See below for more details.
70
+
71
+ ==== Creating a plist
72
+
73
+ There are two ways to generate complete plists. Given an object:
74
+
75
+ obj = [1, :two, {'c' => 0xd}]
76
+
77
+ If you've mixed in <tt>Plist::Emit</tt> (which is already done for +Array+ and +Hash+), you can simply call +to_plist+:
78
+
79
+ obj.to_plist
80
+
81
+ This is equivalent to calling <tt>Plist::Emit.dump(obj)</tt>. Either one will yield:
82
+
83
+ <?xml version="1.0" encoding="UTF-8"?>
84
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
85
+ <plist version="1.0">
86
+ <array>
87
+ <integer>1</integer>
88
+ <string>two</string>
89
+ <dict>
90
+ <key>c</key>
91
+ <integer>13</integer>
92
+ </dict>
93
+ </array>
94
+ </plist>
95
+
96
+ You can also dump plist fragments by passing +false+ as the second parameter:
97
+
98
+ Plist::Emit.dump('holy cow!', false)
99
+ => "<string>holy cow!</string>"
100
+
101
+ ==== Custom serialization
102
+
103
+ If your class can be safely coerced into a native plist datatype, you can implement +to_plist_node+. Upon encountering an object of a class it doesn't recognize, the plist library will check to see if it responds to +to_plist_node+, and if so, insert the result of that call into the plist output.
104
+
105
+ An example:
106
+
107
+ class MyFancyString
108
+ ...
109
+
110
+ def to_plist_node
111
+ return "<string>#{self.defancify}</string>"
112
+ end
113
+ end
114
+
115
+ When you attempt to serialize a +MyFancyString+ object, the +to_plist_node+ method will be called and the object's contents will be defancified and placed in the plist.
116
+
117
+ If for whatever reason you can't add this method, your object will be serialized with <tt>Marshal.dump</tt> instead.
118
+
119
+ == Links
120
+
121
+ [Project Page] http://plist.rubyforge.org
122
+ [GitHub] http://github.com/bleything/plist
123
+ [RDoc] http://plist.rubyforge.org
124
+
125
+ == Credits
126
+
127
+ plist is maintained by Ben Bleything <mailto:ben@bleything.net> and Patrick May <mailto:patrick@hexane.org>. Patrick wrote most of the code; Ben is a recent addition to the project, having merged in his plist generation library.
128
+
129
+ Other folks who have helped along the way:
130
+
131
+ [<b>Martin Dittus</b>] who pointed out that +Time+ wasn't enough for plist <tt>Dates</tt>, especially those in <tt>~/Library/Cookies/Cookies.plist</tt>
132
+ [<b>Chuck Remes</b>] who pushed Patrick towards implementing <tt>#to_plist</tt>
133
+ [<b>Mat Schaffer</b>] who supplied code and test cases for <tt><data></tt> elements
134
+ [<b>Michael Granger</b>] for encouragement and help
135
+ [<b>Carsten Bormann, Chris Hoffman, Dana Contreras, Hongli Lai, Johan Sørensen</b>] for contributing Ruby 1.9.x compatibility fixes
136
+
137
+ == License and Copyright
138
+
139
+ plist is released under the MIT License.
140
+
141
+ Portions of the code (notably the Rakefile) contain code pulled and/or adapted from other projects. These files contain a comment at the top describing what was used.
142
+
143
+ === MIT License
144
+
145
+ Copyright (c) 2006-2010, Ben Bleything <ben@bleything.net> and Patrick May <patrick@hexane.org>
146
+
147
+ Permission is hereby granted, free of charge, to any person obtaining
148
+ a copy of this software and associated documentation files (the
149
+ "Software"), to deal in the Software without restriction, including
150
+ without limitation the rights to use, copy, modify, merge, publish,
151
+ distribute, sublicense, and/or sell copies of the Software, and to
152
+ permit persons to whom the Software is furnished to do so, subject to
153
+ the following conditions:
154
+
155
+ The above copyright notice and this permission notice shall be included
156
+ in all copies or substantial portions of the Software.
157
+
158
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
159
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
160
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
161
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
162
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
163
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
164
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,154 @@
1
+ #
2
+ # Plist Rakefile
3
+ #
4
+ # Based heavily on Geoffrey Grosenbach's Rakefile for gruff.
5
+ # Includes whitespace-fixing task based on code from Typo.
6
+ #
7
+ # Copyright 2006-2010 Ben Bleything and Patrick May
8
+ # Distributed under the MIT License
9
+ #
10
+
11
+ require 'fileutils'
12
+ require 'rubygems'
13
+ require 'rake'
14
+ require 'rake/testtask'
15
+ require 'rake/packagetask'
16
+ require 'rake/contrib/rubyforgepublisher'
17
+ require 'rubygems/package_task'
18
+
19
+ $:.unshift(File.dirname(__FILE__) + "/lib")
20
+ require 'plist'
21
+
22
+ PKG_NAME = 'plist'
23
+ PKG_VERSION = Plist::VERSION
24
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
25
+
26
+ RELEASE_NAME = "REL #{PKG_VERSION}"
27
+
28
+ RUBYFORGE_PROJECT = "plist"
29
+ RUBYFORGE_USER = ENV['RUBYFORGE_USER']
30
+
31
+ TEST_FILES = Dir.glob('test/test_*')
32
+ TEST_ASSETS = Dir.glob('test/assets/*')
33
+ LIB_FILES = Dir.glob('lib/**/*')
34
+ RELEASE_FILES = [ "Rakefile", "README.rdoc", "CHANGELOG", "LICENSE" ] + LIB_FILES + TEST_FILES + TEST_ASSETS
35
+
36
+ task :default => [ :test ]
37
+ # Run the unit tests
38
+ Rake::TestTask.new { |t|
39
+ t.libs << "test"
40
+ t.test_files = TEST_FILES
41
+ t.verbose = true
42
+ }
43
+
44
+ desc "Clean pkg, coverage, and rdoc; remove .bak files"
45
+ task :clean => [ :clobber_rdoc, :clobber_package, :clobber_coverage ] do
46
+ puts cmd = "find . -type f -name *.bak -delete"
47
+ `#{cmd}`
48
+ end
49
+
50
+ task :clobber_coverage do
51
+ puts cmd = "rm -rf coverage"
52
+ `#{cmd}`
53
+ end
54
+
55
+ desc "Generate coverage analysis with rcov (requires rcov to be installed)"
56
+ task :rcov => [ :clobber_coverage ] do
57
+ puts cmd = "rcov -Ilib --xrefs -T test/*.rb"
58
+ puts `#{cmd}`
59
+ end
60
+
61
+ desc "Strip trailing whitespace and fix newlines for all release files"
62
+ task :fix_whitespace => [ :clean ] do
63
+ RELEASE_FILES.reject {|i| i =~ /assets/}.each do |filename|
64
+ next if File.directory? filename
65
+
66
+ File.open(filename) do |file|
67
+ newfile = ''
68
+ needs_love = false
69
+
70
+ file.readlines.each_with_index do |line, lineno|
71
+ if line =~ /[ \t]+$/
72
+ needs_love = true
73
+ puts "#{filename}: trailing whitespace on line #{lineno}"
74
+ line.gsub!(/[ \t]*$/, '')
75
+ end
76
+
77
+ if line.chomp == line
78
+ needs_love = true
79
+ puts "#{filename}: no newline on line #{lineno}"
80
+ line << "\n"
81
+ end
82
+
83
+ newfile << line
84
+ end
85
+
86
+ if needs_love
87
+ tempname = "#{filename}.new"
88
+
89
+ File.open(tempname, 'w').write(newfile)
90
+ File.chmod(File.stat(filename).mode, tempname)
91
+
92
+ FileUtils.ln filename, "#{filename}.bak"
93
+ FileUtils.ln tempname, filename, :force => true
94
+ File.unlink(tempname)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ desc "Copy documentation to rubyforge"
101
+ task :update_rdoc => [ :rdoc ] do
102
+ Rake::SshDirPublisher.new("#{RUBYFORGE_USER}@rubyforge.org", "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}", "rdoc").upload
103
+ end
104
+
105
+ begin
106
+ require 'rdoc/task'
107
+
108
+ # Generate the RDoc documentation
109
+ RDoc::Task.new do |rdoc|
110
+ rdoc.title = "All-purpose Property List manipulation library"
111
+ rdoc.main = "README.rdoc"
112
+
113
+ rdoc.rdoc_dir = 'rdoc'
114
+ rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'CHANGELOG')
115
+ rdoc.rdoc_files.include('lib/**')
116
+
117
+ rdoc.options = [
118
+ '-H', # show hash marks on method names in comments
119
+ '-N', # show line numbers
120
+ ]
121
+ end
122
+ rescue LoadError
123
+ $stderr.puts "Could not load rdoc tasks"
124
+ end
125
+
126
+ # Create compressed packages
127
+ spec = Gem::Specification.new do |s|
128
+ s.name = PKG_NAME
129
+ s.version = PKG_VERSION
130
+
131
+ s.summary = "All-purpose Property List manipulation library."
132
+ s.description = <<-EOD
133
+ Plist is a library to manipulate Property List files, also known as plists. It can parse plist files into native Ruby data structures as well as generating new plist files from your Ruby objects.
134
+ EOD
135
+
136
+ s.authors = "Ben Bleything and Patrick May"
137
+ s.homepage = "http://plist.rubyforge.org"
138
+
139
+ s.rubyforge_project = RUBYFORGE_PROJECT
140
+
141
+ s.has_rdoc = true
142
+
143
+ s.files = RELEASE_FILES
144
+ s.test_files = TEST_FILES
145
+
146
+ s.autorequire = 'plist'
147
+ end
148
+
149
+ Gem::PackageTask.new(spec) do |p|
150
+ p.gem_spec = spec
151
+ p.need_tar = true
152
+ p.need_zip = true
153
+ end
154
+
@@ -0,0 +1,201 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # = plist
4
+ #
5
+ # Copyright 2006-2010 Ben Bleything and Patrick May
6
+ # Distributed under the MIT License
7
+ #
8
+
9
+ module Plist ; end
10
+
11
+ # === Create a plist
12
+ # You can dump an object to a plist in one of two ways:
13
+ #
14
+ # * <tt>Plist::Emit.dump(obj)</tt>
15
+ # * <tt>obj.to_plist</tt>
16
+ # * This requires that you mixin the <tt>Plist::Emit</tt> module, which is already done for +Array+ and +Hash+.
17
+ #
18
+ # The following Ruby classes are converted into native plist types:
19
+ # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false
20
+ # * +Array+ and +Hash+ are both recursive; their elements will be converted into plist nodes inside the <array> and <dict> containers (respectively).
21
+ # * +IO+ (and its descendants) and +StringIO+ objects are read from and their contents placed in a <data> element.
22
+ # * User classes may implement +to_plist_node+ to dictate how they should be serialized; otherwise the object will be passed to <tt>Marshal.dump</tt> and the result placed in a <data> element.
23
+ #
24
+ # For detailed usage instructions, refer to USAGE[link:files/docs/USAGE.html] and the methods documented below.
25
+ module Plist::Emit
26
+ # Helper method for injecting into classes. Calls <tt>Plist::Emit.dump</tt> with +self+.
27
+ def to_plist(envelope = true)
28
+ return Plist::Emit.dump(self, envelope)
29
+ end
30
+
31
+ # Helper method for injecting into classes. Calls <tt>Plist::Emit.save_plist</tt> with +self+.
32
+ def save_plist(filename)
33
+ Plist::Emit.save_plist(self, filename)
34
+ end
35
+
36
+ # The following Ruby classes are converted into native plist types:
37
+ # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time
38
+ #
39
+ # Write us (via RubyForge) if you think another class can be coerced safely into one of the expected plist classes.
40
+ #
41
+ # +IO+ and +StringIO+ objects are encoded and placed in <data> elements; other objects are <tt>Marshal.dump</tt>'ed unless they implement +to_plist_node+.
42
+ #
43
+ # The +envelope+ parameters dictates whether or not the resultant plist fragment is wrapped in the normal XML/plist header and footer. Set it to false if you only want the fragment.
44
+ def self.dump(obj, envelope = true)
45
+ output = plist_node(obj)
46
+
47
+ output = wrap(output) if envelope
48
+
49
+ return output
50
+ end
51
+
52
+ # Writes the serialized object's plist to the specified filename.
53
+ def self.save_plist(obj, filename)
54
+ File.open(filename, 'wb') do |f|
55
+ f.write(obj.to_plist)
56
+ end
57
+ end
58
+
59
+ private
60
+ def self.plist_node(element)
61
+ output = ''
62
+
63
+ if element.respond_to? :to_plist_node
64
+ output << element.to_plist_node
65
+ else
66
+ case element
67
+ when Array
68
+ if element.empty?
69
+ output << "<array/>\n"
70
+ else
71
+ output << tag('array') {
72
+ element.collect {|e| plist_node(e)}
73
+ }
74
+ end
75
+ when Hash
76
+ if element.empty?
77
+ output << "<dict/>\n"
78
+ else
79
+ inner_tags = []
80
+
81
+ element.keys.sort_by{|k| k.to_s }.each do |k|
82
+ v = element[k]
83
+ inner_tags << tag('key', CGI::escapeHTML(k.to_s))
84
+ inner_tags << plist_node(v)
85
+ end
86
+
87
+ output << tag('dict') {
88
+ inner_tags
89
+ }
90
+ end
91
+ when true, false
92
+ output << "<#{element}/>\n"
93
+ when Time
94
+ output << tag('date', element.utc.strftime('%Y-%m-%dT%H:%M:%SZ'))
95
+ when Date # also catches DateTime
96
+ output << tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ'))
97
+ when String, Symbol, Fixnum, Bignum, Integer, Float
98
+ output << tag(element_type(element), CGI::escapeHTML(element.to_s))
99
+ when IO, StringIO
100
+ element.rewind
101
+ contents = element.read
102
+ # note that apple plists are wrapped at a different length then
103
+ # what ruby's base64 wraps by default.
104
+ # I used #encode64 instead of #b64encode (which allows a length arg)
105
+ # because b64encode is b0rked and ignores the length arg.
106
+ data = "\n"
107
+ Base64::encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
108
+ output << tag('data', data)
109
+ else
110
+ output << comment( 'The <data> element below contains a Ruby object which has been serialized with Marshal.dump.' )
111
+ data = "\n"
112
+ Base64::encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
113
+ output << tag('data', data )
114
+ end
115
+ end
116
+
117
+ return output
118
+ end
119
+
120
+ def self.comment(content)
121
+ return "<!-- #{content} -->\n"
122
+ end
123
+
124
+ def self.tag(type, contents = '', &block)
125
+ out = nil
126
+
127
+ if block_given?
128
+ out = RecursiveString.new
129
+ out << "<#{type}>"
130
+
131
+ out << block.call
132
+
133
+ out << "</#{type}>"
134
+ else
135
+ out = "<#{type}>#{contents.to_s}</#{type}>\n"
136
+ end
137
+
138
+ return out.to_s
139
+ end
140
+
141
+ def self.wrap(contents)
142
+ output = ''
143
+
144
+ output << '<?xml version="1.0" encoding="UTF-8"?>' + "\n"
145
+ output << '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + "\n"
146
+ output << '<plist version="1.0">' + "\n"
147
+
148
+ output << contents
149
+
150
+ output << '</plist>' + "\n"
151
+
152
+ return output
153
+ end
154
+
155
+ def self.element_type(item)
156
+ case item
157
+ when String, Symbol
158
+ 'string'
159
+
160
+ when Fixnum, Bignum, Integer
161
+ 'integer'
162
+
163
+ when Float
164
+ 'real'
165
+
166
+ else
167
+ raise "Don't know about this data type... something must be wrong!"
168
+ end
169
+ end
170
+
171
+ class RecursiveString #:nodoc:
172
+ attr_accessor :indent_string
173
+
174
+ def initialize(str = "\t")
175
+ @indent_string = str
176
+ @contents = ''
177
+ end
178
+
179
+ def to_s
180
+ @contents
181
+ end
182
+
183
+ def <<(val)
184
+ if val.is_a? Array
185
+ val.each do |f|
186
+ self << f
187
+ end
188
+ else
189
+ @contents << val
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ class Array #:nodoc:
196
+ include Plist::Emit
197
+ end
198
+
199
+ class Hash #:nodoc:
200
+ include Plist::Emit
201
+ end