class HTree::Encoder

Public Class Methods

internal_charset() click to toggle source

::internal_charset returns the MIME charset corresponding to $KCODE.

  • 'ISO-8859-1' when $KCODE=='NONE'

  • 'UTF-8' when $KCODE=='UTF8'

  • 'EUC-JP' when $KCODE=='EUC'

  • 'Shift_JIS' when $KCODE=='SJIS'

This mapping ignores EUC-KR and various single byte charset other than ISO-8859-1 at least. This should be fixed when Ruby is m17nized.

# File htree/encoder.rb, line 36
def Encoder.internal_charset
  if Object.const_defined? :Encoding
    Encoding.default_external.name
  else
    KcodeCharset[$KCODE]
  end
end
new(output_encoding, internal_encoding=HTree::Encoder.internal_charset) click to toggle source
# File htree/encoder.rb, line 44
def initialize(output_encoding, internal_encoding=HTree::Encoder.internal_charset)
  @buf = ''
  @internal_encoding = internal_encoding
  @output_encoding = output_encoding
  if defined? Encoding::Converter
    if @internal_encoding == output_encoding
      @ic = DummyEncodingConverter.new(@internal_encoding)
    else
      @ic = Encoding::Converter.new(@internal_encoding, output_encoding)
    end
  else
    @ic = Iconv.new(output_encoding, @internal_encoding)
  end
  @charpat = FirstCharPattern[internal_encoding]
  @subcharset_list = SubCharset[output_encoding] || []
  @subcharset_ic = {}
  @subcharset_list.each {|subcharset|
    if defined? Encoding::Converter
      if @internal_encoding == subcharset
        @subcharset_ic[subcharset] = DummyEncodingConverter.new(@internal_encoding)
      else
        @subcharset_ic[subcharset] = Encoding::Converter.new(@internal_encoding, subcharset)
      end
    else
      @subcharset_ic[subcharset] = Iconv.new(subcharset, @internal_encoding)
    end
  }
  @html_output = false
end

Public Instance Methods

finish() click to toggle source
# File htree/encoder.rb, line 251
def finish
  if @ic.respond_to? :finish
    external_str = @ic.finish
  else
    external_str = @ic.close
  end
  @buf << external_str
  @subcharset_ic.reject! {|subcharset, ic|
    if ic.respond_to? :finish
      begin
        ic.finish != external_str
      rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
        true
      end
    else
      begin
        ic.close != external_str
      rescue Iconv::Failure
        true
      end
    end
  }
  @buf
end
finish_with_xmldecl() click to toggle source
# File htree/encoder.rb, line 276
def finish_with_xmldecl
  content = finish
  str = "<?xml version=\"1.0\" encoding=\"#{minimal_charset}\"?>"
  if str.respond_to? :encode
    xmldecl = str.encode(@output_encoding, 'US-ASCII')
  else
    xmldecl = Iconv.conv(@output_encoding, 'US-ASCII', str)
  end
  xmldecl + content
end
minimal_charset() click to toggle source
# File htree/encoder.rb, line 287
def minimal_charset
  @subcharset_list.each {|subcharset|
    if @subcharset_ic.include? subcharset
      return subcharset
    end
  }
  @output_encoding
end