mini_erb 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aff6f338bd13d799f411d961cfdff3567c970257
4
+ data.tar.gz: 5f52a24d7c91a2e9f023e9bbac0099df920a1091
5
+ SHA512:
6
+ metadata.gz: 6af56e3cba6a6f1db760840d6dce4298f7fb2c1bc33d9bb66f7d0e15a3066aeae1103a642d0031c0a71fbe595476797f075f970cc4b36e64dc1eff3468ba096b
7
+ data.tar.gz: 2749572d1280b968f2eb61a855d94592971dbeebe526de074efcc9c545f838f67996e693732a0e450557ec5d5804c334dae64519c5367f2dd2441cd9eb7818f9
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at peter.c.camilleri@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mini_erb.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 PeterCamilleri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,254 @@
1
+ # MiniErb
2
+
3
+ The mini_erb gem is another spin off from the mysh project. In testing the
4
+ handlebars embedded Ruby system used there, it was observed that the test
5
+ code was faster than both the native erb and the faster erubi.
6
+
7
+ The mini_erb gem returns to the syntax of erb but is based on the same
8
+ algorithm of the mysh handlebars.
9
+
10
+ In performance it is still faster. This is laid out in this
11
+ [report](https://github.com/PeterCamilleri/mini_erb/blob/master/docs/embbed_ruby_study.pdf)
12
+ .
13
+
14
+ ### Embedding Ruby into text or html
15
+
16
+ The use of erb to embed ruby code into text or html is widely covered, so only
17
+ a brief summary of this topic is presented here.
18
+
19
+ 1. **Embedding ruby computed values into the text.**
20
+
21
+ The following embeds the results of the execution of the ruby into the results.
22
+
23
+ ```ruby
24
+ env = binding
25
+
26
+ str = "ABCD<%= (1..9).to_a.join %>EFGH"
27
+ puts MiniErb.new(str).result(env)
28
+ ```
29
+ produces
30
+
31
+ ABCD123456789EFGH
32
+
33
+ 2. **Using ruby to control the generated text.**
34
+
35
+ You can also use ruby to control what text or html is included in the output.
36
+ This allows the ruby code to select which text/html is included and even to
37
+ repeat sections.
38
+
39
+ ```ruby
40
+ x = 42
41
+ env = binding
42
+
43
+ str = "<% if x==42 %>Life, the Universe and Everything<% else %>Some stuff<% end %>"
44
+ puts MiniErb.new(str).result(env)
45
+ ```
46
+ produces
47
+
48
+ Life, the Universe and Everything
49
+
50
+ and
51
+
52
+ ```ruby
53
+ env = binding
54
+
55
+ str = "<% 5.times { |i| %> <%= i+1 %> sheep <% } %>"
56
+ puts MiniErb.new(str).result(env)
57
+ ```
58
+ produces
59
+
60
+ 1 sheep 2 sheep 3 sheep 4 sheep 5 sheep
61
+
62
+ 3. **Control of debug code.**
63
+
64
+ Sometimes, there is a need to add code for debug or testing purposes. In
65
+ production, we do not want this code interfering with normal operation but we
66
+ may not wish to simply delete it. The following example shows some "debug"
67
+ code.
68
+
69
+ ```ruby
70
+ env = binding
71
+
72
+ str = "Now is the hour of our discontent! <%= "Answer=42" %>"
73
+ puts MiniErb.new(str).result(env)
74
+ ```
75
+ produces
76
+
77
+ Now is the hour of our discontent! Answer=42
78
+
79
+ In production use, we can turn off that code with:
80
+ ```ruby
81
+ env = binding
82
+
83
+ str = "Now is the hour of our discontent! <%#= "Answer=42" %>"
84
+ puts MiniErb.new(str).result(env)
85
+ ```
86
+ and now we get this clean output
87
+
88
+ Now is the hour of our discontent!
89
+
90
+ 4. **Removal of unwanted new-lines**
91
+
92
+ Sometimes in formatting the embedded ruby code, it is desired to add extra
93
+ new-lines to make the code easier to read and understand. These extra lines
94
+ may not be desirable in the output. These can be controlled as follows:
95
+
96
+ ```ruby
97
+ env = binding
98
+
99
+ str = <<-end_of_string
100
+ ABCD<%= (1..9).to_a.join -%>
101
+ EFGH
102
+ end_of_string
103
+
104
+ puts MiniErb.new(str).result(env)
105
+ ```
106
+ produces
107
+
108
+ ABCD123456789EFGH
109
+
110
+ Note how the output does not contain the spurious new line between the
111
+ "9" and "E" characters.
112
+
113
+ ### Differences from traditional erb.
114
+
115
+ The mini_erb gem supports a subset of the functionality of erb.
116
+ The following lays out the differences:
117
+
118
+ * The suppression of new lines with a tag ending in -&#37;> is active by default.
119
+ * The use of code lines starting with a % sign is not supported. Instead use
120
+ <% ... %> embedded code blocks.
121
+ * In erb, <%% maps to <%. In mini_erb use <\&#37; instead. The same goes for
122
+ %%> which is replaced by \&#37;>.
123
+
124
+ ## Installation
125
+
126
+ Add this line to your application's Gemfile:
127
+
128
+ ```ruby
129
+ gem 'mini_erb'
130
+ ```
131
+
132
+ And then execute:
133
+
134
+ $ bundle
135
+
136
+ Or install it yourself as:
137
+
138
+ $ gem install mini_erb
139
+
140
+ ## Usage
141
+
142
+ The use of the mini_erb gem comes in two distinct phases.
143
+
144
+ 1. Creating a mini_erb object. This object contains the source input transpiled
145
+ into ruby code.
146
+ 2. Executing the transpiled code in an execution environment.
147
+
148
+ ### Phase 1
149
+
150
+ Creating a mini_erb object is done simply with the standard new method. The
151
+ corresponding initialize method is:
152
+
153
+ ```ruby
154
+ def initialize(string, safe_level=nil, _=nil, eoutvar='_erbout')
155
+ @safe_level, @eoutvar = safe_level, eoutvar
156
+ @filename, @lineno = "(mini_erb)", 0
157
+ @src = compile(string)
158
+ end
159
+ ```
160
+
161
+ where:
162
+
163
+ * *string* is a string containing the source text embedded with ruby code.
164
+ * *safe_level* is an optional parameter controlling the level of distrust of the
165
+ embedded ruby code. While outside the purview of this file, the following gives
166
+ a nice summary of the available options:
167
+
168
+ safe_level | constraints
169
+ -----------|--------------
170
+ 0 | No checking of the use of externally supplied (tainted) data is performed. This is the default.
171
+ ≥ 1 | Ruby disallows the use of tainted data by potentially dangerous operations.
172
+ ≥ 2 | Ruby prohibits the loading of program files from globally writable locations.
173
+ ≥ 3 | All newly created objects are considered tainted and untrusted.
174
+ ≥ 4 | Ruby effectively partitions the running program in two. Nontrusted objects may not be modified.
175
+
176
+ (Source: Programming Ruby by Dave Thomas with Chad Fowler and Andy Hunt)
177
+
178
+ * *_* is an optional and unused parameter retained only for erb compatibility.
179
+ * *eoutvar* is an optional parameter used to specify a different local variable
180
+ name to contain the results of the embedding process.
181
+
182
+ In many cases, only one parameter is required as in the many examples above.
183
+
184
+ #### Adding location info.
185
+
186
+ For purposes of debugging, it may be useful to tie the transpiled code to a
187
+ file or line number. This information can be added to the mini_erb object as
188
+ follows:
189
+
190
+ ```ruby
191
+ file_name = "my_file.erb"
192
+ erbed = MiniErb.new(IO.read(file_name))
193
+ erbed.filename = file_name
194
+ ```
195
+ This sets up the erb file and establishes its file name for debug purposes.
196
+
197
+ #### The transpiled code.
198
+
199
+ Optionally, the transpiled ruby code may be accessed using the *src* attribute.
200
+ This is most often done to verify that the transpiler is operating correctly
201
+ but is also useful to those who wish to gain insights into the operation of
202
+ the mini_erb gem. It was this same access provided by the erb library that
203
+ made this gem possible.
204
+
205
+ ### Phase 2
206
+
207
+ The next step is to execute the transpiled code in an environment. This
208
+ environment is a ruby binding to the virtual location of the code execution.
209
+ In effect, code is executed from the place where the binding is taken.
210
+
211
+ This use of bindings allows access to any variables that may have been defined
212
+ in that a location and also allows the state of that location to be modified.
213
+ For example:
214
+
215
+ ```ruby
216
+ x = 42
217
+ str = "x is now <%=x%>.<%x+=1%> x is now <%=x%>."
218
+ puts MiniErb.new(str).result(binding), x
219
+ ```
220
+ produces
221
+
222
+ x is now 42. x is now 43.
223
+ 43
224
+
225
+ Note how the environment was modified by adding one to x.
226
+
227
+ If no binding is specified, the binding of the top level file of the current
228
+ program is used as a default.
229
+
230
+
231
+ ## Contributing
232
+
233
+ 1. Fork it
234
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
235
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
236
+ 4. Push to the branch (`git push origin my-new-feature`)
237
+ 5. Create new Pull Request
238
+
239
+ OR...
240
+
241
+ * Make a suggestion by raising an
242
+ [issue](https://github.com/PeterCamilleri/mini_erb/issues)
243
+ . All ideas and comments are welcome.
244
+
245
+ ## License
246
+
247
+ The gem is available as open source under the terms of the
248
+ [MIT License](./LICENSE.txt).
249
+
250
+ ## Code of Conduct
251
+
252
+ Everyone interacting in the mini_erb project’s codebases, issue trackers,
253
+ chat rooms and mailing lists is expected to follow the
254
+ [code of conduct](./CODE_OF_CONDUCT.md).
data/lib/mini_erb.rb ADDED
@@ -0,0 +1,76 @@
1
+ require_relative "mini_erb/version"
2
+
3
+ # A simplified, streamlined erb replacement.
4
+ class MiniErb
5
+
6
+ # The Ruby code generated by mini erb.
7
+ attr_reader :src
8
+
9
+ # The optional file name used when the mini_erb code is run.
10
+ attr_accessor :filename
11
+
12
+ # The optional line number used when the mini_erb code is run.
13
+ attr_accessor :lineno
14
+
15
+ # Set the file name and optional line number associated with the compiled code.
16
+ def location=((filename, lineno))
17
+ @filename = filename
18
+ @lineno = lineno if lineno
19
+ end
20
+
21
+ # Create a mini_erb object. Spurious parameter is for erb compatibility.
22
+ def initialize(string, safe_level=nil, _=nil, eoutvar='_erbout')
23
+ @safe_level, @eoutvar = safe_level, eoutvar
24
+ @filename, @lineno = "(mini_erb)", 0
25
+ @src = compile(string)
26
+ end
27
+
28
+ # Compile the mini erb input
29
+ def compile(string)
30
+ buffer, suppress = [], false
31
+
32
+ until string.empty?
33
+ text, code, string = string.partition(/<%.*?%>/m)
34
+
35
+ text.sub!(/\A$\r?\n?/, "") if suppress
36
+ suppress = false
37
+
38
+ buffer << "#{@eoutvar}<<#{text.inspect};" unless text.empty?
39
+
40
+ unless code.empty?
41
+ end_point = (suppress = code[-3] == "-") ? -3 : -2
42
+
43
+ unless code[2] == "#"
44
+ if code[2] == "="
45
+ buffer << "#{@eoutvar}<<(#{code[3...end_point]}).to_s;"
46
+ else
47
+ buffer << "#{code[2...end_point]};"
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ @eoutvar + "='';" + buffer.join + @eoutvar
54
+ end
55
+
56
+ # Return the mini erb text with embedded Ruby results.
57
+ def result(evaluator = new_toplevel)
58
+ if @safe_level
59
+ proc {$SAFE = @safe_level; eval(@src, evaluator, @filename, @lineno)}.call
60
+ else
61
+ eval(@src, evaluator, @filename, @lineno)
62
+ end
63
+ end
64
+
65
+ # Generate results and print them.
66
+ def run(evaluator = new_toplevel)
67
+ print result(evaluator)
68
+ end
69
+
70
+ private
71
+
72
+ # Get a duplicate of the default binding if none is specified.
73
+ def new_toplevel
74
+ TOPLEVEL_BINDING.dup
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+
2
+ class MiniErb
3
+ VERSION = "0.1.0".freeze
4
+
5
+ DESCRIPTION = "mini_erb: A simplified, streamlined erb replacement.".freeze
6
+ end
data/mini_erb.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "mini_erb/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mini_erb"
7
+ spec.version = MiniErb::VERSION
8
+ spec.authors = ["PeterCamilleri"]
9
+ spec.email = ["peter.c.camilleri@gmail.com"]
10
+
11
+ spec.summary = MiniErb::DESCRIPTION
12
+ spec.description = "A simplified, streamlined, pure ruby replacement for erb."
13
+ spec.homepage = "https://github.com/PeterCamilleri/mini_erb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|docs)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec
21
+ .files
22
+ .reject { |f| f.downcase == 'exe/readme.md'}
23
+ .grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency 'minitest_visible', "~> 0.1"
30
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ desc "What version of mini_erb is this?"
13
+ task :vers do |t|
14
+ puts
15
+ puts "mini_erb version = #{::MiniErb::VERSION}"
16
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PeterCamilleri
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest_visible
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ description: A simplified, streamlined, pure ruby replacement for erb.
70
+ email:
71
+ - peter.c.camilleri@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - lib/mini_erb.rb
82
+ - lib/mini_erb/version.rb
83
+ - mini_erb.gemspec
84
+ - rakefile.rb
85
+ homepage: https://github.com/PeterCamilleri/mini_erb
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.5.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: 'mini_erb: A simplified, streamlined erb replacement.'
109
+ test_files: []