lib/vpim/rfc2425.rb in vpim-0.16 vs lib/vpim/rfc2425.rb in vpim-0.17
- old
+ new
@@ -1,10 +1,8 @@
=begin
- $Id: rfc2425.rb,v 1.10 2005/01/01 17:17:01 sam Exp $
+ Copyright (C) 2006 Sam Roberts
- Copyright (C) 2005 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
details.
=end
@@ -60,23 +58,22 @@
module Vpim
# Split on \r\n or \n to get the lines, unfold continued lines (they
# start with ' ' or \t), and return the array of unfolded lines.
#
- # This also implements the (invalid) encoding convention of allowing empty
- # lines to be inserted for readability - it does this by dropping
- # zero-length 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.split(/\r?\n/).each do
- |line|
-
+ 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 << unfolded.pop + line[1, line.size-1]
+ unfolded[-1] << line[1, line.size-1]
elsif( line =~ /^$/ )
else
unfolded << line
end
end
@@ -86,14 +83,14 @@
# Convert a +sep+-seperated list of values into an array of values.
def Vpim.decode_list(value, sep = ',') # :nodoc:
list = []
- value.each(sep) {
- |item|
- list << yield(item) unless item =~ %r{^\s*#{sep}?$}
- }
+ value.each(sep) do |item|
+ item.chomp!(sep)
+ list << yield(item)
+ end
list
end
# Convert a RFC 2425 date into an array of [year, month, day].
def Vpim.decode_date(v) # :nodoc:
@@ -158,28 +155,44 @@
#
# text_list
# Convert a RFC 2425 date-list into an array of dates.
def Vpim.decode_date_list(v) # :nodoc:
- dates = Vpim.decode_list(v) { |date| Vpim.decode_date(date) }
+ Vpim.decode_list(v) do |date|
+ date.strip!
+ if date.length > 0
+ Vpim.decode_date(date)
+ end
+ end.compact
end
# Convert a RFC 2425 time-list into an array of times.
def Vpim.decode_time_list(v) # :nodoc:
- times = Vpim.decode_list(v) { |time| Vpim.decode_time(time) }
+ Vpim.decode_list(v) do |time|
+ time.strip!
+ if time.length > 0
+ Vpim.decode_time(time)
+ end
+ end.compact
end
# Convert a RFC 2425 date-time-list into an array of date-times.
def Vpim.decode_date_time_list(v) # :nodoc:
- datetimes = Vpim.decode_list(v) { |datetime| Vpim.decode_date_time(datetime) }
+ Vpim.decode_list(v) do |datetime|
+ datetime.strip!
+ if datetime.length > 0
+ Vpim.decode_date_time(datetime)
+ end
+ end.compact
end
# Convert RFC 2425 text into a String.
# \\ -> \
# \n -> NL
# \N -> NL
# \, -> ,
def Vpim.decode_text(v) # :nodoc:
+ # FIXME - this will fail for "\\,"!
v.gsub(/\\[nN]/, "\n").gsub(/\\,/, ",").gsub(/\\\\/) { |m| "\\" }
end
# Unfold the lines in +card+, then return an array of one Field object per