Skip to content

Commit

Permalink
Hey, let's add .erb support back.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobmcwhirter committed May 3, 2012
1 parent 53e4ea1 commit 16d8d58
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/awestruct/handler_chains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'awestruct/handlers/markdown_handler'
require 'awestruct/handlers/orgmode_handler'
require 'awestruct/handlers/textile_handler'
require 'awestruct/handlers/erb_handler'
require 'awestruct/handlers/haml_handler'
require 'awestruct/handlers/sass_handler'
require 'awestruct/handlers/scss_handler'
Expand All @@ -15,7 +16,6 @@ module Awestruct

class HandlerChains


DEFAULTS = [
HandlerChain.new( /\.md$/,
Awestruct::Handlers::FileHandler,
Expand All @@ -31,6 +31,12 @@ class HandlerChains
Awestruct::Handlers::TextileHandler,
Awestruct::Handlers::LayoutHandler
),
HandlerChain.new( /\.erb$/,
Awestruct::Handlers::FileHandler,
Awestruct::Handlers::FrontMatterHandler,
Awestruct::Handlers::ErbHandler,
Awestruct::Handlers::LayoutHandler
),
HandlerChain.new( /\.org$/,
Awestruct::Handlers::FileHandler,
Awestruct::Handlers::FrontMatterHandler,
Expand Down
34 changes: 34 additions & 0 deletions lib/awestruct/handlers/erb_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

require 'awestruct/handlers/base_handler'

module Awestruct
module Handlers
class ErbHandler < BaseHandler

def initialize(site, delegate)
super( site, delegate )
end

def simple_name
File.basename( File.basename( relative_source_path, '.erb' ), output_extension )
end

def output_filename
File.basename( relative_source_path, '.erb' )
end

def output_extension
File.extname( output_filename )
end

def content_syntax
:erb
end

def rendered_content(context, with_layouts=true)
erb = ERB.new( delegate.rendered_content( context, with_layouts) )
erb.result( context.send( :binding ) )
end
end
end
end
5 changes: 5 additions & 0 deletions lib/awestruct/handlers/front_matter_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def raw_content
@raw_content
end

def rendered_content(context, with_layouts)
parse_parts()
@raw_content
end

def content_line_offset
parse_parts()
@content_line_offset
Expand Down
38 changes: 38 additions & 0 deletions spec/erb_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

require 'awestruct/handlers/file_handler'
require 'awestruct/handlers/erb_handler'

require 'hashery/open_cascade'

describe Awestruct::Handlers::ErbHandler do

before :all do
@site = OpenCascade.new :encoding=>false, :dir=>Pathname.new( File.dirname(__FILE__) + '/test-data/handlers' )
end

def handler_file(path)
"#{@site.dir}/#{path}"
end

def create_context
OpenCascade.new :site=>@site
end

it "should provide a simple name for the page" do
file_handler = Awestruct::Handlers::FileHandler.new( @site, handler_file( "erb-page.html.erb" ) )
erb_handler = Awestruct::Handlers::ErbHandler.new( @site, file_handler )

erb_handler.simple_name.should == 'erb-page'
end

it "should successfully render an ERB page" do
file_handler = Awestruct::Handlers::FileHandler.new( @site, handler_file( "erb-page.html.erb" ) )
erb_handler = Awestruct::Handlers::ErbHandler.new( @site, file_handler )

rendered = erb_handler.rendered_content( create_context )
rendered.should_not be_nil
rendered.should =~ %r(<h1>This is an ERB page</h1>)
rendered.should =~ %r(<h2>The fruit of the day is: apples</h2>)
end

end
2 changes: 2 additions & 0 deletions spec/test-data/handlers/erb-page.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>This is an ERB page</h1>
<h2>The fruit of the day is: <%= "apples" %></h2>

0 comments on commit 16d8d58

Please sign in to comment.