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

Defined Under Namespace

Modules: BinaryMarkers Classes: AsciiGenerator, AsciiParser, BinaryGenerator, BinaryParser, OrdSet, SyntaxError, Uid, UnsupportedTypeError, Url, Uuid, XmlGenerator, XmlParser

Constant Summary collapse

VERSION =
'1.0.3'.freeze

Class Method Summary collapse

Class Method Details

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

Generate ASCII (Plain) plist.

Options:

  • 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.

  • gnu_extension whether allow GNUStep extensions for ASCII plist to support serializing more types, default is true.



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

def self.dump_ascii obj, indent_unit: "\t", initial_indent: '', wrap: true, encoding_comment: false, sort_keys: true, gnu_extension: true
  generator = AsciiGenerator.new indent_unit: indent_unit, initial_indent: initial_indent, sort_keys: sort_keys, gnu_extension: gnu_extension
  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

Generate binary plist, the version is auto detected



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

def self.dump_binary obj, options=nil
  generator = BinaryGenerator.new options
  generator.generate obj
  binding.pry if $test
  generator.output.join
end

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

Generate ASCII (Plain) plist.

Options:

  • segment: whether output an XML segment (not wrapped with ‘<?xml>, <DOCTYPE>, <plist>` 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.

  • gnu_dtd: use GNUStep DTD instead (which is a bit different in string escaping), default is false.

  • indent_unit: the indent unit, default value is ‘“t”`, set to 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.



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

def self.dump_xml obj, segment: false, xml_version: '1.0', gnu_dtd: false, 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 gnu_dtd: gnu_dtd, 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|
    if gnu_dtd
      generator.output << %|<!DOCTYPE plist PUBLIC "-//GNUstep//DTD plist 0.9//EN" "http://www.gnustep.org/plist-0_9.xml">\n|
      generator.output << %|<plist>\n|
    else
      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|
    end
    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 detects format.



28
29
30
31
32
33
34
35
36
37
# File 'lib/property-list.rb', line 28

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(text) ⇒ Object

Parse ASCII plist into a Ruby object



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

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

.load_binary(data) ⇒ Object

Parse binary plist into a Ruby object



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

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

.load_file(file_name) ⇒ Object

Load plist from file

Auto detects format



21
22
23
# File 'lib/property-list.rb', line 21

def self.load_file file_name
  load File.binread file_name
end

.load_xml(xml) ⇒ Object

Parse XML plist into a Ruby object



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

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