lib/vpim/rfc2425.rb in vpim-0.695 vs lib/vpim/rfc2425.rb in vpim-13.11.11
- old
+ new
@@ -1,5 +1,6 @@
+# -*- encoding : utf-8 -*-
=begin
Copyright (C) 2008 Sam Roberts
This library is free software; you can redistribute it and/or modify it
under the same terms as the ruby language itself, see the file COPYING for
@@ -19,17 +20,17 @@
# Note: added ' ' to allowed because its produced by highrisehq.com (X-GOOGLE TALK:)
NAME = '[-a-z0-9_/][-a-z0-9_/ ]*'
# <"> <Any character except CTLs, DQUOTE> <">
QSTR = '"([^"]*)"'
-
+
# *<Any character except CTLs, DQUOTE, ";", ":", ",">
PTEXT = '([^";:,]+)'
-
+
# param-value = ptext / quoted-string
PVALUE = "(?:#{QSTR}|#{PTEXT})"
-
+
# param = name "=" param-value *("," param-value)
# Note: v2.1 allows a type or encoding param-value to appear without the type=
# or the encoding=. This is hideous, but we try and support it, if there
# is no "=", then $2 will be "", and we will treat it as a v2.1 param.
PARAM = ";(#{NAME})(=?)((?:#{PVALUE})?(?:,#{PVALUE})*)"
@@ -74,33 +75,40 @@
# start with ' ' or \t), and return the array of unfolded lines.
#
# This also supports the (invalid) encoding convention of allowing empty
# lines to be inserted for readability - it does this by dropping zero-length
# lines.
- def Vpim.unfold(card) #:nodoc:
- unfolded = []
-
- card.each do |line|
- line.chomp!
- # If it's a continuation line, add it to the last.
- # If it's an empty line, drop it from the input.
- if( line =~ /^[ \t]/ )
- unfolded[-1] << line[1, line.size-1]
- elsif( line =~ /^$/ )
- else
- unfolded << line
- end
+ #
+ # Also supports an the QUOTED-PRINTABLE soft line-break as described here:
+ # http://en.wikipedia.org/wiki/Quoted-printable
+ #
+ def Vpim.unfold(card) # :nodoc:
+ unfolded = []
+ # Ruby 1.9's String can no longer be iterated with #each, so the following
+ # code, which used to work with String, or Array, or File, or anything else
+ # which produced lines when iterated, now just works with String. Sucky.
+ card.each_line do |line|
+ line.chomp!
+ # If it's a continuation line, add it to the last.
+ # If it's an empty line, drop it from the input.
+ if( line =~ /^[ \t]/ )
+ unfolded[-1] << line[1, line.size-1]
+ elsif (unfolded.last && unfolded.last =~ /;ENCODING=QUOTED-PRINTABLE:.*?=$/)
+ unfolded.last << line
+ elsif( line =~ /^$/ )
+ else
+ unfolded << line
end
-
- unfolded
+ end
+ unfolded
end
# Convert a +sep+-seperated list of values into an array of values.
def Vpim.decode_list(value, sep = ',') # :nodoc:
list = []
-
- value.each(sep) do |item|
+
+ value.each_line(sep) do |item|
item.chomp!(sep)
list << yield(item)
end
list
end
@@ -243,18 +251,18 @@
# just support it.
def Vpim.decode_text(v) # :nodoc:
# FIXME - I think this should trim leading and trailing space
v.gsub(/\\(.)/) do
case $1
- when 'n', 'N'
+ when 'n', 'N'
"\n"
else
$1
end
end
end
-
+
def Vpim.encode_text(v) #:nodoc:
v.to_str.gsub(/([\\,;\n])/) { $1 == "\n" ? "\\n" : "\\"+$1 }
end
# v is an Array of String, or just a single String
@@ -284,21 +292,21 @@
def Vpim.encode_paramtext(value)
case value
when %r{\A#{Bnf::SAFECHAR}*\z}
value
else
- raise Vpim::Unencodable, "paramtext #{value.inspect}"
+ raise Vpim::Unencodeable, "paramtext #{value.inspect}"
end
end
def Vpim.encode_paramvalue(value)
case value
when %r{\A#{Bnf::SAFECHAR}*\z}
value
when %r{\A#{Bnf::QSAFECHAR}*\z}
'"' + value + '"'
else
- raise Vpim::Unencodable, "param-value #{value.inspect}"
+ raise Vpim::Unencodeable, "param-value #{value.inspect}"
end
end
# Unfold the lines in +card+, then return an array of one Field object per