Method: File.encoding

Defined in:
lib/picolena/templates/lib/core_exts.rb

.encoding(source) ⇒ Object

Returns a probable encoding for a given plain text file If source is a html file, it parses for metadata to retrieve encoding, and uses file -i otherwise. Returns iso-8859-15 instead of iso-8859-1, to be sure € char can be encoded



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/picolena/templates/lib/core_exts.rb', line 80

def self.encoding(source)
  parse_for_charset="grep -io charset=[a-z0-9\\-]* | sed 's/charset=//i'"
  if File.extname(source)[0,4]==".htm" then
    enc=%x{head -n20 \"#{source}\" | #{parse_for_charset}}.chomp
  else
    enc=%x{file -i \"#{source}\"  | #{parse_for_charset}}.chomp
  end
  #iso-8859-15 should be used instead of iso-8859-1, for € char
  case enc
   when "iso-8859-1"
     "iso-8859-15"
   when "unknown"
     ""
   else
     enc
   end
end