Module: PropertyList

Defined in:
lib/property-list.rb,
lib/property-list/version.rb,
lib/property-list/xml_parser.rb,
lib/property-list/ascii_parser.rb,
lib/property-list/binary_parser.rb,
lib/property-list/xml_generator.rb,
lib/property-list/binary_markers.rb,
lib/property-list/ascii_generator.rb,
lib/property-list/binary_generator.rb

Overview

Load a plist file

PropertyList.load_xml File.read "some_plist.xml"
PropertyList.load_binary File.binread "some_binary.plist"
PropertyList.load_ascii File.read "some_ascii.strings"
PropertyList.load File.binread "unknown_format.plist"

Generate a plist file data

PropertyList.dump_xml obj
PropertyList.dump_binary obj
PropertyList.dump_ascii obj

Defined Under Namespace

Modules: BinaryMarkers Classes: AsciiGenerator, AsciiParser, BinaryGenerator, BinaryParser, OrderedSet, SyntaxError, UID, URL, UUID, XmlGenerator, XmlParser

Constant Summary collapse

VERSION =
'1.0.1'.freeze

Class Method Summary collapse

Class Method Details

.dump_ascii(obj, indent_unit: "\t", initial_indent: '', wrap: true, encoding_comment: false, sort_keys: true) ⇒ Object

options can be:

:indent_unit

the indent unit, default value is "\t", set to '' if you don’t need indent

:initial_indent

initial indent space, default is '', the indentation per line equals to initial_indent + indent * current_indent_level

:wrap

wrap the top level output with ‘{}’ when obj is a Hash, default is true.

:encoding_comment

add encoding comment ‘!$UTF8$!’ on top of file, default is false

:sort_keys

sort dict keys, default is true



14
15
16
17
18
19
20
# File 'lib/property-list/ascii_generator.rb', line 14

def self.dump_ascii obj, indent_unit: "\t", initial_indent: '', wrap: true, encoding_comment: false, sort_keys: true
  generator = AsciiGenerator.new indent_unit: indent_unit, initial_indent: initial_indent, sort_keys: sort_keys
  generator.output << "// !$*UTF8*$!\n" if encoding_comment
  generator.generate obj, wrap
  generator.output << "\n" if wrap and obj.is_a?(Hash)
  generator.output.join
end

.dump_binary(obj, options = nil) ⇒ Object



4
5
6
7
8
# File 'lib/property-list/binary_generator.rb', line 4

def self.dump_binary obj, options=nil
  generator = BinaryGenerator.new options
  generator.generate obj
  generator.output.join
end

.dump_xml(obj, segment: false, xml_version: '1.0', base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: '') ⇒ Object

options can be:

:segment

whether wrap the xml output is a segment or wrapped with &lt;?xml&gt; and &lt;plist&gt; tags. default is false.

:xml_version

you can also specify "1.1" for www.w3.org/TR/xml11/, default is "1.0", no effect if :segment is set to true

:indent_unit

the indent unit, default value is "\t", set to nil or '' if you don’t need indent

:initial_indent

initial indent space, default is '', the indentation per line equals to initial_indent + indent * current_indent_level

:base64_width

the width of characters per line when serializing data with Base64, default value is 68, must be multiple of 4

:base64_indent

whether indent the Base64 encoded data, you can use false for compatibility to generate same output for other frameworks, default value is true



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/property-list/xml_generator.rb', line 17

def self.dump_xml obj, segment: false, xml_version: '1.0', base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: ''
  if !base64_width.is_a?(Integer) or base64_width <= 0 or base64_width % 4 != 0
    raise ArgumentError, "option :base64_width must be a positive integer and a multiple of 4"
  end

  generator = XmlGenerator.new base64_width: base64_width, base64_indent: base64_indent, indent_unit: indent_unit, initial_indent: initial_indent
  if segment
    generator.generate obj
  else
    generator.output << %|<?xml version="#{xml_version}" encoding="UTF-8"?>\n|
    generator.output << %|<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n|
    generator.output << %|<plist version="1.0">\n|
    generator.generate obj
    generator.output << %|</plist>\n|
  end
  generator.output.join
end

.load(data) ⇒ Object

load plist (binary or xml or ascii) into a ruby object auto detect the format



33
34
35
36
37
38
39
40
41
42
# File 'lib/property-list.rb', line 33

def self.load data
  case data.byteslice(0, 8)
  when /\Abplist\d\d/n
    load_binary data.force_encoding('binary')
  when /\A<\?xml\ /n
    load_xml data.force_encoding('utf-8')
  else
    load_ascii data.force_encoding('utf-8')
  end
end

.load_ascii(data) ⇒ Object



2
3
4
# File 'lib/property-list/ascii_parser.rb', line 2

def self.load_ascii(data)
  AsciiParser.new(data).parse
end

.load_binary(data) ⇒ Object



2
3
4
# File 'lib/property-list/binary_parser.rb', line 2

def self.load_binary(data)
  BinaryParser.new(data).parse
end

.load_xml(xml) ⇒ Object



2
3
4
# File 'lib/property-list/xml_parser.rb', line 2

def self.load_xml xml
  XmlParser.new(xml).parse
end