summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-29 09:26:34 (GMT)
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-29 09:26:34 (GMT)
commitaf91b4235416af1ec499afafcf4524d93437607b (patch)
tree66010749bd70f0fd5d3048a7c65f5eb7babc88c8 /bin
parentd33298e52459045ddde4bae619a8da3c3cf7dbd6 (diff)
* lib/erb.rb (ERB): m17n of ERB. adds rdoc.
fixes #712. c.f. [ruby-dev:37516]. * lib/erb.rb (ERB::Compiler#compile): recognizes magic comments. returns a pair of compiled script and its script encoding. * lib/erb.rb (ERB#set_eoutvar): make generated scripts return a string in correct encoding. * lib/erb.rb (ERB#def_method): use Kernel#eval for encoding-awareness of the evaluated string. * bin/erb.rb (ERB::Main.run): adds -E and -U options. String is no longer Enumerable. * man/erb.1: new manapage. * test/erb/test_erb_m17n.rb: new test case for m17n features. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bin')
-rwxr-xr-xbin/erb27
1 files changed, 23 insertions, 4 deletions
diff --git a/bin/erb b/bin/erb
index 8541437..d265641 100755
--- a/bin/erb
+++ b/bin/erb
@@ -72,6 +72,11 @@ class ERB
end
raise "invalid trim mode #{arg.dump}" unless arg =~ /^[0-2]$/
trim_mode = arg.to_i
+ when '-E', '--encoding'
+ arg = ARGV.req_arg
+ set_encoding(*arg.split(/:/, 2))
+ when '-U'
+ set_encoding(Encoding::UTF_8, Encoding::UTF_8)
when '-P'
disable_percent = true
when '--help'
@@ -91,12 +96,15 @@ class ERB
-d set $DEBUG to true
-r [library] load a library
-S [safe_level] set $SAFE (0..4)
+ -E ex[:in] set default external/internal encodings
+ -U set default encoding to UTF-8.
-T [trim_mode] specify trim_mode (0..2, -)
-P ignore lines which start with "%"
EOU
exit 1
end
+ $<.set_encoding(Encoding::ASCII_8BIT, nil)
src = $<.read
filename = $FILENAME
exit 2 unless src
@@ -105,10 +113,8 @@ EOU
erb.filename = filename
if output
if number
- l = 1
- for line in erb.src
- puts "%3d %s"%[l, line]
- l += 1
+ erb.src.each_line.with_index do |line, l|
+ puts "%3d %s"%[l+1, line]
end
else
puts erb.src
@@ -118,6 +124,19 @@ EOU
end
end
module_function :run
+
+ def set_encoding(extern, intern = nil)
+ verbose, $VERBOSE = $VERBOSE, nil
+ Encoding.default_external = extern unless extern.nil? || extern.empty?
+ Encoding.default_internal = intern unless intern.nil? || intern.empty?
+ [$stdin, $stdout, $stderr].each do |io|
+ io.set_encoding(extern, intern)
+ end
+ ensure
+ $VERBOSE = verbose
+ end
+ module_function :set_encoding
+ class << self; private :set_encoding; end
end
end