Class: TMail::Encoder

Inherits:
Object show all
Includes:
TextUtils
Defined in:
lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb

Overview

FIXME: This class can handle only (euc-jp/shift_jis -> iso-2022-jp).

Constant Summary collapse

BENCODE_DEBUG =
false
SPACER =
"\t"
MAX_LINE_LEN =
70
OPTIONS =
{
  'EUC'  => '-Ej -m0',
  'SJIS' => '-Sj -m0',
  'UTF8' => nil,      # FIXME
  'NONE' => nil
}

Constants included from TextUtils

TextUtils::ATOM_UNSAFE, TextUtils::CONTROL_CHAR, TextUtils::MESSAGE_ID, TextUtils::MIME_ENCODED, TextUtils::MONTH, TextUtils::NKF_FLAGS, TextUtils::PHRASE_UNSAFE, TextUtils::RFC2231_ENCODED, TextUtils::TOKEN_UNSAFE, TextUtils::WDAY, TextUtils::ZONESTR_TABLE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TextUtils

#atom_safe?, #decode_RFC2231, #decode_params, #join_domain, #message_id?, #mime_encoded?, #quote_atom, #quote_boundary, #quote_phrase, #quote_token, #time2str, #timezone_string_to_unixtime, #to_kcode, #token_safe?, #unquote

Constructor Details

#initialize(dest = nil, encoding = nil, eol = "\r\n", limit = nil) ⇒ Encoder

Returns a new instance of Encoder.



194
195
196
197
198
199
200
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 194

def initialize( dest = nil, encoding = nil, eol = "\r\n", limit = nil )
  @f = StrategyInterface.create_dest(dest)
  @opt = OPTIONS[$KCODE]
  @eol = eol
  @preserve_quotes = true
  reset
end

Class Method Details

.encode(str) ⇒ Object



177
178
179
180
181
182
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 177

def Encoder.encode( str )
  e = new()
  e.header_body str
  e.terminate
  e.dest.string
end

Instance Method Details

#destObject



228
229
230
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 228

def dest
  @f
end

#encode_value(str) ⇒ Object



302
303
304
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 302

def encode_value( str )
  str.gsub(TOKEN_UNSAFE) {|s| '%%%02x' % s[0] }
end

#header_body(str) ⇒ Object



255
256
257
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 255

def header_body( str )
  scanadd normalize_encoding(str)
end

#header_line(line) ⇒ Object

add



245
246
247
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 245

def header_line( line )
  scanadd line
end

#header_name(name) ⇒ Object



249
250
251
252
253
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 249

def header_name( name )
  add_text name.split(/-/).map {|i| i.capitalize }.join('-')
  add_text ':'
  add_lwsp ' '
end

#kv_pair(k, v) ⇒ Object

FIXME: implement line folding



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 288

def kv_pair( k, v )
  return if v.nil?
  v = normalize_encoding(v)
  if token_safe?(v)
    add_text k + '=' + v
  elsif not CONTROL_CHAR === v
    add_text k + '=' + quote_token(v)
  else
    # apply RFC2231 encoding
    kv = k + '*=' + "iso-2022-jp'ja'" + encode_value(v)
    add_text kv
  end
end

#lwsp(str) ⇒ Object



265
266
267
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 265

def lwsp( str )
  add_lwsp str.sub(/[\r\n]+[^\r\n]*\z/, '')
end

#meta(str) ⇒ Object



269
270
271
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 269

def meta( str )
  add_text str
end

#normalize_encoding(str) ⇒ Object



210
211
212
213
214
215
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 210

def normalize_encoding( str )
  if @opt
  then NKF.nkf(@opt, str)
  else str
  end
end

#phrase(str) ⇒ Object



277
278
279
280
281
282
283
284
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 277

def phrase( str )
  str = normalize_encoding(str)
  if CONTROL_CHAR === str
    scanadd str
  else
    add_text quote_phrase(str)
  end
end

#preserve_quotesObject



206
207
208
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 206

def preserve_quotes
  @preserve_quotes
end

#preserve_quotes=(bool) ⇒ Object



202
203
204
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 202

def preserve_quotes=( bool )
  @preserve_quotes
end

#puts(str = nil) ⇒ Object



232
233
234
235
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 232

def puts( str = nil )
  @f << str if str
  @f << @eol
end

#resetObject



217
218
219
220
221
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 217

def reset
  @text = ''
  @lwsp = ''
  @curlen = 0
end

#spaceObject Also known as: spc



259
260
261
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 259

def space
  add_lwsp ' '
end

#terminateObject



223
224
225
226
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 223

def terminate
  add_lwsp ''
  reset
end

#text(str) ⇒ Object



273
274
275
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 273

def text( str )
  scanadd normalize_encoding(str)
end

#write(str) ⇒ Object



237
238
239
# File 'lib/action_mailer/vendor/tmail-1.1.0/tmail/encode.rb', line 237

def write( str )
  @f << str
end