jrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ *.log
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jrb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010-2011 Voormedia B.V.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ JRB - Plain Ruby templates
2
+ ==========================
3
+
4
+ JRB is a simple template handler that allows you to use plain Ruby files as
5
+ templates. Use it when you're experimenting with your views. Use it if you
6
+ have templates that don't have much markup. Or use it if you prefer to use
7
+ HTML generators such as [Crafty](https://github.com/voormedia/crafty).
8
+
9
+
10
+ Synopsis
11
+ --------
12
+
13
+ Add `jrb` to your `Gemfile`:
14
+
15
+ gem "jrb"
16
+
17
+ Now you can write Ruby templates whenever you like. JRB renders the last
18
+ return value of your Ruby template.
19
+
20
+ # app/views/examples/hello.html.rb
21
+ "Hello world!"
22
+
23
+ For more complex scenarios, JRB exposes the method `<<`, aliased as `write`.
24
+ It allows you to write directly to the output stream.
25
+
26
+ # app/views/examples/greeting.html.rb
27
+ write "Hello "
28
+ write @name
29
+ write "!"
30
+
31
+ JRB escapes HTML output by default.
32
+
33
+ # app/views/examples/escaping.html.rb
34
+ write "<html>".html_safe
35
+ write @unsafe_content
36
+ write "</html>".html_safe
37
+
38
+ JRB really shines when used in combination with HTML helpers such as those
39
+ provided by [Crafty](https://github.com/voormedia/crafty).
40
+
41
+ # app/controllers/application_controller.rb
42
+ class ApplicationController < ActionController::Base
43
+ helper Crafty::HTML::Basic
44
+ end
45
+
46
+ # app/views/examples/builder.html.rb
47
+ html do
48
+ head do
49
+ title "Hello"
50
+ end
51
+ body do
52
+ div class: ["main", "content"] do
53
+ p "Hello #{@name}!"
54
+ end
55
+ end
56
+ end
57
+
58
+
59
+ About JRB
60
+ ---------
61
+
62
+ JRB was created by Rolf Timmermans (r.timmermans *at* voormedia.com)
63
+
64
+ Copyright 2010-2011 Voormedia - [www.voormedia.com](http://www.voormedia.com/)
65
+
66
+
67
+ License
68
+ -------
69
+
70
+ JRB is released under the MIT license.
@@ -0,0 +1,10 @@
1
+ require "bundler"
2
+ require "rake/testtask"
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.pattern = "test/**/*_test.rb"
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jrb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jrb"
7
+ s.version = JRB::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rolf Timmermans"]
10
+ s.email = ["r.timmermans@voormedia.com"]
11
+ s.homepage = "https://github.com/voormedia/jrb"
12
+ s.summary = %q{JRB templates are Just Ruby}
13
+ s.description = %q{JRB is a trivial templating engine that allows you to use plain Ruby files as templates.}
14
+
15
+ s.rubyforge_project = "jrb"
16
+
17
+ s.add_dependency("tilt", ["~> 1.3"])
18
+ s.add_development_dependency("rails", ["~> 3.0"])
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,5 @@
1
+ require "tilt"
2
+ require "jrb/template"
3
+ require "jrb/rails" if defined?(Rails)
4
+
5
+ Tilt.register JRB::Template, "rb"
@@ -0,0 +1,20 @@
1
+ module JRB
2
+ module Rails
3
+ class TemplateHandler
4
+ class << self
5
+ def call(template)
6
+ tilt_template = JRB::Template.new(template.identifier) { template.source }
7
+ tilt_template.send(:precompiled, {}).first
8
+ end
9
+ end
10
+ end
11
+
12
+ class Railtie < ::Rails::Railtie
13
+ initializer "jrb.register_handler" do |app|
14
+ ActiveSupport.on_load(:action_view) do
15
+ ActionView::Template.register_template_handler "rb", TemplateHandler
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ require "tilt/template"
2
+
3
+ module JRB
4
+ class Template < Tilt::Template
5
+ self.default_mime_type = "text/html"
6
+
7
+ def prepare
8
+ @new_buffer = options.delete(:escape_html) == false ? "''" : "ActiveSupport::SafeBuffer.new"
9
+ end
10
+
11
+ def precompiled_preamble(locals)
12
+ <<-RUBY
13
+ #{super}
14
+ __output_buffer = #{@new_buffer}
15
+ __old_output_buffer, @__output_buffer = @__output_buffer, __output_buffer
16
+ instance_eval do
17
+ def <<(data)
18
+ @__output_buffer << data
19
+ end
20
+ alias write <<
21
+ end
22
+ __result = begin
23
+ RUBY
24
+ end
25
+
26
+ def precompiled_postamble(locals)
27
+ <<-RUBY
28
+ ensure
29
+ @__output_buffer = __old_output_buffer
30
+ end
31
+ __output_buffer << __result unless __result == __output_buffer
32
+ __output_buffer
33
+ RUBY
34
+ end
35
+
36
+ def precompiled_template(locals)
37
+ data.to_str
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module JRB
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path("test_helper", File.dirname(__FILE__))
3
+
4
+ class ActionViewTest < ActionController::TestCase
5
+ tests TestController
6
+
7
+ test "rendering .rb files" do
8
+ get :index, :name => "John"
9
+ assert_equal "<html><head></head><body>Hello John!</body></html>", response.body
10
+ end
11
+
12
+ test "escaping html characters" do
13
+ get :index, :name => "<script>unsafe</script>"
14
+ assert_equal "<html><head></head><body>Hello &lt;script&gt;unsafe&lt;/script&gt;!</body></html>", response.body
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ class TestController < ActionController::Base
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1 @@
1
+ "<head></head>".html_safe
@@ -0,0 +1,4 @@
1
+ write "<html>".html_safe
2
+ write render :partial => "layouts/header"
3
+ write "<body>".html_safe + yield + "</body>".html_safe
4
+ write "</html>".html_safe
@@ -0,0 +1 @@
1
+ "Hello #{params[:name]}!"
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "action_controller/railtie"
4
+
5
+ Bundler.require
6
+ require "jrb"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ config.encoding = "utf-8"
11
+ config.active_support.deprecation = :stderr
12
+ end
13
+ end
14
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ get "/" => "test#index"
3
+ end
@@ -0,0 +1,7 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,204 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path("test_helper", File.dirname(__FILE__))
3
+ require "tempfile"
4
+
5
+ class TiltTemplateTest < ActiveSupport::TestCase
6
+ test "registered for .rb files" do
7
+ assert Tilt.mappings["rb"].include?(JRB::Template)
8
+ end
9
+
10
+ test "loading and evaluating templates on #render" do
11
+ template = JRB::Template.new { "'Hello World!'" }
12
+ assert_equal "Hello World!", template.render
13
+ end
14
+
15
+ test "can be rendered more than once" do
16
+ template = JRB::Template.new { "'Hello World!'" }
17
+ 3.times { assert_equal "Hello World!", template.render }
18
+ end
19
+
20
+ test "passing locals" do
21
+ template = JRB::Template.new { '"Hey " + name + "!"' }
22
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
23
+ end
24
+
25
+ test "evaluating in an object scope" do
26
+ template = JRB::Template.new { '"Hey #{@name}!"' }
27
+ scope = Object.new
28
+ scope.instance_variable_set :@name, 'Joe'
29
+ assert_equal "Hey Joe!", template.render(scope)
30
+ end
31
+
32
+ test "automatic output escaping" do
33
+ template = JRB::Template.new { '"Hey #{@name}!"' }
34
+ scope = Object.new
35
+ scope.instance_variable_set :@name, '<script>Joe</script>'
36
+ assert_equal "Hey &lt;script&gt;Joe&lt;/script&gt;!", template.render(scope)
37
+ end
38
+
39
+ test "disabled automatic output escaping" do
40
+ template = JRB::Template.new(:escape_html => false) { '"Hey #{@name}!"' }
41
+ scope = Object.new
42
+ scope.instance_variable_set :@name, '<script>Joe</script>'
43
+ assert_equal "Hey <script>Joe</script>!", template.render(scope)
44
+ end
45
+
46
+ test "value equals output buffer" do
47
+ template = JRB::Template.new { '__output_buffer << "Hey #{name}!"; __output_buffer' }
48
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
49
+ end
50
+
51
+ test "write method" do
52
+ template = JRB::Template.new { 'write "Hey "; write name; write "!"' }
53
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
54
+ assert_equal "Hey Jane!", template.render(Object.new, :name => 'Jane')
55
+ end
56
+ test "passing a block for yield" do
57
+ template = JRB::Template.new { '"Hey #{yield}!"' }
58
+ assert_equal "Hey Joe!", template.render { 'Joe' }
59
+ end
60
+
61
+ test "backtrace file and line reporting without locals" do
62
+ data = '"Hey #{name}!" + fail'
63
+ template = JRB::Template.new('test.rb', 11) { data }
64
+ begin
65
+ template.render
66
+ fail 'should have raised an exception'
67
+ rescue => boom
68
+ assert_kind_of NameError, boom
69
+ line = boom.backtrace.grep(/^test\.rb:/).first
70
+ assert line, "Backtrace didn't contain test.rb"
71
+ file, line, meth = line.split(":")
72
+ assert_equal '11', line
73
+ end
74
+ end
75
+
76
+ test "backtrace file and line reporting with locals" do
77
+ data = '"Hey #{name}!" + fail'
78
+ template = JRB::Template.new('test.rb', 1) { data }
79
+ begin
80
+ template.render(Object.new, :name => 'Joe', :foo => 'bar')
81
+ fail 'should have raised an exception'
82
+ rescue => boom
83
+ assert_kind_of RuntimeError, boom
84
+ line = boom.backtrace.first
85
+ file, line, meth = line.split(":")
86
+ assert_equal 'test.rb', file
87
+ assert_equal '1', line
88
+ end
89
+ end
90
+ end
91
+
92
+ class CompiledTiltTemplateTest < ActiveSupport::TestCase
93
+ class Scope
94
+ end
95
+
96
+ test "compiling template source to a method" do
97
+ template = JRB::Template.new { "'Hello World!'" }
98
+ template.render(Scope.new)
99
+ method = template.send(:compiled_method, [])
100
+ assert_kind_of UnboundMethod, method
101
+ end
102
+
103
+ test "loading and evaluating templates on #render" do
104
+ template = JRB::Template.new { "'Hello World!'" }
105
+ assert_equal "Hello World!", template.render(Scope.new)
106
+ assert_equal "Hello World!", template.render(Scope.new)
107
+ end
108
+
109
+ test "passing locals" do
110
+ template = JRB::Template.new { '"Hey " + name + "!"' }
111
+ assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
112
+ end
113
+
114
+ test "evaluating in an object scope" do
115
+ template = JRB::Template.new { '"Hey #{@name}!"' }
116
+ scope = Scope.new
117
+ scope.instance_variable_set :@name, 'Joe'
118
+ assert_equal "Hey Joe!", template.render(scope)
119
+ scope.instance_variable_set :@name, 'Jane'
120
+ assert_equal "Hey Jane!", template.render(scope)
121
+ end
122
+
123
+ test "automatic output escaping" do
124
+ template = JRB::Template.new { '"Hey #{@name}!"' }
125
+ scope = Scope.new
126
+ scope.instance_variable_set :@name, '<script>Joe</script>'
127
+ assert_equal "Hey &lt;script&gt;Joe&lt;/script&gt;!", template.render(scope)
128
+ end
129
+
130
+ test "disabled automatic output escaping" do
131
+ template = JRB::Template.new(:escape_html => false) { '"Hey #{@name}!"' }
132
+ scope = Scope.new
133
+ scope.instance_variable_set :@name, '<script>Joe</script>'
134
+ assert_equal "Hey <script>Joe</script>!", template.render(scope)
135
+ end
136
+
137
+ test "value equals output buffer" do
138
+ template = JRB::Template.new { '__output_buffer << "Hey #{name}!"; __output_buffer' }
139
+ assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
140
+ end
141
+
142
+ test "write method" do
143
+ template = JRB::Template.new { 'write "Hey "; write name; write "!"' }
144
+ assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
145
+ assert_equal "Hey Jane!", template.render(Scope.new, :name => 'Jane')
146
+ end
147
+
148
+ test "passing a block for yield" do
149
+ template = JRB::Template.new { '"Hey #{yield}!"' }
150
+ assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
151
+ assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' }
152
+ end
153
+
154
+ test "backtrace file and line reporting without locals" do
155
+ data = '"Hey #{name}!" + fail'
156
+ template = JRB::Template.new('test.erb', 11) { data }
157
+ begin
158
+ template.render(Scope.new)
159
+ fail 'should have raised an exception'
160
+ rescue => boom
161
+ assert_kind_of NameError, boom
162
+ line = boom.backtrace.grep(/^test\.erb:/).first
163
+ assert line, "Backtrace didn't contain test.erb"
164
+ file, line, meth = line.split(":")
165
+ assert_equal '11', line
166
+ end
167
+ end
168
+
169
+ test "backtrace file and line reporting with locals" do
170
+ data = '"Hey #{name}!" + fail'
171
+ template = JRB::Template.new('test.erb') { data }
172
+ begin
173
+ template.render(Scope.new, :name => 'Joe', :foo => 'bar')
174
+ fail 'should have raised an exception'
175
+ rescue => boom
176
+ assert_kind_of RuntimeError, boom
177
+ line = boom.backtrace.first
178
+ file, line, meth = line.split(":")
179
+ assert_equal 'test.erb', file
180
+ assert_equal '1', line
181
+ end
182
+ end
183
+
184
+ test "encoding with magic comment" do
185
+ f = Tempfile.open("template")
186
+ f.puts('# coding: UTF-8')
187
+ f.puts('"ふが #{@hoge}"')
188
+ f.close()
189
+ @hoge = "ほげ"
190
+ jrb = JRB::Template.new(f.path)
191
+ 3.times { jrb.render(self) }
192
+ f.delete
193
+ end
194
+
195
+ test "encoding with :default_encoding" do
196
+ f = Tempfile.open("template")
197
+ f.puts('"ふが #{@hoge}"')
198
+ f.close()
199
+ @hoge = "ほげ"
200
+ jrb = JRB::Template.new(f.path, :default_encoding => 'UTF-8')
201
+ 3.times { jrb.render(self) }
202
+ f.delete
203
+ end
204
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jrb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Rolf Timmermans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: tilt
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "1.3"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "3.0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: JRB is a trivial templating engine that allows you to use plain Ruby files as templates.
38
+ email:
39
+ - r.timmermans@voormedia.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - jrb.gemspec
54
+ - lib/jrb.rb
55
+ - lib/jrb/rails.rb
56
+ - lib/jrb/template.rb
57
+ - lib/jrb/version.rb
58
+ - test/action_view_test.rb
59
+ - test/dummy/app/controllers/test_controller.rb
60
+ - test/dummy/app/views/layouts/_header.html.rb
61
+ - test/dummy/app/views/layouts/test.html.rb
62
+ - test/dummy/app/views/test/index.html.rb
63
+ - test/dummy/config.ru
64
+ - test/dummy/config/application.rb
65
+ - test/dummy/config/boot.rb
66
+ - test/dummy/config/environment.rb
67
+ - test/dummy/config/routes.rb
68
+ - test/test_helper.rb
69
+ - test/tilt_test.rb
70
+ homepage: https://github.com/voormedia/jrb
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project: jrb
93
+ rubygems_version: 1.8.1
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: JRB templates are Just Ruby
97
+ test_files:
98
+ - test/action_view_test.rb
99
+ - test/dummy/app/controllers/test_controller.rb
100
+ - test/dummy/app/views/layouts/_header.html.rb
101
+ - test/dummy/app/views/layouts/test.html.rb
102
+ - test/dummy/app/views/test/index.html.rb
103
+ - test/dummy/config.ru
104
+ - test/dummy/config/application.rb
105
+ - test/dummy/config/boot.rb
106
+ - test/dummy/config/environment.rb
107
+ - test/dummy/config/routes.rb
108
+ - test/test_helper.rb
109
+ - test/tilt_test.rb