Class: Color::RGB::JP::Compiler::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/color/rgb/jp/compiler/command.rb

Class Method Summary collapse

Class Method Details

.check_not_blank(v, msg) ⇒ Object



58
59
60
# File 'lib/color/rgb/jp/compiler/command.rb', line 58

def self.check_not_blank(v, msg)
  error_exit(msg, 1) if v.nil? or v == ""
end

.dispatchObject



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
56
# File 'lib/color/rgb/jp/compiler/command.rb', line 8

def self.dispatch
  output = $stdout
  encoding = Color::RGB::JP::Base::Encode::INTERNAL_ENCODING
  pallet = "jis"
  prefix = "##"
  do_compile = true
  generator = "#{$0} #{ARGV * " "}"

  ARGV.options do |opt|
    opt.program_name = "color-japanese"
    opt.version = Color::RGB::JP::VERSION::STRING
    opt.banner = "Usage: jcolorc [options] <source file>"
    opt.separator ""
    opt.separator "  Options:"
    opt.on("-o <output file>", "--output",
           "output file (default: stdout)") {|v| output = v}
    opt.on("-p <color pallet>", "--pallet",
           "color pallet (jis or traditional).",
           "(default: #{pallet})") {|v| pallet = v }
    opt.on("-K<kcode>",
           "specifies KANJI code-set (default: #{encoding})") {|v| encoding = parse_kcode(v) }
    opt.on("-E",
           "output preprocessed ERB script") { do_compile = false }
    opt.on("--prefix=<directive prefix>",
           "prefix the pre-processor directive.",
           "(default: #{prefix})") {|v| prefix = v}
    opt.separator ""
    opt.on_tail("-v", "--version", "print the version"){
      puts opt.ver
      exit 0
    }
    opt.on_tail("-h", "--help", "print this message"){
      puts opt
      exit 0
    }
    opt.parse!
  end

  pallet = parse_pallet(pallet)
  input = parse_input(ARGV)

  check_not_blank(prefix, "Error: no prefix.")
  check_not_blank(output, "Error: no output file.")

  $KCODE = encoding

  jcolorc = Color::RGB::JP::Compiler::Compiler.new(pallet, encoding, prefix, generator)
  jcolorc.execute(input, output, do_compile)
end

.error_exit(msg, status) ⇒ Object



97
98
99
100
101
# File 'lib/color/rgb/jp/compiler/command.rb', line 97

def self.error_exit(msg, status)
  $stderr.puts msg if msg
  $stderr.puts ARGV.options
  exit status
end

.parse_input(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/color/rgb/jp/compiler/command.rb', line 62

def self.parse_input(args)
  case args.length
  when 0
    $stdin
  when 1
    ARGV.shift
  else
    error_exit("Error: too many source file.", 1)
  end
end

.parse_kcode(kcode) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/color/rgb/jp/compiler/command.rb', line 73

def self.parse_kcode(kcode)
  case kcode.downcase
  when "e", "euc", /\Aeuc[_-]?jp/
    "EUC-JP"
  when "s", "sjis", /\Ashift[_-]?jis\z/
    "Shift_JIS"
  when "u", /\Autf[_-]?8\z/
    "UTF-8"
  else
    error_exit("invalid KCODE #{kcode.dump}", 1)
  end
end

.parse_pallet(pallet_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/color/rgb/jp/compiler/command.rb', line 86

def self.parse_pallet(pallet_name)
  case pallet_name
  when /\Ajis/i
    Color::RGB::JP::JISZ8102
  when /\Atrad/i
    Color::RGB::JP::Traditional
  else
    error_exit("invalid pallet name `#{pallet_name}': jis or traditional", 1)
  end
end