renee-render 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,7 +5,7 @@ Rendering templates in Renee should be familiar and intuitive using the `render`
5
5
  ```ruby
6
6
  run Renee::Core.new {
7
7
  path('blog') do
8
- get { render! :haml, :"blogs/index" }
8
+ get { render! "blogs/index", :haml }
9
9
  end
10
10
  }
11
11
  ```
@@ -1,5 +1,6 @@
1
1
  class Renee
2
2
  module Render
3
- VERSION = "0.1.1"
3
+ # Current Renee-render version number.
4
+ VERSION = "0.2.0"
4
5
  end
5
6
  end
data/lib/renee-render.rb CHANGED
@@ -1,108 +1,115 @@
1
1
  require 'tilt'
2
+ require 'callsite'
2
3
 
4
+ # Top-level Renee constant
3
5
  class Renee
4
6
  # This module is responsible for handling the rendering of templates
5
7
  # using Tilt supporting all included template engines.
6
8
  module Render
7
- ##
8
- # Exception responsible for when a generic rendering error occurs.
9
- #
10
- class RenderError < RuntimeError; end
11
-
12
9
  ##
13
10
  # Exception responsible for when an expected template does not exist.
14
11
  #
15
- class TemplateNotFound < RenderError; end
12
+ class TemplateNotFound < RuntimeError; end
16
13
 
17
14
  # Same as render but automatically halts.
18
15
  # @param (see #render)
19
16
  # @return (see #render)
20
17
  # @see #render
21
- def render!(*args, &blk)
22
- halt render(*args, &blk)
18
+ def render!(file, engine = nil, options = nil, &blk)
19
+ halt render(file, engine, options, &blk)
20
+ end
21
+
22
+ # Same as inline but automatically halts.
23
+ # @param (see #inline)
24
+ # @return (see #inline)
25
+ # @see #inline
26
+ def inline!(data, engine, options = {}, &blk)
27
+ options[:_caller] = Callsite.parse(caller.first)
28
+ halt inline(data, engine, options, &blk)
23
29
  end
24
30
 
31
+ ##
32
+ # Renders a file given the engine and the content.
33
+ #
34
+ # @param [String] file The path to the file to render
35
+ # @param [Symbol] engine The name of the engine to use to render the content. If this isn't specified
36
+ # it will be detected based on the extension of the file.
37
+ # @param [Hash] options The options to pass in for rendering.
38
+ #
39
+ # @return [String] The result of rendering the data with specified engine.
40
+ #
41
+ # @example
42
+ # render "index", :haml # => "<p>test</p>"
43
+ # render "index" # => "<p>test</p>"
44
+ #
45
+ # @api public
46
+ #
47
+ def render(file, engine = nil, options = nil, &block)
48
+ options, engine = engine, nil if engine.is_a?(Hash)
49
+ render_setup(engine, options, block) do |view_options, views|
50
+ template_cache.fetch(engine, file, view_options) do
51
+ file_path, found_engine = find_template(views, file, engine)
52
+ template = Tilt[found_engine]
53
+ raise TemplateNotFound, "Template engine not found: #{found_engine.inspect}" unless template
54
+ raise TemplateNotFound, "Template #{file.inspect} (with engine #{engine.inspect}) not found in #{views.inspect}!" unless file_path
55
+ # TODO suppress errors for layouts?
56
+ template.new(file_path, 1, view_options)
57
+ end
58
+ end
59
+ end # render
60
+
25
61
  ##
26
62
  # Renders a string given the engine and the content.
27
63
  #
28
- # @param [Symbol] engine The template engine to use for rendering.
29
- # @param [String] data The content or file to render.
30
- # @param [Hash] options The rendering options to pass onto tilt.
64
+ # @param [String] data The string data to render.
65
+ # @param [Symbol] engine The name of the engine to use to render the content. If this isn't specified
66
+ # it will be detected based on the extension of the file.
67
+ # @param [Hash] options The options to pass in for rendering.
31
68
  #
32
69
  # @return [String] The result of rendering the data with specified engine.
33
70
  #
34
71
  # @example
35
- # render :haml, "%p test" => "<p>test</p>"
36
- # render :haml, :index => "<p>test</p>"
37
- # render "index" => "<p>test</p>"
72
+ # inline "%p test", :haml # => "<p>test</p>"
38
73
  #
39
74
  # @api public
40
75
  #
41
- def render(engine, data=nil, options={}, &block)
42
- # Handles the case where engine is unspecified by shifting the data (i.e render "index")
43
- engine, data, options = nil, engine.to_sym, data if data.nil? || data.is_a?(Hash)
76
+ def inline(data, engine, options = nil, &block)
77
+ options, engine = engine, nil if engine.is_a?(Hash)
78
+ call_data = options.delete(:_caller) || Callsite.parse(caller.first)
79
+ render_setup(engine, options, block) do |view_options, views|
80
+ body = data.is_a?(Proc) ? data : Proc.new { data }
81
+ template = Tilt[engine]
82
+ raise "Template engine not found: #{engine}" if template.nil?
83
+ template.new(call_data.filename, call_data.line, view_options, &body)
84
+ end
85
+ end
44
86
 
87
+ private
88
+ def render_setup(engine, options, block)
45
89
  options ||= {}
46
90
  options[:outvar] ||= '@_out_buf'
47
- # TODO allow default encoding to be set (as an option)
48
- options[:default_encoding] ||= "utf-8"
91
+ options[:default_encoding] ||= settings.default_encoding || options[:encoding] || "utf-8"
49
92
 
50
93
  locals = options.delete(:locals) || {}
51
94
  views = options.delete(:views) || settings.views_path || "./views"
52
95
  layout = options.delete(:layout)
53
- layout_engine = options.delete(:layout_engine) || engine
96
+ layout_engine = options.delete(:layout_engine)
54
97
  # TODO suppress template errors for layouts?
55
98
  # TODO allow content_type to be set with an option to render?
56
99
  scope = options.delete(:scope) || self
57
100
 
58
101
  # TODO default layout file convention?
59
- template = compile_template(engine, data, options, views)
102
+ template = yield(options, views)
60
103
  output = template.render(scope, locals, &block)
61
104
 
62
105
  if layout # render layout
63
106
  # TODO handle when layout is missing better!
64
107
  options = options.merge(:views => views, :layout => false, :scope => scope)
65
- return render(layout_engine, layout, options.merge(:locals => locals)) { output }
108
+ render(layout, layout_engine, options.merge(:locals => locals)) { output }
109
+ else
110
+ output
66
111
  end
67
-
68
- output
69
- end # render
70
-
71
- ##
72
- # Constructs a template based on engine, data and options.
73
- #
74
- # @param [Symbol] engine The template engine to use for rendering.
75
- # @param [String] data The content or file to render.
76
- # @param [Hash] options The rendering options to pass onto tilt.
77
- # @param [String] views The view_path from which to locate the template.
78
- #
79
- # @return [Tilt::Template] The tilt template to render with all required options.
80
- # @raise [TemplateNotFound] The template to render could not be located.
81
- # @raise [RenderError] The template to render could not be located.
82
- #
83
- # @api private
84
- #
85
- def compile_template(engine, data, options, views)
86
- template_cache.fetch engine, data, options do
87
- if data.is_a?(Symbol) # data is template path
88
- file_path, engine = find_template(views, data, engine)
89
- template = Tilt[engine]
90
- raise TemplateNotFound, "Template engine not found: #{engine}" if template.nil?
91
- raise TemplateNotFound, "Template '#{data}' not found in '#{views}'!" unless file_path
92
- # TODO suppress errors for layouts?
93
- template.new(file_path, 1, options)
94
- elsif data.is_a?(String) # data is body string
95
- # TODO figure out path based on caller file
96
- path, line = options[:path] || "caller file", options[:line] || 1
97
- body = data.is_a?(String) ? Proc.new { data } : data
98
- template = Tilt[engine]
99
- raise "Template engine not found: #{engine}" if template.nil?
100
- template.new(path, line.to_i, options, &body)
101
- else # data can't be handled
102
- raise RenderError, "Cannot render data #{data.inspect}."
103
- end
104
- end # template_cache.fetch
105
- end # compile_template
112
+ end
106
113
 
107
114
  ##
108
115
  # Searches view paths for template based on data and engine with rendering options.
data/renee-render.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_runtime_dependency 'rack', "~> 1.3.0"
22
22
  s.add_runtime_dependency 'tilt', "~> 1.3.3"
23
+ s.add_runtime_dependency 'callsite', '~> 0.0.6'
23
24
  s.add_development_dependency 'minitest', "~> 2.6.1"
24
25
  s.add_development_dependency 'rake'
25
26
  s.add_development_dependency 'bundler', "~> 1.0.10"
data/test/render_test.rb CHANGED
@@ -6,9 +6,9 @@ describe Renee::Render do
6
6
 
7
7
  it "should allow rendering string with engine" do
8
8
  mock_app {
9
- path("/a") { get { render! :erb, "<p>test</p>" } }
10
- path("/b") { get { render! :erb, "<p><%= foo %></p>", :locals => { :foo => "bar" } } }
11
- path("/c") { get { halt render(:erb, "<p><%= foo %></p>", :locals => { :foo => "bar" }) } }
9
+ path("/a") { get { inline! "<p>test</p>", :erb } }
10
+ path("/b") { get { inline! "<p><%= foo %></p>", :erb, :locals => { :foo => "bar" } } }
11
+ path("/c") { get { halt inline("<p><%= foo %></p>", :erb, :locals => { :foo => "bar" }) } }
12
12
  }
13
13
  get('/a')
14
14
  assert_equal 200, response.status
@@ -25,9 +25,9 @@ describe Renee::Render do
25
25
  create_view :index, "%p test", :haml
26
26
  create_view :foo, "%p= foo", :haml
27
27
  mock_app {
28
- path("/a") { get { render! :haml, :index } }
29
- path("/b") { get { render! :haml, :foo, :locals => { :foo => "bar" } } }
30
- path("/c") { get { halt render(:haml, :foo, :locals => { :foo => "bar" }) } }
28
+ path("/a") { get { render! 'index', :haml } }
29
+ path("/b") { get { render! 'foo', :haml, :locals => { :foo => "bar" } } }
30
+ path("/c") { get { halt render('foo', :haml, :locals => { :foo => "bar" }) } }
31
31
  }
32
32
  get('/a')
33
33
  assert_equal 200, response.status
@@ -60,8 +60,8 @@ describe Renee::Render do
60
60
  create_view :foo, "%p= foo", :haml
61
61
  create_view :layout, "%div.wrapper= yield", :haml
62
62
  mock_app {
63
- path("/a") { get { render! :haml, :index, :layout => :layout } }
64
- path("/b") { get { render! :foo, :layout => :layout, :locals => { :foo => "bar" } } }
63
+ path("/a") { get { render! 'index', :haml, :layout => :layout } }
64
+ path("/b") { get { render! 'foo', :layout => :layout, :locals => { :foo => "bar" } } }
65
65
  }
66
66
  get('/a')
67
67
  assert_equal 200, response.status
@@ -76,8 +76,8 @@ describe Renee::Render do
76
76
  create_view :foo, "%p= foo", :haml
77
77
  create_view :base, "<div class='wrapper'><%= yield %></div>", :erb
78
78
  mock_app {
79
- path("/a") { get { render! :haml, :index, :layout => :base, :layout_engine => :erb } }
80
- path("/b") { get { render! :foo, :layout => :base, :locals => { :foo => "bar" } } }
79
+ path("/a") { get { render! 'index', :haml, :layout => 'base', :layout_engine => :erb } }
80
+ path("/b") { get { render! 'foo', :haml, :layout => 'base', :locals => { :foo => "bar" } } }
81
81
  }
82
82
  get('/a')
83
83
  assert_equal 200, response.status
@@ -102,13 +102,5 @@ describe Renee::Render do
102
102
  }
103
103
  assert_raises(Renee::Render::TemplateNotFound) { get('/') }
104
104
  end # missing template, with engine
105
-
106
- it "should fail properly rendering invalid data" do
107
- create_view :index, "%p test", :haml
108
- mock_app {
109
- get { render! :haml, /invalid regex data/ }
110
- }
111
- assert_raises(Renee::Render::RenderError) { get('/') }
112
- end # missing template, with engine
113
105
  end
114
106
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: renee-render
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease:
5
- version: 0.1.1
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
6
11
  platform: ruby
7
12
  authors:
8
13
  - Josh Hull
@@ -12,85 +17,134 @@ autorequire:
12
17
  bindir: bin
13
18
  cert_chain: []
14
19
 
15
- date: 2011-10-19 00:00:00 Z
20
+ date: 2011-10-20 00:00:00 Z
16
21
  dependencies:
17
22
  - !ruby/object:Gem::Dependency
18
- name: rack
19
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
20
24
  none: false
21
25
  requirements:
22
26
  - - ~>
