Class TMail::Encoder
In: lib/tmail/encode.rb
Parent: Object

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

Methods

dest   encode   encode_value   header_body   header_line   header_name   kv_pair   lwsp   meta   new   normalize_encoding   phrase   puts   reset   space   spc   terminate   text   write  

Included Modules

TextUtils

Constants

BENCODE_DEBUG = false unless defined?(BENCODE_DEBUG)
SPACER = "\t"
MAX_LINE_LEN = 70
OPTIONS = { 'EUC' => '-Ej -m0', 'SJIS' => '-Sj -m0', 'UTF8' => nil, # FIXME 'NONE' => nil
METHOD_ID = { ?j => :extract_J, ?e => :extract_E, ?a => :extract_A, ?s => :extract_S

Public Class methods

[Source]

# File lib/tmail/encode.rb, line 153
    def Encoder.encode(str)
      e = new()
      e.header_body str
      e.terminate
      e.dest.string
    end

[Source]

# File lib/tmail/encode.rb, line 170
    def initialize(dest = nil, encoding = nil, eol = "\r\n", limit = nil)
      @f = StrategyInterface.create_dest(dest)
      @opt = OPTIONS[$KCODE]
      @eol = eol
      reset
    end

Public Instance methods

[Source]

# File lib/tmail/encode.rb, line 195
    def dest
      @f
    end

[Source]

# File lib/tmail/encode.rb, line 268
    def encode_value(str)
      str.gsub(RFC2231_UNSAFE) {|s| '%%%02X' % s[0] }
    end

[Source]

# File lib/tmail/encode.rb, line 222
    def header_body(str)
      scanadd normalize_encoding(str)
    end

add

[Source]

# File lib/tmail/encode.rb, line 212
    def header_line(line)
      scanadd line
    end

[Source]

# File lib/tmail/encode.rb, line 216
    def header_name(name)
      add_text name.split(/-/).map {|i| i.capitalize }.join('-')
      add_text ':'
      add_lwsp ' '
    end

FIXME: implement line folding

[Source]

# File lib/tmail/encode.rb, line 255
    def kv_pair(k, v)
      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

[Source]

# File lib/tmail/encode.rb, line 232
    def lwsp(str)
      add_lwsp str.sub(/[\r\n]+[^\r\n]*\z/, '')
    end

[Source]

# File lib/tmail/encode.rb, line 236
    def meta(str)
      add_text str
    end

[Source]

# File lib/tmail/encode.rb, line 177
    def normalize_encoding(str)
      if @opt
      then NKF.nkf(@opt, str)
      else str
      end
    end

[Source]

# File lib/tmail/encode.rb, line 244
    def phrase(str)
      str = normalize_encoding(str)
      if CONTROL_CHAR =~ str
        scanadd str
      else
        add_text quote_phrase(str)
      end
    end

[Source]

# File lib/tmail/encode.rb, line 199
    def puts(str = nil)
      @f << str if str
      @f << @eol
    end

[Source]

# File lib/tmail/encode.rb, line 184
    def reset
      @text = ''
      @lwsp = ''
      @curlen = 0
    end

[Source]

# File lib/tmail/encode.rb, line 226
    def space
      add_lwsp ' '
    end
spc()

Alias for space

[Source]

# File lib/tmail/encode.rb, line 190
    def terminate
      add_lwsp ''
      reset
    end

[Source]

# File lib/tmail/encode.rb, line 240
    def text(str)
      scanadd normalize_encoding(str)
    end

[Source]

# File lib/tmail/encode.rb, line 204
    def write(str)
      @f << str
    end

[Validate]