color-japanese 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ module FileUtils
2
+ SHELL_COMMAND_FILTER = []
3
+
4
+ alias sh_original sh
5
+ def sh(*cmd, &block)
6
+ cmd = SHELL_COMMAND_FILTER.inject(cmd){|c,filter| filter.call(c) or c }
7
+ sh_original(*cmd, &block)
8
+ end
9
+
10
+ def add_sh_filter(&block)
11
+ SHELL_COMMAND_FILTER << block
12
+ end
13
+
14
+ def add_sh_filter_for(pattern, &block)
15
+ add_sh_filter {|cmd|
16
+ if cmd[0] =~ pattern
17
+ block.call(cmd)
18
+ else
19
+ cmd
20
+ end
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ def windows?
2
+ RUBY_PLATFORM =~ /win/
3
+ end
4
+
5
+ def with_temporary_rename(from, to)
6
+ mv from, to
7
+ yield
8
+ ensure
9
+ mv to, from
10
+ end
11
+
12
+ def has_dot?
13
+ if windows?
14
+ v = `dot -V 2>&1` rescue nil
15
+ v and v =~ /Graphviz/
16
+ else
17
+ `which dot` =~ /\/dot/
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require "color/rgb/jp/jisz8102"
2
+ require "color/rgb/jp/traditional"
3
+ require "color/rgb/jp/version"
4
+
5
+ module Color::RGB::JP
6
+ def self.pallets
7
+ [
8
+ Color::RGB::JP::Traditional,
9
+ Color::RGB::JP::JISZ8102
10
+ ]
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require "color"
2
+
3
+ module Color::RGB::JP
4
+ module Base; end
5
+ end
6
+
7
+ require "color/rgb/jp/base/encode"
8
+ require "color/rgb/jp/base/color_name"
9
+ require "color/rgb/jp/base/named_rgb"
10
+ require "color/rgb/jp/base/pallet"
@@ -0,0 +1,39 @@
1
+ require "color/rgb/jp/base/encode"
2
+
3
+ module Color::RGB::JP::Base
4
+
5
+ class ColorName
6
+ include Encode
7
+
8
+ def self.encoded_attr_reader(internal_encoding, *attributes)
9
+ attributes.each do |attr|
10
+ class_eval %Q{
11
+ def #{attr}(encoding = "#{internal_encoding}")
12
+ encode_to(encoding, "#{internal_encoding}", @#{attr})
13
+ end
14
+ }
15
+ end
16
+ end
17
+
18
+ encoded_attr_reader Encode::INTERNAL_ENCODING, :kanji, :hiragana, :katakana, :romaji
19
+ attr_reader :const_name
20
+
21
+ def initialize(const_name, kanji, hiragana, katakana = nil, romaji = nil)
22
+ @const_name = const_name
23
+ @kanji = kanji
24
+ @hiragana = hiragana
25
+ @katakana = katakana
26
+ @romaji = romaji
27
+ end
28
+
29
+ def to_s
30
+ romaji
31
+ end
32
+
33
+ def names(encoding = Encode::INTERNAL_ENCODING)
34
+ %W(kanji hiragana katakana romaji).map{|e| self.__send__(e, encoding) }
35
+ end
36
+ alias to_a names
37
+ end
38
+
39
+ end
@@ -0,0 +1,23 @@
1
+ require "iconv"
2
+
3
+ module Color::RGB::JP::Base
4
+
5
+ module Encode
6
+ INTERNAL_ENCODING = "UTF-8"
7
+
8
+ module_function
9
+
10
+ def encode_to(to, from, value)
11
+ return nil if value.nil?
12
+ return value if to.nil?
13
+ return value if from.nil?
14
+ return value if from == to
15
+ return Iconv.iconv(to, from, value)[0]
16
+ end
17
+
18
+ def with_internal_encoding(value, from)
19
+ yield encode_to(INTERNAL_ENCODING, from, value)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,15 @@
1
+ module Color::RGB::JP::Base
2
+
3
+ class NamedRGB
4
+ attr_reader :name, :rgb
5
+ def initialize(name, rgb)
6
+ @name = name
7
+ @rgb = rgb
8
+ end
9
+
10
+ def to_s
11
+ "%s (%s)" % [name.romaji, rgb.html]
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,49 @@
1
+ require "color"
2
+
3
+ require "color/rgb/jp/base/encode"
4
+ require "color/rgb/jp/base/named_rgb"
5
+ require "color/rgb/jp/base/color_name"
6
+
7
+ module Color::RGB::JP::Base
8
+
9
+ module Pallet
10
+ include Encode
11
+ include Enumerable
12
+
13
+ def colors
14
+ @colors
15
+ end
16
+
17
+ def each(&block)
18
+ colors.each(&block)
19
+ end
20
+
21
+ def [](name, encoding = nil)
22
+ with_internal_encoding(name, encoding) {|n|
23
+ @index[normalize(n)]
24
+ }
25
+ end
26
+
27
+ def define_color(const, hex, *names)
28
+ @colors ||= []
29
+ @index ||= {}
30
+
31
+ name = ColorName.new(const, *names)
32
+ rgb = ::Color::RGB.from_html(hex)
33
+ color = NamedRGB.new(name, rgb)
34
+ @colors << color
35
+
36
+ [const, *names].compact.each {|e|
37
+ @index[normalize(e)] ||= color
38
+ }
39
+ color
40
+ end
41
+ alias c define_color
42
+
43
+ def normalize(v)
44
+ v.gsub(/-/, "").downcase
45
+ end
46
+ private :normalize
47
+ end
48
+
49
+ end
@@ -0,0 +1,102 @@
1
+ require "optparse"
2
+
3
+ require "color/rgb/jp"
4
+ require "color/rgb/jp/compiler/compiler"
5
+
6
+ class Color::RGB::JP::Compiler::Command
7
+
8
+ def self.dispatch
9
+ output = $stdout
10
+ encoding = Color::RGB::JP::Base::Encode::INTERNAL_ENCODING
11
+ pallet = "jis"
12
+ prefix = "##"
13
+ do_compile = true
14
+ generator = "#{$0} #{ARGV * " "}"
15
+
16
+ ARGV.options do |opt|
17
+ opt.program_name = "color-japanese"
18
+ opt.version = Color::RGB::JP::VERSION::STRING
19
+ opt.banner = "Usage: jcolorc [options] <source file>"
20
+ opt.separator ""
21
+ opt.separator " Options:"
22
+ opt.on("-o <output file>", "--output",
23
+ "output file (default: stdout)") {|v| output = v}
24
+ opt.on("-p <color pallet>", "--pallet",
25
+ "color pallet (jis or traditional).",
26
+ "(default: #{pallet})") {|v| pallet = v }
27
+ opt.on("-K<kcode>",
28
+ "specifies KANJI code-set (default: #{encoding})") {|v| encoding = parse_kcode(v) }
29
+ opt.on("-E",
30
+ "output preprocessed ERB script") { do_compile = false }
31
+ opt.on("--prefix=<directive prefix>",
32
+ "prefix the pre-processor directive.",
33
+ "(default: #{prefix})") {|v| prefix = v}
34
+ opt.separator ""
35
+ opt.on_tail("-v", "--version", "print the version"){
36
+ puts opt.ver
37
+ exit 0
38
+ }
39
+ opt.on_tail("-h", "--help", "print this message"){
40
+ puts opt
41
+ exit 0
42
+ }
43
+ opt.parse!
44
+ end
45
+
46
+ pallet = parse_pallet(pallet)
47
+ input = parse_input(ARGV)
48
+
49
+ check_not_blank(prefix, "Error: no prefix.")
50
+ check_not_blank(output, "Error: no output file.")
51
+
52
+ $KCODE = encoding
53
+
54
+ jcolorc = Color::RGB::JP::Compiler::Compiler.new(pallet, encoding, prefix, generator)
55
+ jcolorc.execute(input, output, do_compile)
56
+ end
57
+
58
+ def self.check_not_blank(v, msg)
59
+ error_exit(msg, 1) if v.nil? or v == ""
60
+ end
61
+
62
+ def self.parse_input(args)
63
+ case args.length
64
+ when 0
65
+ $stdin
66
+ when 1
67
+ ARGV.shift
68
+ else
69
+ error_exit("Error: too many source file.", 1)
70
+ end
71
+ end
72
+
73
+ def self.parse_kcode(kcode)
74
+ case kcode.downcase
75
+ when "e", "euc", /\Aeuc[_-]?jp/
76
+ "EUC-JP"
77
+ when "s", "sjis", /\Ashift[_-]?jis\z/
78
+ "Shift_JIS"
79
+ when "u", /\Autf[_-]?8\z/
80
+ "UTF-8"
81
+ else
82
+ error_exit("invalid KCODE #{kcode.dump}", 1)
83
+ end
84
+ end
85
+
86
+ def self.parse_pallet(pallet_name)
87
+ case pallet_name
88
+ when /\Ajis/i
89
+ Color::RGB::JP::JISZ8102
90
+ when /\Atrad/i
91
+ Color::RGB::JP::Traditional
92
+ else
93
+ error_exit("invalid pallet name `#{pallet_name}': jis or traditional", 1)
94
+ end
95
+ end
96
+
97
+ def self.error_exit(msg, status)
98
+ $stderr.puts msg if msg
99
+ $stderr.puts ARGV.options
100
+ exit status
101
+ end
102
+ end
@@ -0,0 +1,203 @@
1
+ require "erb"
2
+
3
+ require "rubygems"
4
+ require "color/rgb/jp"
5
+
6
+
7
+ module Color::RGB::JP
8
+ module Compiler; end
9
+ end
10
+
11
+ class Color::RGB::JP::Compiler::Compiler
12
+
13
+ COLOR_NAME_CHAR = /[^[:space:][:punct:][:cntrl:]]/
14
+ COLOR_NAME_RE = /#{COLOR_NAME_CHAR}+(?:-#{COLOR_NAME_CHAR}+)*/
15
+
16
+ DEFAULT_PREPROCESSOR = [:preprocess_alias, :preprocess_color]
17
+ DEFAULT_TRIM_MODE = ">%"
18
+
19
+ attr_accessor :pallet, :encoding, :prefix, :generator, :preprocessor, :trim_mode
20
+
21
+ def initialize(pallet, encoding, prefix, generator)
22
+ @pallet = pallet
23
+ @encoding = encoding
24
+ @prefix = prefix
25
+ @generator = generator
26
+ @preprocessor = DEFAULT_PREPROCESSOR
27
+ @trim_mode = DEFAULT_TRIM_MODE
28
+ end
29
+
30
+ def execute(input, output, do_compile)
31
+ with_input_stream(input) do |is|
32
+ erbscript = preprocess(is.read, input)
33
+ with_output_stream(output) do |os|
34
+ if do_compile
35
+ os.write compile(erbscript, input)
36
+ else
37
+ os.write erbscript
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def preprocess(src, input)
44
+ preprocess_snippet(prefix) + do_preprocess(src, @prefix)
45
+ end
46
+
47
+ def compile(src, input)
48
+ e = ERB.new(src, 0, @trim_mode)
49
+ e.filename = input if String === input
50
+ e.result
51
+ end
52
+
53
+ private
54
+
55
+ def do_preprocess(src, prefix)
56
+ @preprocessor.inject(src){|r,fn|
57
+ if Proc === fn
58
+ fn.call(self, r, prefix) || r
59
+ else
60
+ self.__send__(fn, r, prefix) || r
61
+ end
62
+ }
63
+ end
64
+
65
+ def preprocess_alias(src, prefix)
66
+ scan_erb(src, /#{prefix}\s*alias[ \t]*((?:#{COLOR_NAME_RE}| )*)/, "<%"){|m|
67
+ n, v = m.strip.split(/\s+/, 2)
68
+ "Color::RGB::JP::ERBUtil.alias(%q{#{n}}, %Q{#{v}})"
69
+ }
70
+ end
71
+
72
+ def preprocess_color(src, prefix)
73
+ scan_erb(src, /#{prefix}(#{COLOR_NAME_RE})/, "<%="){|m|
74
+ "Color::RGB::JP::ERBUtil.to_rgb(%q{#{m}})"
75
+ }
76
+ end
77
+
78
+ def scan_erb(src, re, embed_mode, &block)
79
+ context = :text
80
+ src.gsub(/(<%)|(%>)|(^%)|(\n|$)|#{re}/) {
81
+ tokens = [:stag, :etag, :percent, :lf, :value].zip(Regexp.last_match.captures)
82
+ context, r = process_token(context, embed_mode, *tokens.find{|e| e[1]}, &block)
83
+ r
84
+ }
85
+ end
86
+
87
+ def process_token(context, embed_mode, token, value)
88
+ case context
89
+ when :erb1
90
+ case token
91
+ when :value; [context, embed_erb(yield(value))]
92
+ when :lf; [:text, value]
93
+ else [context, value]
94
+ end
95
+ when :erbtag
96
+ case token
97
+ when :value; [context, embed_erb(yield(value))]
98
+ when :etag; [:text, value]
99
+ else [context, value]
100
+ end
101
+ when :text
102
+ case token
103
+ when :value; [context, embed_erb(yield(value), embed_mode)]
104
+ when :stag; [:erbtag, value]
105
+ when :percent; [:erb1, value]
106
+ else [context, value]
107
+ end
108
+ else
109
+ raise "[bug] unknown context `#{context}': token=#{token}, value=#{value}"
110
+ end
111
+ end
112
+
113
+ def embed_erb(str, tag = nil)
114
+ if tag.nil?
115
+ str
116
+ else
117
+ "#{tag} #{str} %>"
118
+ end
119
+ end
120
+
121
+ def preprocess_snippet(prefix)
122
+ return <<-SNIPPET.gsub(/^ /, "")
123
+ <%
124
+ #
125
+ # Auto generated by "#{@generator}".
126
+ # Date: #{Time.now.iso8601}.
127
+ #
128
+
129
+ require "color/rgb/jp"
130
+
131
+ $KCODE = "#{@encoding}"
132
+
133
+ class Color::RGB
134
+ undef to_s
135
+ alias to_s html
136
+ end
137
+
138
+ module Color::RGB::JP::ERBUtil
139
+ _pallet = ::#{@pallet.name}
140
+ _alias = {}
141
+
142
+ define_method(:pallet) do
143
+ _pallet
144
+ end
145
+ module_function :pallet
146
+
147
+ define_method(:alias) do |name, value|
148
+ _alias[name] = value
149
+ end
150
+ module_function :alias
151
+
152
+ define_method(:alias?) do |name|
153
+ _alias.key?(name)
154
+ end
155
+ module_function :alias?
156
+
157
+ define_method(:to_rgb) do |name|
158
+ c = lookup(name)
159
+ unless c
160
+ msg = "undefined color name: `\#{name}'"
161
+ msg << " (alias of '\#{resolve_alias(name)}')" if alias?(name)
162
+ warn msg
163
+ return "#{prefix}\#{name}"
164
+ end
165
+ c.rgb
166
+ end
167
+ module_function :to_rgb
168
+
169
+ define_method(:resolve_alias) do |name|
170
+ if _alias.key?(name)
171
+ resolve_alias(_alias[name])
172
+ else
173
+ name
174
+ end
175
+ end
176
+ module_function :resolve_alias
177
+
178
+ define_method(:lookup) do |name|
179
+ _pallet[resolve_alias(name), "#{@encoding}"]
180
+ end
181
+ module_function :lookup
182
+ end
183
+ %>
184
+ SNIPPET
185
+ end
186
+
187
+ def with_input_stream(stream)
188
+ if stream.respond_to?(:read)
189
+ yield stream
190
+ else
191
+ open(stream) {|s| yield s }
192
+ end
193
+ end
194
+
195
+ def with_output_stream(stream)
196
+ if stream.respond_to?(:write)
197
+ yield stream
198
+ else
199
+ open(stream, "w") {|s| yield s }
200
+ end
201
+ end
202
+
203
+ end