erb 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ #!/usr/bin/env ruby
2
+ # Tiny eRuby --- ERB2
3
+ # Copyright (c) 1999-2000,2002 Masatoshi SEKI
4
+ # You can redistribute it and/or modify it under the same terms as Ruby.
5
+
6
+ require 'erb'
7
+
8
+ class ERB
9
+ module Main
10
+ def ARGV.switch
11
+ return nil if self.empty?
12
+ arg = self.shift
13
+ return nil if arg == '--'
14
+ case arg
15
+ when /\A-(.)(.*)/
16
+ if $1 == '-'
17
+ arg, @maybe_arg = arg.split(/=/, 2)
18
+ return arg
19
+ end
20
+ raise 'unknown switch "-"' if $2[0] == ?- and $1 != 'T'
21
+ if $2.size > 0
22
+ self.unshift "-#{$2}"
23
+ @maybe_arg = $2
24
+ else
25
+ @maybe_arg = nil
26
+ end
27
+ "-#{$1}"
28
+ when /\A(\w+)=/
29
+ arg
30
+ else
31
+ self.unshift arg
32
+ nil
33
+ end
34
+ end
35
+
36
+ def ARGV.req_arg
37
+ (@maybe_arg || self.shift || raise('missing argument')).tap {
38
+ @maybe_arg = nil
39
+ }
40
+ end
41
+
42
+ def trim_mode_opt(trim_mode, disable_percent)
43
+ return trim_mode if disable_percent
44
+ case trim_mode
45
+ when 0
46
+ return '%'
47
+ when 1
48
+ return '%>'
49
+ when 2
50
+ return '%<>'
51
+ when '-'
52
+ return '%-'
53
+ end
54
+ end
55
+ module_function :trim_mode_opt
56
+
57
+ def run(factory=ERB)
58
+ trim_mode = 0
59
+ disable_percent = false
60
+ variables = {}
61
+ begin
62
+ while switch = ARGV.switch
63
+ case switch
64
+ when '-x' # ruby source
65
+ output = true
66
+ when '-n' # line number
67
+ number = true
68
+ when '-v' # verbose
69
+ $VERBOSE = true
70
+ when '--version' # version
71
+ STDERR.puts factory.version
72
+ exit
73
+ when '-d', '--debug' # debug
74
+ $DEBUG = true
75
+ when '-r' # require
76
+ require ARGV.req_arg
77
+ when '-S' # security level
78
+ warn 'warning: -S option of erb command is deprecated. Please do not use this.'
79
+ arg = ARGV.req_arg
80
+ raise "invalid safe_level #{arg.dump}" unless arg =~ /\A[0-1]\z/
81
+ safe_level = arg.to_i
82
+ when '-T' # trim mode
83
+ arg = ARGV.req_arg
84
+ if arg == '-'
85
+ trim_mode = arg
86
+ next
87
+ end
88
+ raise "invalid trim mode #{arg.dump}" unless arg =~ /\A[0-2]\z/
89
+ trim_mode = arg.to_i
90
+ when '-E', '--encoding'
91
+ arg = ARGV.req_arg
92
+ set_encoding(*arg.split(/:/, 2))
93
+ when '-U'
94
+ set_encoding(Encoding::UTF_8, Encoding::UTF_8)
95
+ when '-P'
96
+ disable_percent = true
97
+ when '--help'
98
+ raise "print this help"
99
+ when /\A-/
100
+ raise "unknown switch #{switch.dump}"
101
+ else
102
+ var, val = *switch.split('=', 2)
103
+ (variables ||= {})[var] = val
104
+ end
105
+ end
106
+ rescue # usage
107
+ STDERR.puts $!.to_s
108
+ STDERR.puts File.basename($0) +
109
+ " [switches] [var=value...] [inputfile]"
110
+ STDERR.puts <<EOU
111
+ -x print ruby script
112
+ -n print ruby script with line number
113
+ -v enable verbose mode
114
+ -d set $DEBUG to true
115
+ -r library load a library
116
+ -E ex[:in] set default external/internal encodings
117
+ -U set default encoding to UTF-8
118
+ -T trim_mode specify trim_mode (0..2, -)
119
+ -P disable ruby code evaluation for lines beginning with %
120
+ var=value set variable
121
+ EOU
122
+ exit 1
123
+ end
124
+
125
+ $<.set_encoding(Encoding::UTF_8, nil)
126
+ src = $<.read
127
+ filename = $FILENAME
128
+ exit 2 unless src
129
+ trim = trim_mode_opt(trim_mode, disable_percent)
130
+ if safe_level.nil?
131
+ erb = factory.new(src, trim_mode: trim)
132
+ else
133
+ # [deprecated] This will be removed at Ruby 2.7.
134
+ erb = factory.new(src, safe_level, trim_mode: trim)
135
+ end
136
+ erb.filename = filename
137
+ if output
138
+ if number
139
+ erb.src.each_line.with_index do |line, l|
140
+ puts "%3d %s"%[l+1, line]
141
+ end
142
+ else
143
+ puts erb.src
144
+ end
145
+ else
146
+ bind = TOPLEVEL_BINDING
147
+ if variables
148
+ enc = erb.encoding
149
+ for var, val in variables do
150
+ val = val.encode(enc) if val
151
+ bind.local_variable_set(var, val)
152
+ end
153
+ end
154
+ erb.run(bind)
155
+ end
156
+ end
157
+ module_function :run
158
+
159
+ def set_encoding(extern, intern = nil)
160
+ verbose, $VERBOSE = $VERBOSE, nil
161
+ Encoding.default_external = extern unless extern.nil? || extern == ""
162
+ Encoding.default_internal = intern unless intern.nil? || intern == ""
163
+ [$stdin, $stdout, $stderr].each do |io|
164
+ io.set_encoding(extern, intern)
165
+ end
166
+ ensure
167
+ $VERBOSE = verbose
168
+ end
169
+ module_function :set_encoding
170
+ class << self; private :set_encoding; end
171
+ end
172
+ end
173
+
174
+ ERB::Main.run
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Masatoshi SEKI
8
+ autorequire:
9
+ bindir: libexec
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An easy to use but powerful templating system for Ruby.
14
+ email:
15
+ - seki@ruby-lang.org
16
+ executables:
17
+ - erb
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/test.yml"
22
+ - ".gitignore"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - erb.gemspec
30
+ - lib/erb.rb
31
+ - libexec/erb
32
+ homepage: https://github.com/ruby/erb
33
+ licenses:
34
+ - Ruby
35
+ - BSD-2-Clause
36
+ metadata:
37
+ homepage_uri: https://github.com/ruby/erb
38
+ source_code_uri: https://github.com/ruby/erb
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.0.rc.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: An easy to use but powerful templating system for Ruby.
58
+ test_files: []