smartgen_action_view 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .rvmrc
5
+ doc
6
+ spec/sandbox/**/*
7
+ .project
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format documentation
3
+ --debug
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ platforms :mri_18 do
6
+ gem "ruby-debug"
7
+ end
8
+
9
+ platforms :mri_19 do
10
+ gem "ruby-debug19" if RUBY_VERSION < "1.9.3"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,87 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smartgen_action_view (0.0.1)
5
+ actionpack (>= 3.0.4)
6
+ smartgen (~> 0.6.0.beta)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ RedCloth (4.2.7)
12
+ abstract (1.0.0)
13
+ actionpack (3.0.4)
14
+ activemodel (= 3.0.4)
15
+ activesupport (= 3.0.4)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.13)
21
+ rack-test (~> 0.5.7)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.4)
24
+ activesupport (= 3.0.4)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4)
27
+ activesupport (3.0.4)
28
+ archive-tar-minitar (0.5.2)
29
+ bluecloth (2.0.11)
30
+ builder (2.1.2)
31
+ columnize (0.3.2)
32
+ diff-lcs (1.1.2)
33
+ directory_watcher (1.3.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ i18n (0.5.0)
37
+ linecache (0.43)
38
+ linecache19 (0.5.12)
39
+ ruby_core_source (>= 0.1.4)
40
+ nokogiri (1.4.4)
41
+ rack (1.2.1)
42
+ rack-mount (0.6.13)
43
+ rack (>= 1.0.0)
44
+ rack-test (0.5.7)
45
+ rack (>= 1.0)
46
+ rspec (2.5.0)
47
+ rspec-core (~> 2.5.0)
48
+ rspec-expectations (~> 2.5.0)
49
+ rspec-mocks (~> 2.5.0)
50
+ rspec-core (2.5.1)
51
+ rspec-expectations (2.5.0)
52
+ diff-lcs (~> 1.1.2)
53
+ rspec-mocks (2.5.0)
54
+ ruby-debug (0.10.4)
55
+ columnize (>= 0.1)
56
+ ruby-debug-base (~> 0.10.4.0)
57
+ ruby-debug-base (0.10.4)
58
+ linecache (>= 0.3)
59
+ ruby-debug-base19 (0.11.25)
60
+ columnize (>= 0.3.1)
61
+ linecache19 (>= 0.5.11)
62
+ ruby_core_source (>= 0.1.4)
63
+ ruby-debug19 (0.11.6)
64
+ columnize (>= 0.3.1)
65
+ linecache19 (>= 0.5.11)
66
+ ruby-debug-base19 (>= 0.11.19)
67
+ ruby_core_source (0.1.5)
68
+ archive-tar-minitar (>= 0.5.2)
69
+ smartgen (0.6.0.beta2)
70
+ RedCloth (>= 4.2.7)
71
+ activesupport (>= 2.3.5)
72
+ bluecloth (>= 2.0.9)
73
+ directory_watcher (>= 1.3.2)
74
+ i18n (>= 0.5.0)
75
+ nokogiri (>= 1.4.4)
76
+ thor (>= 0.14.6)
77
+ thor (0.14.6)
78
+ tzinfo (0.3.24)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ rspec (>= 2.3.0)
85
+ ruby-debug
86
+ ruby-debug19
87
+ smartgen_action_view!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Vicente Mundim
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ Smartgen ActionView
2
+ ===================
3
+
4
+ What if you could use all things you are used to when working with rails in your smartgen files? This gem is what you are looking for then. It combines the power of ActionView, like using ERB templates, rendering partials, using helpers, and many other stuff with Smartgen.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+
6
+ desc "Run all examples"
7
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,11 @@
1
+ require 'action_controller'
2
+ require 'action_view'
3
+
4
+ require 'smartgen'
5
+
6
+ require File.expand_path(File.join('smartgen_action_view/renderer'), File.dirname(__FILE__))
7
+ require File.expand_path(File.join('smartgen_action_view/pre_processor'), File.dirname(__FILE__))
8
+
9
+ Smartgen::Engine::Textile.register(Smartgen::Engine::ActionViewPreProcessor.new)
10
+ Smartgen::Engine::Markdown.register(Smartgen::Engine::ActionViewPreProcessor.new)
11
+ Smartgen::Engine::ERB.register(Smartgen::Engine::ActionViewPreProcessor.new)
@@ -0,0 +1,41 @@
1
+ require 'tempfile'
2
+
3
+ module Smartgen
4
+ module Engine
5
+ class ActionViewPreProcessor
6
+ attr_accessor :engine
7
+ attr_accessor :tempfile
8
+
9
+ def process(body, metadata)
10
+ create_tempfile_with(body)
11
+ view = ::ActionView::Base.new File.dirname(tempfile.path)
12
+ result = view.render :file => tempfile.path
13
+ setup_content_for(view, metadata)
14
+ result
15
+ ensure
16
+ tempfile.close! if tempfile
17
+ end
18
+
19
+ private
20
+
21
+ def create_tempfile_with(body)
22
+ self.tempfile = Tempfile.new 'smartgen'
23
+ tempfile.write body
24
+ tempfile.rewind
25
+ end
26
+
27
+ def setup_content_for(view, metadata)
28
+ metadata.content_for = Smartgen::ObjectHash.new unless metadata.has_key?(:content_for)
29
+ metadata.content_for.merge!(process_content_for(view, metadata))
30
+ end
31
+
32
+ def process_content_for(view, metadata)
33
+ view.instance_variable_get('@_content_for').inject({}) do |result, item|
34
+ name, contents = item
35
+ result[name] = engine.process_without_pre_processors(contents, metadata).html_safe
36
+ result
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ module Smartgen
2
+ module Renderer
3
+ # A renderer that uses ActionView to render markup files.
4
+ class ActionView
5
+ module Helper
6
+ def markup_file
7
+ @markup_file
8
+ end
9
+
10
+ def markup_file=(markup_file)
11
+ @markup_file = markup_file
12
+ end
13
+
14
+ def metadata
15
+ @metadata
16
+ end
17
+
18
+ def metadata=(metadata)
19
+ @metadata = metadata
20
+ end
21
+ end
22
+
23
+ # Renders the markup file using the given layout.
24
+ #
25
+ # It exposes +markup_file+ variable and its +metadata+ to the ActionView layout.
26
+ def render(layout, markup_file, metadata=Smartgen::ObjectHash.new)
27
+ view = ::ActionView::Base.new(source_dir(markup_file))
28
+
29
+ view.extend(Helper)
30
+ view.markup_file = markup_file
31
+ view.metadata = metadata
32
+
33
+ setup_content_for_with(view)
34
+
35
+ view.render(:layout => layout, :text => markup_file.contents)
36
+ end
37
+
38
+ private
39
+
40
+ def source_dir(markup_file)
41
+ File.dirname(markup_file.path)
42
+ end
43
+
44
+ def setup_content_for_with(view)
45
+ if view.metadata[:content_for]
46
+ view.metadata[:content_for].each do |name, contents|
47
+ view.content_for(name.to_sym) { contents }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module SmartgenActionView
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "smartgen_action_view/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "smartgen_action_view"
7
+ s.version = SmartgenActionView::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Vicente Mundim"]
10
+ s.email = ["vicente.mundim@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Use ActionView to render files in smartgen}
13
+ s.description = %q{This gem gives you all the power of ActionView when rendering files in smartgen, such as using partials, helpers, etc.}
14
+
15
+ s.rubyforge_project = "smartgen_action_view"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency "smartgen", "~> 0.6.0.beta"
24
+ s.add_dependency "actionpack", ">= 3.0.4"
25
+
26
+ s.add_development_dependency "rspec", ">= 2.3.0"
27
+ end
@@ -0,0 +1,3 @@
1
+ <h3>Here is some partial</h3>
2
+
3
+ <p>With some content</p>
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1>Here is some header</h1>
4
+ <p>And a paragraph</p>
5
+ </body>
6
+ </html>
@@ -0,0 +1,12 @@
1
+ <html>
2
+ <body>
3
+ <section class="header">
4
+
5
+ <h1>Here is some header</h1>
6
+ <p>And a paragraph</p>
7
+ </section>
8
+
9
+ <h2>Here is a smaller header</h2>
10
+ <p>And another paragraph</p>
11
+ </body>
12
+ </html>
@@ -0,0 +1,7 @@
1
+ <html>
2
+ <body>
3
+ <h1>Here is some header</h1>
4
+ <p>And a paragraph</p>
5
+ <p>index</p>
6
+ </body>
7
+ </html>
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>Some Title</title>
4
+ </head>
5
+ <body>
6
+ <h1>Here is some header</h1>
7
+ <p>And a paragraph</p>
8
+ </body>
9
+ </html>
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <body>
3
+ <h1>Here is some header</h1>
4
+ <p>And a paragraph</p>
5
+ <h3>Here is some partial</h3>
6
+
7
+ <p>With some content</p>
8
+ </body>
9
+ </html>
@@ -0,0 +1,3 @@
1
+ h1. Here is some header
2
+
3
+ And a paragraph
@@ -0,0 +1,10 @@
1
+ <% content_for :header do %>
2
+ h1. Here is some header
3
+
4
+ And a paragraph
5
+
6
+ <% end %>
7
+
8
+ h2. Here is a smaller header
9
+
10
+ And another paragraph
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <%= yield %>
4
+ </body>
5
+ </html>
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <body>
3
+ <section class="header">
4
+ <%= yield :header %>
5
+ </section>
6
+
7
+ <%= yield %>
8
+ </body>
9
+ </html>
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <%= yield %>
4
+ <p><%= markup_file.filename %></p>
5
+ </body>
6
+ </html>
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <title><%= metadata.title %></title>
4
+ </head>
5
+ <body>
6
+ <%= yield %>
7
+ </body>
8
+ </html>
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <%= yield %>
4
+ <%= render :partial => 'partial' %>
5
+ </body>
6
+ </html>
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Smartgen::Engine::ActionViewPreProcessor do
5
+ let :metadata do
6
+ Smartgen::ObjectHash.new
7
+ end
8
+
9
+ before do
10
+ Smartgen::Engine::Textile.register(subject)
11
+ Smartgen::Engine::Textile.new # will set engine on subject
12
+ end
13
+
14
+ it "should allow the engine to be set" do
15
+ subject.should respond_to(:engine=)
16
+ end
17
+
18
+ it "should allow the engine to be retrieved" do
19
+ subject.should respond_to(:engine)
20
+ end
21
+
22
+ it "should process contents using action view" do
23
+ subject.process('<%= 1 + 1 %>', metadata).should == '2'
24
+ end
25
+
26
+ it "should strip out content_for contents from result" do
27
+ subject.process('<% content_for :some_section do %>*something*<% end %><p>some other content</p>', metadata).should == '<p>some other content</p>'
28
+ end
29
+
30
+ it "should add content_for data to metadata" do
31
+ subject.process('<% content_for :some_section do %>*something*<% end %><p>some other content</p>', metadata)
32
+ metadata[:content_for].should have_key(:some_section)
33
+ end
34
+
35
+ it "should process content in content_for data" do
36
+ subject.process('<% content_for :some_section do %>*something*<% end %><p>some other content</p>', metadata)
37
+ metadata[:content_for][:some_section].should == "<p><strong>something</strong></p>"
38
+ end
39
+
40
+ it "should return safe html content in content_for data" do
41
+ subject.process('<% content_for :some_section do %>*something*<% end %><p>some other content</p>', metadata)
42
+ metadata[:content_for][:some_section].should be_html_safe
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Smartgen::Renderer::ActionView do
5
+ matcher :be_equal_ignoring_identation do |expected|
6
+ match do |actual|
7
+ without_identation(actual) == without_identation(expected)
8
+ end
9
+
10
+ diffable
11
+
12
+ def without_identation(value)
13
+ value.split("\n").map(&:strip).reject(&:empty?).join("\n")
14
+ end
15
+ end
16
+
17
+ let :markup_file do
18
+ Smartgen::MarkupFile.new fixture('index.textile'), :metadata => metadata
19
+ end
20
+
21
+ let :metadata do
22
+ Smartgen::ObjectHash.new
23
+ end
24
+
25
+ it "should render file using layout, putting contents on 'yield' call in layout template" do
26
+ subject.render('layout', markup_file, metadata).should be_equal_ignoring_identation(expectation('index.html'))
27
+ end
28
+
29
+ it "should render file with markup_file in layout" do
30
+ subject.render('layout_with_markup_file', markup_file, metadata).should be_equal_ignoring_identation(expectation('index_with_markup_file.html'))
31
+ end
32
+
33
+ it "should render file with metadata in layout" do
34
+ metadata.title = 'Some Title'
35
+ subject.render('layout_with_metadata', markup_file, metadata).should be_equal_ignoring_identation(expectation('index_with_metadata.html'))
36
+ end
37
+
38
+ it "should render file with partials in layout" do
39
+ subject.render('layout_with_partial', markup_file, metadata).should be_equal_ignoring_identation(expectation('index_with_partial.html'))
40
+ end
41
+
42
+ it "should render file with content_for sections, putting then in layout appropriately" do
43
+ markup_file = Smartgen::MarkupFile.new fixture('index_with_content_for.textile'), :metadata => metadata
44
+ subject.render('layout_with_content_for', markup_file, metadata).should be_equal_ignoring_identation(expectation('index_with_content_for.html'))
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ require 'rspec'
4
+ require 'smartgen'
5
+ require 'smartgen_action_view'
6
+
7
+ RSpec.configure do |config|
8
+ def fixture(path)
9
+ File.join(fixtures_dir, path)
10
+ end
11
+
12
+ def fixtures_dir
13
+ File.expand_path('fixtures', File.dirname(__FILE__))
14
+ end
15
+
16
+ def expectation(path)
17
+ File.read(fixture(File.join('expectations', path)))
18
+ end
19
+
20
+ def capture(stream)
21
+ begin
22
+ stream = stream.to_s
23
+ eval "$#{stream} = StringIO.new"
24
+ yield
25
+ result = eval("$#{stream}").string
26
+ ensure
27
+ eval("$#{stream} = #{stream.upcase}")
28
+ end
29
+
30
+ result
31
+ end
32
+
33
+ alias silence capture
34
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smartgen_action_view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vicente Mundim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-25 00:00:00.000000000 -03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: smartgen
17
+ requirement: &2161521780 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.0.beta
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2161521780
26
+ - !ruby/object:Gem::Dependency
27
+ name: actionpack
28
+ requirement: &2161521280 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2161521280
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2161520820 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2161520820
48
+ description: This gem gives you all the power of ActionView when rendering files in
49
+ smartgen, such as using partials, helpers, etc.
50
+ email:
51
+ - vicente.mundim@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - .rspec
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/smartgen_action_view.rb
64
+ - lib/smartgen_action_view/pre_processor.rb
65
+ - lib/smartgen_action_view/renderer.rb
66
+ - lib/smartgen_action_view/version.rb
67
+ - smartgen_action_view.gemspec
68
+ - spec/fixtures/_partial.html.erb
69
+ - spec/fixtures/expectations/index.html
70
+ - spec/fixtures/expectations/index_with_content_for.html
71
+ - spec/fixtures/expectations/index_with_markup_file.html
72
+ - spec/fixtures/expectations/index_with_metadata.html
73
+ - spec/fixtures/expectations/index_with_partial.html
74
+ - spec/fixtures/index.textile
75
+ - spec/fixtures/index_with_content_for.textile
76
+ - spec/fixtures/layout.html.erb
77
+ - spec/fixtures/layout_with_content_for.html.erb
78
+ - spec/fixtures/layout_with_markup_file.html.erb
79
+ - spec/fixtures/layout_with_metadata.html.erb
80
+ - spec/fixtures/layout_with_partial.html.erb
81
+ - spec/lib/pre_processor_spec.rb
82
+ - spec/lib/renderer_spec.rb
83
+ - spec/spec_helper.rb
84
+ has_rdoc: true
85
+ homepage: ''
86
+ licenses: []
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project: smartgen_action_view
105
+ rubygems_version: 1.6.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Use ActionView to render files in smartgen
109
+ test_files:
110
+ - spec/fixtures/_partial.html.erb
111
+ - spec/fixtures/expectations/index.html
112
+ - spec/fixtures/expectations/index_with_content_for.html
113
+ - spec/fixtures/expectations/index_with_markup_file.html
114
+ - spec/fixtures/expectations/index_with_metadata.html
115
+ - spec/fixtures/expectations/index_with_partial.html
116
+ - spec/fixtures/index.textile
117
+ - spec/fixtures/index_with_content_for.textile
118
+ - spec/fixtures/layout.html.erb
119
+ - spec/fixtures/layout_with_content_for.html.erb
120
+ - spec/fixtures/layout_with_markup_file.html.erb
121
+ - spec/fixtures/layout_with_metadata.html.erb
122
+ - spec/fixtures/layout_with_partial.html.erb
123
+ - spec/lib/pre_processor_spec.rb
124
+ - spec/lib/renderer_spec.rb
125
+ - spec/spec_helper.rb