inline_templates 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1391230187758595f59ccb744dbb3ccb124aef1d
4
+ data.tar.gz: ade328c124f06942ada52b3f6198f5038ec90a9e
5
+ SHA512:
6
+ metadata.gz: 19f7c6edded139cd6b9e8fa0534ea9725bbff35f07e5c526ea901469a947183998845a3bc3e1c77592407a8205b39cbc7002fc11d0bb5fc9af6e5a8e7bd1dde1
7
+ data.tar.gz: b8f383aec61fd96b5a25a121c57f7cf70de16597af31735eaeb9cce2d09ee127e09cf4820fd1a5689dd9a30b5898a723a459787252764bf50afdb533f0b7b7a4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in inline_templates.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Gridasov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Inline Templates
2
+
3
+ Inline Templates allow you to write HTML markup in your controllers just like arbre, but without its inherent incompatibilities. All helpers - Rails builtin, provided by other gems and yours - are available out of box.
4
+
5
+ For example:
6
+ ```ruby
7
+ @inline_html = rit do
8
+ ~ form_for(:session) do |f|
9
+ ~ div(class: "fields") do
10
+ ~ div(class: "field") do
11
+ ~ f.label(:email, 'E-Mail')
12
+ ~ f.text_field(:email)
13
+ end
14
+ ~ div(class: "field") do
15
+ ~ f.label(:password, 'Password')
16
+ ~ f.password_field(:password)
17
+ end
18
+ end
19
+
20
+ ~ div(class: "actions") do
21
+ ~ f.submit
22
+ end
23
+ end
24
+ end
25
+ ```
26
+
27
+ Plain strings (i.e. not helpers output or variable values) should be escaped by `t` or `h` helper before passing them to `~` for output. `t` should be used for text and `h` should be used for HTML.
28
+
29
+ Inline Templates can also be used in views. It handles `.rit` files with same syntax.
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ gem 'inline_templates'
36
+
37
+ And then execute:
38
+
39
+ $ bundle
40
+
41
+ Or install it yourself as:
42
+
43
+ $ gem install inline_templates
44
+
45
+ ## Usage
46
+
47
+ In Rails controllers:
48
+ ```ruby
49
+ class TestController < ApplicationController
50
+ include InlineMarkup::Helpers
51
+
52
+ def test
53
+ @inline_html = rit(:list => [ 'a', 'b', 'c' ]) do
54
+ ~ ul do
55
+ list.each do |item|
56
+ ~ li(item)
57
+ end
58
+ end
59
+ end
60
+ ```
61
+
62
+ Standalone:
63
+ ```ruby
64
+ view = ActionView::Base.new context, assigns, controller, formats
65
+ inline_html = InlineTemplates.render view, details, locals do
66
+ ~ t('foo')
67
+ end
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inline_templates/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "inline_templates"
8
+ spec.version = InlineTemplates::VERSION
9
+ spec.authors = ["Sergey Gridasov"]
10
+ spec.email = ["grindars@gmail.com"]
11
+ spec.description = %q{Inline templates for Rails controllers with Arbre-like DSL}
12
+ spec.summary = %q{Inline templates for Rails}
13
+ spec.homepage = "https://github.com/grindars/inline_templates"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ">= 2"
24
+
25
+ spec.add_dependency 'actionpack', '>= 3'
26
+ end
@@ -0,0 +1,27 @@
1
+ module InlineTemplates
2
+ class BlankObject < BasicObject
3
+ include ::Kernel
4
+
5
+ def self.make_blank(*keep)
6
+ old_verbose = $VERBOSE
7
+ begin
8
+ $VERBOSE = nil
9
+
10
+ drop_methods [ :!, :!=, :==, :__id__, :__send__, :equal?, :instance_eval, :instance_exec ], keep
11
+ drop_methods ::Kernel.instance_methods, keep
12
+ drop_methods ::Kernel.private_instance_methods, keep
13
+
14
+ ensure
15
+ $VERBOSE = old_verbose
16
+ end
17
+ end
18
+
19
+ def self.drop_methods(methods, keep)
20
+ methods.each do |name|
21
+ next if keep.include? name
22
+
23
+ undef_method name
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ module InlineTemplates
2
+ class BufferWrapper < BlankObject
3
+ make_blank :respond_to?
4
+
5
+ def initialize(object, buffer)
6
+ @object = object
7
+ @buffer = buffer
8
+ end
9
+
10
+ def __inline_templates_object; @object; end
11
+
12
+ def ~
13
+ @buffer.append = @object
14
+ @object
15
+ end
16
+
17
+ def method_missing(name, *args, &block)
18
+ args.map! &BufferWrapper.method(:unwrap)
19
+
20
+ BufferWrapper.wrap @object.__send__(name, *args, &block), @buffer
21
+ end
22
+
23
+ def respond_to_missing?(name, include_private = false)
24
+ @object.respond_to?(name, include_private)
25
+ end
26
+
27
+ def self.wrap(result, buffer)
28
+ if result.class == ::NilClass || result.class == ::TrueClass || result.class == ::FalseClass
29
+ result
30
+ else
31
+ BufferWrapper.new(self.unwrap(result), buffer)
32
+ end
33
+ end
34
+
35
+ def self.unwrap(obj)
36
+ if obj.respond_to? :__inline_templates_object
37
+ obj.__inline_templates_object
38
+ else
39
+ obj
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ module InlineTemplates
2
+ class Builder
3
+ def build(context, name, *args, &block)
4
+ context.content_tag name, *args, &block
5
+ end
6
+
7
+ def can_build?(name)
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module InlineTemplates
2
+ module Helpers
3
+ extend ActiveSupport::Concern
4
+
5
+ def rit(locals = {}, &block)
6
+ InlineTemplates.render(view_context, {}, locals, &block)
7
+ end
8
+
9
+ included do
10
+ protected :rit
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ module InlineTemplates
2
+ class RenderingContext < BlankObject
3
+ make_blank :instance_exec, :instance_variable_set
4
+
5
+ def initialize(context, locals, builder)
6
+ @_inlinetemplates_context = context
7
+ @_inlinetemplates_locals = locals
8
+ @_inlinetemplates_evaluating = true
9
+ @_inlinetemplates_builder = builder
10
+
11
+ context.instance_variables.each do |var|
12
+ instance_variable_set var, context.instance_variable_get(var)
13
+ end
14
+ end
15
+
16
+ def t(obj)
17
+ BufferWrapper.wrap obj.to_s, @_inlinetemplates_context.output_buffer
18
+ end
19
+
20
+ def h(obj)
21
+ BufferWrapper.wrap obj.to_s.html_safe, @_inlinetemplates_context.output_buffer
22
+ end
23
+
24
+ def method_missing(name, *args, &block)
25
+ args.map! &BufferWrapper.method(:unwrap)
26
+
27
+ if @_inlinetemplates_locals.include?(name) && args.length == 0
28
+ result = @_inlinetemplates_locals[name]
29
+
30
+ elsif @_inlinetemplates_context.respond_to?(name, true)
31
+ result = @_inlinetemplates_context.__send__ name, *args, &block
32
+
33
+ elsif @_inlinetemplates_builder.can_build?(name)
34
+ result = @_inlinetemplates_builder.build @_inlinetemplates_context, name, *args, &block
35
+
36
+ else
37
+ super
38
+ end
39
+
40
+ BufferWrapper.wrap result, @_inlinetemplates_context.output_buffer
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ module InlineTemplates
2
+ class TemplateHandler
3
+ def call(source)
4
+ <<-EOF
5
+ @output_buffer ||= ActionView::OutputBuffer.new
6
+ context = ::InlineTemplates::RenderingContext.new(self, local_assigns, ::InlineTemplates::Builder.new)
7
+ context.instance_exec do
8
+ #{source.source}
9
+ end
10
+ @output_buffer.to_s
11
+ EOF
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module InlineTemplates
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "active_support"
2
+ require "action_view"
3
+
4
+ require "inline_templates/version"
5
+ require "inline_templates/builder"
6
+ require "inline_templates/blank_object"
7
+ require "inline_templates/buffer_wrapper"
8
+ require "inline_templates/rendering_context"
9
+ require "inline_templates/helpers"
10
+ require "inline_templates/template_handler.rb"
11
+
12
+ module InlineTemplates
13
+ def self.render(view, details, locals, &block)
14
+ identifier = "(inline:#{block.inspect})"
15
+
16
+ handler = ->(template) do
17
+ method_name = nil
18
+ template.instance_exec do
19
+ method_name = self.method_name
20
+
21
+ view.singleton_class.send :define_method, "#{method_name}_block" do
22
+ block
23
+ end
24
+ end
25
+
26
+ <<-EOF
27
+ @output_buffer ||= ActionView::OutputBuffer.new
28
+ context = ::InlineTemplates::RenderingContext.new(self, local_assigns, ::InlineTemplates::Builder.new)
29
+ context.instance_exec &self.#{method_name}_block
30
+ @output_buffer.to_s
31
+ EOF
32
+ end
33
+
34
+ template = ActionView::Template.new("", identifier, handler, details)
35
+ template.render view, locals
36
+ end
37
+ end
38
+
39
+ ActionView::Template.register_template_handler :rit, InlineTemplates::TemplateHandler.new
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe InlineTemplates::Helpers do
4
+ it "defines helper" do
5
+ invoke_controller(TestController, :test).should == TestController::ReferenceOutput
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe InlineTemplates::TemplateHandler do
4
+ it "renders" do
5
+ invoke_controller(TestController, :test_file).should == TestController::ReferenceOutput
6
+ end
7
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe InlineTemplates do
4
+ describe 'render' do
5
+ it 'builds elements' do
6
+ test_rit do
7
+ ~ span('test')
8
+ end.should == "<span>test</span>"
9
+ end
10
+
11
+ it 'builds multiple elements' do
12
+ test_rit do
13
+ ~ span('test 1')
14
+ ~ span('test 2')
15
+ end.should == "<span>test 1</span><span>test 2</span>"
16
+ end
17
+
18
+ it 'captures' do
19
+ test_rit do
20
+ ~ div do
21
+ ~ span('test')
22
+ end
23
+ end.should == "<div><span>test</span></div>"
24
+ end
25
+
26
+ it 'handles locals and conditionals' do
27
+ locals = {
28
+ :cond => true,
29
+ :a => 'foo',
30
+ :b => 'bar'
31
+ }
32
+
33
+ test_block = proc do
34
+ if cond
35
+ ~ a
36
+ else
37
+ ~ b
38
+ end
39
+ end
40
+
41
+ test_rit(locals, &test_block).should == "foo"
42
+ locals[:cond] = false
43
+ test_rit(locals, &test_block).should == "bar"
44
+ end
45
+
46
+ it 'supports response postprocessing' do
47
+ test_rit do
48
+ e1 = div 'foo'
49
+ e2 = span 'bar'
50
+
51
+ ~ (e1 + e2)
52
+ end.should == "<div>foo</div><span>bar</span>"
53
+ end
54
+
55
+ it 'properly handles nested blocks' do
56
+ locals = { :list => [ '1', '2', '3', '4' ] }
57
+ test_rit(locals) do
58
+ ~ ul do
59
+ list.each do |item|
60
+ ~ li(item)
61
+ end
62
+ end
63
+ end.should == '<ul><li>1</li><li>2</li><li>3</li><li>4</li></ul>'
64
+ end
65
+
66
+ it 'implements t helper' do
67
+ test_rit do
68
+ ~ t('<br />')
69
+ end.should == "&lt;br /&gt;"
70
+ end
71
+
72
+ it 'implements h helper' do
73
+ test_rit do
74
+ ~ h('<br />')
75
+ end.should == "<br />"
76
+ end
77
+
78
+ it 'passes instance variables' do
79
+ test_rit do
80
+ ~ t(@virtual_path)
81
+ end.should == "(inline)"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,39 @@
1
+ require 'action_controller'
2
+ require 'inline_templates'
3
+
4
+ def invoke_controller(controller, action)
5
+ status, headers, content = controller.action(action).call({
6
+ "rack.input" => StringIO.new,
7
+ "REQUEST_METHOD" => "GET"
8
+ })
9
+
10
+ content.body
11
+ end
12
+
13
+ def test_rit(locals = {}, &template)
14
+ inner_context = nil
15
+ assigns = {}
16
+ controller = nil
17
+ formats = nil
18
+
19
+ view = ActionView::Base.new inner_context, assigns, controller, formats
20
+
21
+ InlineTemplates.render view, { virtual_path: "(inline)" }, locals, &template
22
+ end
23
+
24
+ class TestController < ActionController::Base
25
+ ReferenceOutput = "<div>test</div>"
26
+
27
+ include InlineTemplates::Helpers
28
+
29
+ def test
30
+ self.response_body = rit do
31
+ ~ div("test")
32
+ end
33
+ end
34
+
35
+ def test_file
36
+ render :file => File.expand_path("../support/test.rit", __FILE__)
37
+ end
38
+ end
39
+
@@ -0,0 +1 @@
1
+ ~ div("test")
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inline_templates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Gridasov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-19 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ description: Inline templates for Rails controllers with Arbre-like DSL
70
+ email:
71
+ - grindars@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - inline_views.gemspec
82
+ - lib/inline_templates.rb
83
+ - lib/inline_templates/blank_object.rb
84
+ - lib/inline_templates/buffer_wrapper.rb
85
+ - lib/inline_templates/builder.rb
86
+ - lib/inline_templates/helpers.rb
87
+ - lib/inline_templates/rendering_context.rb
88
+ - lib/inline_templates/template_handler.rb
89
+ - lib/inline_templates/version.rb
90
+ - spec/inline_templates/helpers_spec.rb
91
+ - spec/inline_templates/template_handler_spec.rb
92
+ - spec/inline_templates_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/test.rit
95
+ homepage: https://github.com/grindars/inline_templates
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Inline templates for Rails
119
+ test_files:
120
+ - spec/inline_templates/helpers_spec.rb
121
+ - spec/inline_templates/template_handler_spec.rb
122
+ - spec/inline_templates_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/test.rit