markababy 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -21,6 +21,14 @@ Usage is similar to Markaby, and easy to use directly from a Ruby script:
21
21
  Use Markababy.capture if you want to capture the output as a string instead
22
22
  of printing it to $stdout.
23
23
 
24
+ To use Markababy within a Rails 3 app, first add it to your Gemfile:
25
+
26
+ gem 'markababy'
27
+
28
+ Then change the extension on your template files from .erb to .rb, and you
29
+ can start writing your templates in Ruby! Controller instance variables and
30
+ helpers will be available as methods.
31
+
24
32
  Some differences from Markaby:
25
33
 
26
34
  * No auto-stringification
@@ -0,0 +1,51 @@
1
+ module Markababy
2
+ class Builder < BasicObject
3
+ def initialize(options, &block)
4
+ @options = options
5
+
6
+ @output = @options[:output]
7
+
8
+ @escape = @options[:escape]
9
+
10
+ @context = @options[:context]
11
+
12
+ instance_eval(&block)
13
+ end
14
+
15
+ def method_missing(sym, *args, &block)
16
+ if !@context.nil? && @context.respond_to?(sym)
17
+ return @context.send(sym, *args, &block)
18
+ end
19
+
20
+ attributes, content = [], []
21
+
22
+ args.flatten.each do |arg|
23
+ if arg.respond_to?(:to_hash)
24
+ arg.to_hash.each { |k, v| attributes << ' %s="%s"' % [@escape[k.to_s], @escape[v.to_s]] }
25
+ elsif arg.respond_to?(:id2name)
26
+ attributes << ' %s' % @escape[arg.to_s]
27
+ elsif arg.respond_to?(:html_safe?) && arg.html_safe?
28
+ content << arg.to_s
29
+ else
30
+ content << @escape[arg.to_s]
31
+ end
32
+ end
33
+
34
+ @output << (attributes.empty? ? "<#{sym}>" : "<#{sym}#{attributes.join}>")
35
+
36
+ @output << content.join unless content.empty?
37
+
38
+ instance_eval(&block) unless block.nil?
39
+
40
+ @output << "</#{sym}>" unless content.empty? && block.nil?
41
+ end
42
+
43
+ def text(content)
44
+ if content.respond_to?(:html_safe?) && content.html_safe?
45
+ @output << content.to_s
46
+ else
47
+ @output << @escape[content.to_s]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,3 @@
1
- require 'markababy'
2
- require 'action_view'
3
-
4
1
  module Markababy
5
2
  class RailsTemplateContext
6
3
  def initialize(controller)
@@ -23,19 +20,4 @@ module Markababy
23
20
  end
24
21
  end
25
22
  end
26
-
27
- module RailsTemplateHandler
28
- def self.call(template)
29
- "self.output_buffer = ''\n" +
30
- "Markababy.capture(output: self.output_buffer, context: Markababy::RailsTemplateContext.new(self)) do\n" +
31
- "#{template.source}\n" +
32
- "end\n"
33
- end
34
-
35
- def self.extended(base)
36
- base.register_default_template_handler :rb, self
37
- end
38
- end
39
-
40
- ActionView::Template.extend RailsTemplateHandler
41
23
  end
@@ -0,0 +1,10 @@
1
+ module Markababy
2
+ module RailsTemplateHandler
3
+ def self.call(template)
4
+ "self.output_buffer = ''\n" +
5
+ "Markababy.capture(output: self.output_buffer, context: Markababy::RailsTemplateContext.new(self)) do\n" +
6
+ "#{template.source}\n" +
7
+ "end\n"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Markababy
2
+ class Railtie < Rails::Railtie
3
+ initializer 'markababy.action_view' do |app|
4
+ ActiveSupport.on_load(:action_view) do
5
+ ActionView::Template.register_default_template_handler :rb, RailsTemplateHandler
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/markababy.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'markababy/builder'
1
2
  require 'cgi'
2
3
 
3
4
  module Markababy
@@ -10,58 +11,18 @@ module Markababy
10
11
 
11
12
  options[:output] = $stdout unless options.has_key?(:output)
12
13
 
13
- options[:output] << "<!DOCTYPE html>\n" if options[:doctype]
14
+ options[:output] << doctype if options[:doctype]
14
15
 
15
16
  Builder.new(options, &block)
16
17
  end
17
18
 
