tempeh 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: e662a14d9152dd7487a8b69e550d16b075886ca8
4
+ data.tar.gz: 50a9fb441f3823938a2f8d06b620d902b40389a3
5
+ SHA512:
6
+ metadata.gz: 4bfe92cdcea987941dc31ac079708b86f9c0fbf8926390fc8e92fd42b49594f0408e44a51ea8650dd21d173749c16156d47bc42c20feff2390941b69d3d4b6d3
7
+ data.tar.gz: deecdba9c512a409722888a1bbb464ecffe8eb9955cdd9e22bbfbc7e66a3a69e1e8751e2b241dcf706e65cd7aa217e4381cba259f1c33614889b880983675b93
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .gs/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tempeh (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ clap (1.0.0)
10
+ cutest (1.2.3)
11
+ clap
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ cutest
18
+ tempeh!
19
+
20
+ BUNDLED WITH
21
+ 1.14.6
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2011 Michel Martens
4
+ Copyright (c) 2016 Francesco Rodriguez
5
+ Copyright (c) 2017 Steven Weiss
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ Tempeh
2
+ ======
3
+
4
+ Tasty Ruby Templates
5
+
6
+ Description
7
+ -----------
8
+
9
+ Tempeh is an opininated template engine for rendering html. It steals
10
+ most of its ideas from Mote and also the Herb fork. That means it is a
11
+ small layer on top of Ruby that escapes strings by default. The render API
12
+ differs from both, as the context the template gets rendered with does not
13
+ get "compiled" with the template, instead the context gets lazily executed
14
+ each time when passed to `#render`.
15
+
16
+ Usage
17
+ -----
18
+
19
+ ```ruby
20
+ template = Tempeh.new("
21
+ % if hungry? %
22
+ Eat { args[:food] }!
23
+ % end %
24
+ ")
25
+
26
+ module Steve
27
+ def self.hungry?
28
+ true # always...
29
+ end
30
+ end
31
+
32
+ template.render(Steve, food: 'Tempeh') # Eat Tempeh!
33
+ ```
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ task :gems do
2
+ sh "mkdir -p .gs"
3
+ end
4
+
5
+ task install: [:gems] do
6
+ sh "gs bundle install --system"
7
+ end
8
+
9
+ task :test do
10
+ sh "gs cutest -r ./test/*.rb"
11
+ end
data/lib/tempeh.rb ADDED
@@ -0,0 +1,57 @@
1
+ class Tempeh
2
+ VERSION = "0.1.0"
3
+
4
+ BLK_OPEN = '%'
5
+
6
+ BLK_CLOSE = '%'
7
+
8
+ STR_OPEN = '{'
9
+
10
+ STR_CLOSE = '}'
11
+
12
+ PATTERN = /
13
+ (?<!\\)(#{BLK_OPEN})\s+(.*?)\s+#{BLK_CLOSE} | # Multiline Ruby blocks.
14
+ (?<!\\)(#{STR_OPEN})(.*?)#{STR_CLOSE} # Ruby to be evaluated a string.
15
+ /mx
16
+
17
+ HTML_ESCAPE = {
18
+ "&" => "&amp;",
19
+ ">" => "&gt;",
20
+ "<" => "&lt;",
21
+ '"' => "&#39;",
22
+ "'" => "&#34;",
23
+ }.freeze
24
+
25
+ UNSAFE = /[&"'><]/
26
+
27
+ class << self
28
+ def compile(str)
29
+ terms = str.split(PATTERN)
30
+ parts = "proc do |args = {}, __o = ''|"
31
+
32
+ while (term = terms.shift)
33
+ case term
34
+ when BLK_OPEN then parts << terms.shift << "\n"
35
+ when STR_OPEN then parts << "__o << Tempeh.escape((" << terms.shift << ").to_s)\n"
36
+ else parts << "__o << " << term.dump << "\n"
37
+ end
38
+ end
39
+
40
+ parts << "__o; end"
41
+
42
+ eval(parts)
43
+ end
44
+
45
+ def escape(str)
46
+ str.gsub(UNSAFE, HTML_ESCAPE)
47
+ end
48
+ end
49
+
50
+ def initialize(str)
51
+ @code = Tempeh.compile(str)
52
+ end
53
+
54
+ def render(context = nil, **args)
55
+ context.instance_exec(args, &@code)
56
+ end
57
+ end
data/tempeh.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ require_relative "./lib/tempeh"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "tempeh"
5
+ s.summary = "Tempeh"
6
+ s.version = Tempeh::VERSION
7
+ s.authors = ["Steve Weiss"]
8
+ s.email = ["weissst@mail.gvsu.edu"]
9
+ s.homepage = "https://github.com/sirscriptalot/tempeh"
10
+ s.license = "MIT"
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
data/test/all.rb ADDED
@@ -0,0 +1,57 @@
1
+ require_relative '../lib/tempeh'
2
+
3
+ test 'empty lines' do
4
+ template = Tempeh.new("\n\n \n")
5
+
6
+ assert_equal "\n\n \n", template.render
7
+ end
8
+
9
+ test 'quotes' do
10
+ template = Tempeh.new("'foo' 'bar' 'baz'")
11
+
12
+ assert_equal "'foo' 'bar' 'baz'", template.render
13
+ end
14
+
15
+ test 'block expressions' do
16
+ template = Tempeh.new("% if true %yes% else %no% end %")
17
+
18
+ assert_equal "yes", template.render
19
+ end
20
+
21
+ test 'string expressions' do
22
+ template = Tempeh.new("{ 'Hello' }")
23
+
24
+ assert_equal "Hello", template.render
25
+ end
26
+
27
+ test 'escapes string expressions' do
28
+ template = Tempeh.new(%q({ %q(<>&"') }))
29
+
30
+ assert_equal "&lt;&gt;&amp;&#39;&#34;", template.render
31
+ end
32
+
33
+ test 'nil string expressions' do
34
+ template = Tempeh.new("{ nil }")
35
+
36
+ assert_equal "", template.render
37
+ end
38
+
39
+ test 'allows escaping expressions' do
40
+ template = Tempeh.new('\{ nil }\% hello %')
41
+
42
+ assert_equal '\{ nil }\% hello %', template.render
43
+ end
44
+
45
+ test 'render with context' do
46
+ context = Struct.new(:foo).new('bar')
47
+
48
+ template = Tempeh.new("{ foo }")
49
+
50
+ assert_equal "bar", template.render(context)
51
+ end
52
+
53
+ test 'render with args' do
54
+ template = Tempeh.new("{ args[:foo] }")
55
+
56
+ assert_equal "bar", template.render(foo: "bar")
57
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tempeh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Weiss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - weissst@mail.gvsu.edu
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/tempeh.rb
41
+ - tempeh.gemspec
42
+ - test/all.rb
43
+ homepage: https://github.com/sirscriptalot/tempeh
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.11
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Tempeh
67
+ test_files: []