Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String fields that contain line breaks end up with tabs inserted at the start of every new line #14

Closed
nsforge opened this issue Nov 18, 2012 · 5 comments · Fixed by #54

Comments

@nsforge
Copy link

nsforge commented Nov 18, 2012

The plist generation code seems to incorrectly indent the output when writing a string field that has line breaks. Try running the following code:

require 'rubygems'
require 'plist'

hash = {"name"=>"David Smith", "address"=>"123 Smith St\nAnytown 12345"}

hash.save_plist(File.expand_path("~/Test.plist"))

The generated plist has an extra tab character inserted before the "Anytown 12345", which makes the raw plist data "look" like its indented correctly, but actually corrupts the string itself. Here is the generated output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>address</key>
    <string>123 Smith St
    Anytown 12345</string>
    <key>name</key>
    <string>David Smith</string>
</dict>
</plist>

Whereas the correct output would be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>address</key>
    <string>123 Smith St
Anytown 12345</string>
    <key>name</key>
    <string>David Smith</string>
</dict>
</plist>
@quantumgardener
Copy link

I have been able to create a workaround for this. I'm using Plist with Day One Journal files and this problem seriously impacts formatting.

The code I have in place is

doc = ''
doc = Nokogiri::XML(journal.to_plist.gsub! /\t/, '') {|x| x.noblanks}
doc = doc.to_xml(:indent => 1, :indent_text => "\t")
File.open(fn, 'w') {|f| f.write(doc) }  

Where journal is the plist.

@tomandersen
Copy link

Please note that this breaks the entire thing pretty badly.

@cristianbica
Copy link

here's the workarounds I use till the issues will be fixed:

  plist_string = some_hash.to_plist
  plist_string = plist_string.gsub(/&amp;([a-z0-9]+;)/,'&\1')     #fix double encoding issue
  plist_string = plist_string.gsub("/Apple Computer/","/Apple/")  #fix doctype
  plist_string = plist_string.gsub(/([^>]\n)\t+/,'\1')            #fix spacing issue

@mattbrictson
Copy link
Collaborator

Yep, this is definitely a bug. Thanks for the report!

@tboyko
Copy link
Contributor

tboyko commented Nov 30, 2020

Please see PR #54

@mattbrictson mattbrictson linked a pull request Dec 29, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants