Skip to content

Commit

Permalink
first pass at Sinatra::Erector
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed May 21, 2009
1 parent 654e417 commit bddbbf8
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 133 deletions.
7 changes: 0 additions & 7 deletions CHANGES
@@ -1,7 +0,0 @@
= 0.9.2.2

* layout support is tested

= 0.9.2.1

* use Rack::Test instead of Sinatra::Test
9 changes: 8 additions & 1 deletion 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
Expand Down
15 changes: 8 additions & 7 deletions 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!!!!!"
Expand Down
16 changes: 8 additions & 8 deletions Rakefile
Expand Up @@ -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"
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion TODO
@@ -1,3 +1,3 @@
= TODO

* refactor render/views in sinatra
* set :erector
4 changes: 2 additions & 2 deletions VERSION.yml
@@ -1,4 +1,4 @@
---
:major: 0
:minor: 9
:patch: 2.2
:minor: 1
:patch: 0
44 changes: 44 additions & 0 deletions 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
31 changes: 0 additions & 31 deletions lib/sinatra/markaby.rb

This file was deleted.

49 changes: 49 additions & 0 deletions 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
49 changes: 0 additions & 49 deletions sinatra-markaby.gemspec

This file was deleted.

49 changes: 23 additions & 26 deletions test/sinatra_markaby_test.rb → test/sinatra_erector_test.rb
@@ -1,66 +1,63 @@
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
}
get '/'
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
Expand All @@ -69,4 +66,4 @@ def test_raises_error_if_template_not_found
__END__

@@ in_file
mab.p "Hey #{@name}"
p "Hey #{@name}"
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/views/hello.erector
@@ -0,0 +1 @@
p "Hello, #{@name}!"
6 changes: 6 additions & 0 deletions test/views/html.erector
@@ -0,0 +1,6 @@
puts "zomg"
puts "block=#{@block}"
html do
head { title "Hello" }
body { yield }
end

0 comments on commit bddbbf8

Please sign in to comment.