Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first pass at Sinatra::Erector

  • Loading branch information...
commit bddbbf843276afec424746cd0f8e56c302df6a93 1 parent 654e417
@alexch authored
View
7 CHANGES
@@ -1,7 +0,0 @@
-= 0.9.2.2
-
-* layout support is tested
-
-= 0.9.2.1
-
-* use Rack::Test instead of Sinatra::Test
View
9 LICENSE
@@ -1,4 +1,11 @@
-Copyright (c) 2009 unwwwired.net
+
+based on Sinatra::Markaby by Brent
+Author:: S. Brent Faulkner <brentf@unwwwired.net>
+License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
+
+
+Author:: Alex Chaffee <alex@stinky.com>
+License:: Copyright (c) 2009 Alex Chaffee, released under the MIT license
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
15 README.rdoc
@@ -1,26 +1,27 @@
-= sinatra-markaby
+= sinatra-erector
-sinatra-markaby is an extension for sinatra to enable rendering of html files
-using markaby templates.
+sinatra-erector is an extension for sinatra to enable rendering of html files
+using erector templates.
== Installation
- sudo gem install sbfaulkner-sinatra-markaby -s http://gems.github.com
+# TODO: make it part of Erector
+ sudo gem install sbfaulkner-sinatra-erector -s http://gems.github.com
== Example
require 'rubygems'
require 'sinatra'
- require 'sinatra/markaby'
+ require 'sinatra/erector'
get '/' do
- markaby :template
+ erector :template
end
__END__
@@ template
- mab.html do
+ html do
head { title "Hello world" }
body do
p "Hello world!!!!!"
View
16 Rakefile
@@ -3,13 +3,13 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |s|
- s.name = "sinatra-markaby"
- s.summary = %Q{Sinatra plugin to enable markaby (.mab) template rendering.}
- s.email = "brentf@unwwwired.net"
- s.homepage = "http://github.com/sbfaulkner/sinatra-markaby"
- s.description = "Sinatra plugin to enable markaby (.mab) template rendering."
- s.authors = ["S. Brent Faulkner"]
- s.add_dependency "markaby"
+ s.name = "sinatra-erector"
+ s.summary = %Q{Sinatra plugin to enable erector (.erector) template rendering.}
+ s.email = "alex@stinky.com"
+ s.homepage = "http://github.com/alexch/sinatra-erector"
+ s.description = "Sinatra plugin to enable erector (.erector) template rendering."
+ s.authors = ["Alex Chaffee"]
+ s.add_dependency "erector"
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
@@ -18,7 +18,7 @@ end
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'sinatra-markaby'
+ rdoc.title = 'sinatra-erector'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
View
2  TODO
@@ -1,3 +1,3 @@
= TODO
-* refactor render/views in sinatra
+* set :erector
View
4 VERSION.yml
@@ -1,4 +1,4 @@
---
:major: 0
-:minor: 9
-:patch: 2.2
+:minor: 1
+:patch: 0
View
44 lib/sinatra/erector.rb
@@ -0,0 +1,44 @@
+require 'sinatra/base'
+require 'erector'
+require 'activesupport'
+
+module Sinatra
+ module Erector
+
+ include ActiveSupport::CoreExtensions::String::Inflections
+
+ # Generate html file using Erector.
+ # Takes the name of a template to render as a Symbol and returns a String
+ # with the rendered output.
+ def erector(template=nil, options={}, locals = {}, &block)
+ options, template = template, nil if template.is_a?(Hash)
+ template = lambda { block } if template.nil?
+ render :erector, template, options, locals
+ end
+
+ protected
+ def render_erector(template, data, options, locals, &block)
+ # puts "---"
+ # puts "template=#{template}"
+ # puts "data='#{data}'"
+ # puts "options.inspect='#{options.inspect}'"
+ # puts "locals='#{locals.inspect}'"
+ # puts "block='#{block}'"
+
+ filename = options.delete(:filename) || '<ERECTOR>'
+ line = options.delete(:line) || 1
+
+ if data.respond_to?(:to_str)
+ widget = ::Erector::Widget.new(locals) do
+ eval(data.to_str, binding, filename, line)
+ end
+ # end
+ elsif data.kind_of?(Proc)
+ widget = ::Erector::Widget.new(locals, &data)
+ end
+ widget.to_s
+ end
+ end
+
+ helpers Erector
+end
View
31 lib/sinatra/markaby.rb
@@ -1,31 +0,0 @@
-require 'sinatra/base'
-require 'markaby'
-
-module Sinatra
- module Markaby
- # Generate html file using Markaby.
- # Takes the name of a template to render as a Symbol and returns a String with the rendered output.
- #
- # Options for markaby may be specified in Sinatra using set :markaby, { ... }
- # TODO: the options aren't actually used yet
- def markaby(template=nil, options={}, locals = {}, &block)
- options, template = template, nil if template.is_a?(Hash)
- template = lambda { block } if template.nil?
- render :mab, template, options, locals
- end
-
- protected
- def render_mab(template, data, options, locals, &block)
- filename = options.delete(:filename) || '<MARKABY>'
- line = options.delete(:line) || 1
- mab = ::Markaby::Builder.new(locals)
- if data.respond_to?(:to_str)
- eval(data.to_str, binding, filename, line)
- elsif data.kind_of?(Proc)
- data.call(mab)
- end
- end
- end
-
- helpers Markaby
-end
View
49 sinatra-erector.gemspec
@@ -0,0 +1,49 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{sinatra-erector}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Alex Chaffee"]
+ s.date = %q{2009-05-20}
+ s.description = %q{Sinatra plugin to enable erector (.erector) template rendering.}
+ s.email = %q{alex@stinky.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION.yml",
+ "lib/sinatra/erector.rb",
+ "test/sinatra_erector_test.rb",
+ "test/test_helper.rb",
+ "test/views/hello.erector",
+ "test/views/html.erector"
+ ]
+ s.homepage = %q{http://github.com/alexch/sinatra-erector}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.3}
+ s.summary = %q{Sinatra plugin to enable erector (.erector) template rendering.}
+ s.test_files = [
+ "test/sinatra_erector_test.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<erector>, [">= 0"])
+ else
+ s.add_dependency(%q<erector>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<erector>, [">= 0"])
+ end
+end
View
49 sinatra-markaby.gemspec
@@ -1,49 +0,0 @@
-# -*- encoding: utf-8 -*-
-
-Gem::Specification.new do |s|
- s.name = %q{sinatra-markaby}
- s.version = "0.9.2.2"
-
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["S. Brent Faulkner"]
- s.date = %q{2009-04-29}
- s.description = %q{Sinatra plugin to enable markaby (.mab) template rendering.}
- s.email = %q{brentf@unwwwired.net}
- s.extra_rdoc_files = [
- "LICENSE",
- "README.rdoc"
- ]
- s.files = [
- "LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION.yml",
- "lib/sinatra/markaby.rb",
- "test/sinatra_markaby_test.rb",
- "test/test_helper.rb",
- "test/views/hello.mab"
- ]
- s.has_rdoc = true
- s.homepage = %q{http://github.com/sbfaulkner/sinatra-markaby}
- s.rdoc_options = ["--charset=UTF-8"]
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.1}
- s.summary = %q{Sinatra plugin to enable markaby (.mab) template rendering.}
- s.test_files = [
- "test/sinatra_markaby_test.rb",
- "test/test_helper.rb"
- ]
-
- if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
- s.specification_version = 2
-
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<markaby>, [">= 0"])
- else
- s.add_dependency(%q<markaby>, [">= 0"])
- end
- else
- s.add_dependency(%q<markaby>, [">= 0"])
- end
-end
View
49 test/sinatra_markaby_test.rb → test/sinatra_erector_test.rb
@@ -1,10 +1,10 @@
require File.dirname(__FILE__) + '/test_helper'
-class SinatraMarkabyTest < Test::Unit::TestCase
- def markaby_app(&block)
+class SinatraErectorTest < Test::Unit::TestCase
+ def erector_app(&block)
mock_app {
use_in_file_templates!
- helpers Sinatra::Markaby
+ helpers Sinatra::Erector
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
@@ -12,55 +12,52 @@ def markaby_app(&block)
end
def test_renders_inline_strings
- markaby_app { markaby 'mab.p "Hello shrimp!"' }
+ erector_app { erector 'p "Hello shrimp!"' }
assert last_response.ok?
- assert_equal '<p>Hello shrimp!</p>', last_response.body
+ assert_equal '<p>Hello shrimp!</p>', last_response.body.join
end
def test_renders_inline_blocks
- markaby_app {
- @name = 'Frank & Mary'
- markaby do |mab|
- mab.p "Hello #{@name}!"
+ erector_app {
+ name = 'Frank & Mary'
+ erector do
+ p "Hello #{name}!"
end
}
assert last_response.ok?
- assert_equal '<p>Hello Frank &amp; Mary!</p>', last_response.body
+ assert_equal '<p>Hello Frank &amp; Mary!</p>', last_response.body.join
end
- def test_renders_markaby_files_in_views_path
- markaby_app {
- @name = 'World'
- markaby :hello
+ def test_renders_erector_files_in_views_path
+ erector_app {
+ erector :hello, :locals => {:name => "World"}
}
assert last_response.ok?
- assert_equal '<p>Hello, World!</p>', last_response.body
+ assert_equal '<p>Hello, World!</p>', last_response.body.join
end
def test_renders_in_file_template
- markaby_app {
- @name = 'Joe'
- markaby :in_file
+ erector_app {
+ erector :in_file, :locals => {:name => "Joe"}
}
assert last_response.ok?
- assert_equal '<p>Hey Joe</p>', last_response.body
+ assert_equal '<p>Hey Joe</p>', last_response.body.join
end
def test_renders_with_layout
- markaby_app {
- @name = 'with Layout'
- markaby :hello, :layout => :html
+ erector_app {
+ erector :hello, :layout => :html, :locals => { :name => 'with Layout'}
}
assert last_response.ok?
- assert_equal '<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/><title>Hello</title></head><body><p>Hello, with Layout!</p></body></html>', last_response.body
+ assert_equal '<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/><title>Hello</title></head><body><p>Hello, with Layout!</p></body></html>', last_response.body.join
end
def test_raises_error_if_template_not_found
mock_app {
- helpers Sinatra::Markaby
+ helpers Sinatra::Erector
set :environment, :test
set :raise_errors, true
- get('/') { markaby :no_such_template }
+ get('/') { erector :no_such_template }
}
assert_raises(Errno::ENOENT) { get('/') }
end
@@ -69,4 +66,4 @@ def test_raises_error_if_template_not_found
__END__
@@ in_file
-mab.p "Hey #{@name}"
+p "Hey #{@name}"
View
2  test/test_helper.rb
@@ -3,7 +3,7 @@
require 'rack/test'
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'sinatra/markaby'
+require 'sinatra/erector'
class Test::Unit::TestCase
include Rack::Test::Methods
View
1  test/views/hello.erector
@@ -0,0 +1 @@
+p "Hello, #{@name}!"
View
6 test/views/html.erector
@@ -0,0 +1,6 @@
+puts "zomg"
+puts "block=#{@block}"
+html do
+ head { title "Hello" }
+ body { yield }
+end
Please sign in to comment.
Something went wrong with that request. Please try again.