lib/review/preprocessor.rb in review-1.7.2 vs lib/review/preprocessor.rb in review-2.0.0.beta1

- old
+ new

@@ -22,14 +22,28 @@ @errutils_file = f @errutils_err = false end def warn(msg) + if @config["outencoding"] =~ /^EUC$/ + msg = NKF.nkf("-W -e", msg) + elsif @config["outencoding"] =~ /^SJIS$/ + msg = NKF.nkf("-W -s", msg) + elsif @config["outencoding"] =~ /^JIS$/ + msg = NKF.nkf("-W -j", msg) + end $stderr.puts "#{location()}: warning: #{msg}" end def error(msg) + if @config["outencoding"] =~ /^EUC$/ + msg = NKF.nkf("-W -e", msg) + elsif @config["outencoding"] =~ /^SJIS$/ + msg = NKF.nkf("-W -s", msg) + elsif @config["outencoding"] =~ /^JIS$/ + msg = NKF.nkf("-W -j", msg) + end @errutils_err = true raise ApplicationError, "#{location()}: #{msg}" end def location @@ -132,30 +146,30 @@ when /\A\#@mapfile/ direc = parse_directive(line, 1, 'eval') path = expand(direc.arg) ent = @repository.fetch_file(path) ent = evaluate(path, ent) if direc['eval'] - replace_block(f, line, ent, false) # FIXME: turn off lineno: tmp + replace_block f, line, ent, false # FIXME: turn off lineno: tmp when /\A\#@map(?:range)?/ direc = parse_directive(line, 2, 'unindent') path = expand(direc.args[0]) ent = @repository.fetch_range(path, direc.args[1]) or error "unknown range: #{path}: #{direc.args[1]}" ent = (direc['unindent'] ? unindent(ent, direc['unindent']) : ent) - replace_block(f, line, ent, false) # FIXME: turn off lineno: tmp + replace_block f, line, ent, false # FIXME: turn off lineno: tmp when /\A\#@end/ error 'unbaranced #@end' when /\A\#@/ op = line.slice(/@(\w+)/, 1) #error "unkown directive: #{line.strip}" unless known_directive?(op) warn "unkown directive: #{line.strip}" unless known_directive?(op) @f.print line - when /\A\s*\z/ # empty line + when /\A\s*\z/ # empty line @f.puts else @f.print line end end @@ -167,15 +181,38 @@ def known_directive?(op) KNOWN_DIRECTIVES.index(op) end + def convert_outencoding(*s) + ine = "" + if @config["inencoding"] =~ /^EUC$/i + ine = "-E," + elsif @config["inencoding"] =~ /^SJIS$/i + ine = "-S," + elsif @config["inencoding"] =~ /^JIS$/i + ine = "-J," + elsif @config["inencoding"] =~ /^UTF\-8$/i + ine = "-W," + end + + if @config["outencoding"] =~ /^EUC$/i + NKF.nkf("#{ine} -m0x -e", *s) + elsif @config["outencoding"] =~ /^SJIS$/i + NKF.nkf("#{ine} -m0x -s", *s) + elsif @config["outencoding"] =~ /^JIS$/i + NKF.nkf("#{ine} -m0x -j", *s) + else + NKF.nkf("#{ine} -m0x -w", *s) + end + end + def replace_block(f, directive_line, newlines, with_lineno) @f.print directive_line newlines.each do |line| print_number line.number if with_lineno - @f.print line.string + @f.print convert_outencoding(line.string) end skip_list f end def print_number(num) @@ -204,12 +241,12 @@ end class Directive def initialize(op, args, opts) @op = op - @args = args - @opts = opts + @args = args + @opts = opts end attr_reader :op attr_reader :args attr_reader :opts @@ -258,16 +295,16 @@ table end def optarg_value(spec) case spec - when 'true' then true # [name=true] - when 'false' then false # [name=false] - when 'nil' then nil # [name=nil] - when nil then true # [name] - when /^\d+$/ then $&.to_i # [name=8] - else # [name=val] + when 'true' then true # [name=true] + when 'false' then false # [name=false] + when 'nil' then nil # [name=nil] + when nil then true # [name] + when /^\d+$/ then $&.to_i # [name=8] + else # [name=val] spec end end def init_vars @@ -426,11 +463,11 @@ def _parse_file(f) whole = [] repo = {'file' => whole} curr = {'WHOLE' => whole} lineno = 1 - yacchack = false # remove ';'-only lines. + yacchack = false # remove ';'-only lines. opened = [['(not opened)', '(not opened)']] * 3 f.each do |line| case line when /(?:\A\#@|\#@@)([a-z]+)_(begin|end)\((.*)\)/ @@ -471,10 +508,10 @@ opened.pop when /(?:\A\#@|\#@@)yacchack/ yacchack = true - when /\A\#@-/ # does not increment line number. + when /\A\#@-/ # does not increment line number. line = canonical($') curr.each_value do |list| list.push Line.new(nil, line) end