Module: Virginity::Fixes

Defined in:
lib/virginity/fixes.rb

Constant Summary collapse

EQUALS_SIGN_WITHOUT_HEX =
/=(([^0-9^A-F])|(.[^0-9^A-F]))/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fix_faulty_qp_chars(s) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/virginity/fixes.rb', line 104

def self.fix_faulty_qp_chars(s)
  x = Virginity::LineFolding::unfold_and_split(s)
  y = x.map do |l|
    if l =~ /QUOTED\-PRINTABLE/
      l =~ /(.*):(.*)/
      prevalue, value = $1, $2
      value = value.gsub(EQUALS_SIGN_WITHOUT_HEX) { |s| " #{$1}" }
      "#{prevalue}:#{value}"
    else
      l
    end
  end
  y.join("\n")
end

.guess_latin1(s) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/virginity/fixes.rb', line 119

def self.guess_latin1(s)
  x = Vcard.new(s).super_clean!
  x.fields.each do |f|
    begin
      f.to_s.encode('UTF-8//TRANSLIT', Encoding::UTF_8)
      f.to_s.force_encoding(LATIN1).encode
    rescue EncodingError
      print "\tguessing Latin1 for #{f.to_s.inspect}"
      f.params << Param.new("CHARSET", "Latin1")
      f.clean_charsets!
      raise "GAAAAAAAAH!" unless f.to_s.is_utf8?
      puts "\t-->\t" + f.to_s.inspect
    end
  end
  x.super_clean!.to_s
end

.line_parts(line) ⇒ Object



36
37
38
39
40
41
# File 'lib/virginity/fixes.rb', line 36

def self.line_parts(line)
  ContentLine::line_parts(line)
rescue InvalidEncoding
  # so, it's invalid 3.0 encoded, could it be a 2.1-encoding?
  DirectoryInformation::line21_parts(line)
end

.photo_folding_like_apple(value, options = {}) ⇒ Object

FIXME –> width can’t be greater than width in the normal folding method… but it is now :-(



44
45
46
47
48
49
# File 'lib/virginity/fixes.rb', line 44

def self.photo_folding_like_apple(value, options = {})
  width = options[:width] || 78
  line_ending = (options[:windows_line_endings] ? "\r\n" : "\n")
  s = line_ending + "  " + value.gsub(/.{#{width-2},#{width-2}}/) {|x| x + line_ending + "  "}
  s.sub(/#{line_ending}  $/,"") # remove the last line ending if if it so happens to be that the last line is 'empty'
end

.reencode_qp(qp, type = :value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/virginity/fixes.rb', line 88

def self.reencode_qp(qp, type=:value)
  decoded = EncodingDecoding::decode_quoted_printable(qp)
  case type
  when nil
    @value = EncodingDecoding::encode_text decoded
  when :separated
    @value = EncodingDecoding::encode_text_list [decoded]
  when :structured
    @value = EncodingDecoding::encode_structured_text [decoded]
  else
    raise TypeError, type
  end
end

.remove_ascii_ctl_chars(s) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/virginity/fixes.rb', line 79

def self.remove_ascii_ctl_chars(s)
  ctl = (0..31).to_a + [127]
  v = ""
  s.each_byte do |c|
    v << c unless ctl.include? c
  end
  v
end

.sane_line_endings(s) ⇒ Object



51
52
53
# File 'lib/virginity/fixes.rb', line 51

def self.sane_line_endings(s)
  s.gsub(LineFolding::LINE_ENDING, "\n")
end

.should_be_folded?(line) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/virginity/fixes.rb', line 55

def self.should_be_folded?(line)
  return false if line =~ /\A $/ # TODO: clarify this, it was line.first, which takes the first string.
  if line.include?(":") # probably a field
    unless line[0] == ":" or line.split(":").first.match(/[\(\)]/)
      # it is a name then, so it's a new field
      return false
    end
  end
  true
end

.unfold_wrongly_folded_lines(s) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/virginity/fixes.rb', line 66

def self.unfold_wrongly_folded_lines(s)
  x = ""
  sane_line_endings(s).split("\n").each do |line|
    if should_be_folded?(line.dup)
      x << " " + line
    else
      x << line
    end
    x << "\n"
  end
  x
end

Instance Method Details

#non_empty_lines(faulty_lines) ⇒ Object

(needed for simon3.vcf)



20
21
22
# File 'lib/virginity/fixes.rb', line 20

def non_empty_lines(faulty_lines) # (needed for simon3.vcf)
  faulty_lines.reject {|line| line.empty? }
end

#remove_spaces_from_base64_lines(faulty_lines) ⇒ Object

for osx more info on this Base64-line: www.imc.org/imc-vcard/mail-archive/msg00555.html



26
27
28
29
30
31
32
33
34
# File 'lib/virginity/fixes.rb', line 26

def remove_spaces_from_base64_lines(faulty_lines)
  faulty_lines.map do |line|
    if line =~ /BASE64/ # FIXME, this will break if the value contains this string. --> so let's do this in the field-class?
      line.gsub(/\s/, "")
    else
      line
    end
  end
end

#unfold_faulty_qp_lines(faulty_lines) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/virginity/fixes.rb', line 3

def unfold_faulty_qp_lines(faulty_lines)
  lines = faulty_lines.dup
  loop do # unfold line that do not begin with " " but are encoded as QP
    changed = false
    lines.each_with_index do |line, i|
      # if line is QP, ends with equals and is not the last line
      if line =~ /ENCODING=QUOTED-PRINTABLE/i and line =~ /=$/ and lines.length > i+1
        lines[i] = lines[i].chomp('=') # remove the soft-line break, the last character, (=)...
        lines[i] += lines.delete_at(i+1) # ...and add the next line to the failing line.
        changed = true
      end
    end
    break unless changed
  end
  lines
end