Class: Uchardet::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/uchardet/cli.rb

Class Method Summary collapse

Class Method Details

.detectObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/uchardet/cli.rb', line 61

def self.detect
  detector = ICU::UCharsetDetector.new
  detector.input_filtered = @options[:input_filtered]
  detector.declared_encoding = @options[:declared_encoding]

  source = IO.read(@options[:path])
  matches = if @options[:detect_all]
    detector.detect_all(source)
  else
    [detector.detect(source)]
  end
  
  matches.each do |match|
    @stdout.puts "#{match[:encoding]} (confidence #{match[:confidence]}%)"
  end
rescue Exception => ex
  STDERR.puts "ERROR: #{ex.to_s}"
end

.execute(stdout, args = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uchardet/cli.rb', line 5

def self.execute(stdout, args=[])
  @stdout = stdout
  @options = {
    :input_filtered => false,
    :declared_encoding => nil,
    :detect_all => false,
    :path => nil
  }
  
  parser = OptionParser.new do |opts|
    opts.banner = "      Usage: \#{File.basename($0)} [options] file\n    BANNER\n    \n    opts.on(\"-l\", \"--list\",\n            \"Display list of detectable character sets.\"\n            ) { self.list; exit }\n    opts.on(\"-s\", \"--strip\",\n            \"Strip HTML or XML markup before detection.\"\n            ) { @options[:input_filtered] = true }\n    opts.on(\"-e\", \"--encoding\",\n            \"Hint the charset detector about possible encoding.\"\n            ) { |arg| @options[:declared_encoding] = arg }\n    opts.on(\"-a\", \"--all\",\n            \"Show all matching encodings.\"\n            ) { @options[:detect_all] = true }\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\"\n            ) { @stdout.puts opts; exit }\n            \n    if args.empty?\n      @stdout.puts opts\n    else\n      begin\n        opts.parse!(args)\n      rescue OptionParser::ParseError => ex\n        STDERR.puts \"ERROR: \#{ex.to_s}. See \#{File.basename($0)} --help\"\n        exit\n      end\n    \n      @options[:path] = args.last\n      if @options[:path].nil? || @options[:path].empty?\n        @stdout.puts opts\n        STDERR.puts \"ERROR: please specify a file path.\"\n        exit\n      end\n      \n      self.detect\n    end\n  end\nend\n".gsub(/^\s*/,'')

.listObject



57
58
59
# File 'lib/uchardet/cli.rb', line 57

def self.list
  ICU::UCharsetDetector.detectable_charsets.uniq.sort.each { |name| @stdout.puts name }
end