23
27
  - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 1
31
+ - 3
32
+ - 0
24
33
  version: 1.3.0
34
+ requirement: *id001
25
35
  type: :runtime
26
36
  prerelease: false
27
- version_requirements: *id001
37
+ name: rack
28
38
  - !ruby/object:Gem::Dependency
29
- name: tilt
30
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
31
40
  none: false
32
41
  requirements:
33
42
  - - ~>
34
43
  - !ruby/object:Gem::Version
44
+ hash: 29
45
+ segments:
46
+ - 1
47
+ - 3
48
+ - 3
35
49
  version: 1.3.3
50
+ requirement: *id002
36
51
  type: :runtime
37
52
  prerelease: false
38
- version_requirements: *id002
53
+ name: tilt
39
54
  - !ruby/object:Gem::Dependency
40
- name: minitest
41
- requirement: &id003 !ruby/object:Gem::Requirement
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
42
56
  none: false
43
57
  requirements:
44
58
  - - ~>
45
59
  - !ruby/object:Gem::Version
60
+ hash: 19
61
+ segments:
62
+ - 0
63
+ - 0
64
+ - 6
65
+ version: 0.0.6
66
+ requirement: *id003
67
+ type: :runtime
68
+ prerelease: false
69
+ name: callsite
70
+ - !ruby/object:Gem::Dependency
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 21
77
+ segments:
78
+ - 2
79
+ - 6
80
+ - 1
46
81
  version: 2.6.1
82
+ requirement: *id004
47
83
  type: :development
48
84
  prerelease: false
49
- version_requirements: *id003
85
+ name: minitest
50
86
  - !ruby/object:Gem::Dependency
51
- name: rake
52
- requirement: &id004 !ruby/object:Gem::Requirement
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
53
88
  none: false
54
89
  requirements:
55
90
  - - ">="
56
91
  - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
57
95
  version: "0"
96
+ requirement: *id005
58
97
  type: :development
59
98
  prerelease: false
60
- version_requirements: *id004
99
+ name: rake
61
100
  - !ruby/object:Gem::Dependency
62
- name: bundler
63
- requirement: &id005 !ruby/object:Gem::Requirement
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
64
102
  none: false
65
103
  requirements:
66
104
  - - ~>
67
105
  - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 1
109
+ - 0
110
+ - 10
68
111
  version: 1.0.10
112
+ requirement: *id006
69
113
  type: :development
70
114
  prerelease: false
71
- version_requirements: *id005
115
+ name: bundler
72
116
  - !ruby/object:Gem::Dependency
73
- name: rack-test
74
- requirement: &id006 !ruby/object:Gem::Requirement
117
+ version_requirements: &id007 !ruby/object:Gem::Requirement
75
118
  none: false
76
119
  requirements:
77
120
  - - ">="
78
121
  - !ruby/object:Gem::Version
122
+ hash: 11
123
+ segments:
124
+ - 0
125
+ - 5
126
+ - 0
79
127
  version: 0.5.0
128
+ requirement: *id007
80
129
  type: :development
81
130
  prerelease: false
82
- version_requirements: *id006
131
+ name: rack-test
83
132
  - !ruby/object:Gem::Dependency
84
- name: haml
85
- requirement: &id007 !ruby/object:Gem::Requirement
133
+ version_requirements: &id008 !ruby/object:Gem::Requirement
86
134
  none: false
87
135
  requirements:
88
136
  - - ">="
89
137
  - !ruby/object:Gem::Version
138
+ hash: 7
139
+ segments:
140
+ - 2
141
+ - 2
142
+ - 0
90
143
  version: 2.2.0
144
+ requirement: *id008
91
145
  type: :development
92
146
  prerelease: false
93
- version_requirements: *id007
147
+ name: haml
94
148
  description: The super-friendly web framework rendering component.
95
149
  email:
96
150
  - joshbuddy@gmail.com
@@ -124,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
178
  requirements:
125
179
  - - ">="
126
180
  - !ruby/object:Gem::Version
127
- hash: -2087846859516822907
181
+ hash: 3
128
182
  segments:
129
183
  - 0
130
184
  version: "0"
@@ -133,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
187
  requirements:
134
188
  - - ">="
135
189
  - !ruby/object:Gem::Version
136
- hash: -2087846859516822907
190
+ hash: 3
137
191
  segments:
138
192
  - 0
139
193
  version: "0"