18
- class Builder < BasicObject
19
- def initialize(options, &block)
20
- @options = options
21
-
22
- @output = @options[:output]
23
-
24
- @escape = @options[:escape]
25
-
26
- @context = @options[:context]
27
-
28
- instance_eval(&block)
29
- end
30
-
31
- def method_missing(sym, *args, &block)
32
- if !@context.nil? && @context.respond_to?(sym)
33
- return @context.send(sym, *args, &block)
34
- end
35
-
36
- attributes, content = [], []
37
-
38
- args.flatten.each do |arg|
39
- if arg.respond_to?(:to_hash)
40
- arg.to_hash.each { |k, v| attributes << ' %s="%s"' % [@escape[k.to_s], @escape[v.to_s]] }
41
- elsif arg.respond_to?(:id2name)
42
- attributes << ' %s' % @escape[arg.to_s]
43
- elsif arg.respond_to?(:html_safe?) && arg.html_safe?
44
- content << arg.to_s
45
- else
46
- content << @escape[arg.to_s]
47
- end
48
- end
49
-
50
- @output << (attributes.empty? ? "<#{sym}>" : "<#{sym}#{attributes.join}>")
51
-
52
- @output << content.join unless content.empty?
53
-
54
- instance_eval(&block) unless block.nil?
55
-
56
- @output << "</#{sym}>" unless content.empty? && block.nil?
57
- end
19
+ def self.doctype
20
+ @doctype ||= "<!DOCTYPE html>\n".freeze
21
+ end
58
22
 
59
- def text(content)
60
- if content.respond_to?(:html_safe?) && content.html_safe?
61
- @output << content.to_s
62
- else
63
- @output << @escape[content.to_s]
64
- end
65
- end
23
+ if defined?(Rails)
24
+ require 'markababy/rails_template_context'
25
+ require 'markababy/rails_template_handler'
26
+ require 'markababy/railtie'
66
27
  end
67
28
  end
data/markababy.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'markababy'
3
- s.version = '1.0.0'
3
+ s.version = '1.1.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -8,7 +8,6 @@ Gem::Specification.new do |s|
8
8
  s.description = 'Markaby\'s little sister'
9
9
  s.summary = 'See description'
10
10
  s.files = Dir.glob('{lib,spec}/**/*') + %w(README.txt Rakefile markababy.gemspec)
11
- s.add_development_dependency('activesupport', ['>= 3.0.3'])
12
- s.add_development_dependency('actionpack', ['>= 3.0.3'])
11
+ s.add_development_dependency('rails', ['>= 3.0.3'])
13
12
  s.require_path = 'lib'
14
13
  end
@@ -1,7 +1,12 @@
1
1
  require 'minitest/autorun'
2
- require 'abstract_controller'
2
+ require 'rails/all'
3
3
  require 'markababy'
4
- require 'markababy/rails'
4
+
5
+ class DummyApplication < Rails::Application
6
+ config.active_support.deprecation = :log
7
+ end
8
+
9
+ DummyApplication.initialize!
5
10
 
6
11
  class DummyController < AbstractController::Base
7
12
  # cf. http://amberbit.com/blog/render-views-partials-outside-controllers-rails-3
@@ -21,7 +26,7 @@ end
21
26
 
22
27
  describe DummyController do
23
28
  it 'should return the correct markup' do
24
- output = '<html><head><title>Controller says hello!</title><p>12,345,678</p></head></html>'
29
+ output = '<html><head><title>Controller says hello!</title></head><body><p>12,345,678</p></body></html>'
25
30
 
26
31
  DummyController.new.index.must_equal output
27
32
  end
@@ -1,7 +1,8 @@
1
1
  html {
2
2
  head {
3
3
  title message
4
-
4
+ }
5
+ body {
5
6
  p number_with_delimiter(12345678)
6
7
  }
7
8
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markababy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-11 00:00:00.000000000Z
12
+ date: 2011-10-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activesupport
16
- requirement: &10149360 !ruby/object:Gem::Requirement
15
+ name: rails
16
+ requirement: &4155440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,7 @@ dependencies:
21
21
  version: 3.0.3
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *10149360
25
- - !ruby/object:Gem::Dependency
26
- name: actionpack
27
- requirement: &10149030 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: 3.0.3
33
- type: :development
34
- prerelease: false
35
- version_requirements: *10149030
24
+ version_requirements: *4155440
36
25
  description: Markaby's little sister
37
26
  email:
38
27
  - mail@timcraft.com
@@ -40,7 +29,10 @@ executables: []
40
29
  extensions: []
41
30
  extra_rdoc_files: []
42
31
  files:
43
- - lib/markababy/rails.rb
32
+ - lib/markababy/builder.rb
33
+ - lib/markababy/rails_template_context.rb
34
+ - lib/markababy/rails_template_handler.rb
35
+ - lib/markababy/railtie.rb
44
36
  - lib/markababy.rb
45
37
  - spec/markababy_rails_spec.rb
46
38
  - spec/markababy_spec.rb