Skip to content

Commit

Permalink
See if adding Encoding information to BaseHandler helps us with UTF.
Browse files Browse the repository at this point in the history
[Issue #158] There is a general problem with non ASCII encodings in
awestruct when running under Ruby 1.9. Mayhap by declaring the default
encoding to be UTF-8 we can be a little more better.
  • Loading branch information
lance committed Aug 14, 2012
1 parent 87351c6 commit aee0a38
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/awestruct/handlers/base_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require 'shellwords'
require 'open3'

if RUBY_VERSION.start_with? "1.9"
Encoding.default_external = "UTF-8"
else
puts RUBY_VERSION
end

module Awestruct
module Handlers
class BaseHandler
Expand Down
23 changes: 20 additions & 3 deletions spec/erb_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

# -*- coding: ASCII-8BIT -*-
require 'awestruct/page'
require 'awestruct/handlers/file_handler'
require 'awestruct/handlers/erb_handler'

Expand All @@ -7,11 +8,14 @@
describe Awestruct::Handlers::ErbHandler do

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

def handler_file(path)
"#{@site.dir}/#{path}"
Pathname.new( File.dirname( __FILE__ ) + "/test-data/handlers/#{path}" )
end

def create_context
Expand All @@ -35,4 +39,17 @@ def create_context
rendered.should =~ %r(<h2>The fruit of the day is: apples</h2>)
end

it "should interpolate variables" do
page = Awestruct::Page.new( @site, Awestruct::Handlers::ErbHandler::CHAIN.create( @site, handler_file("erb-page.html.erb") ) )
page.prepare!
page.content.should =~ %r(<h3>bacon</h3>)
end

it "should handle UTF character encodings" do
page = Awestruct::Page.new( @site, Awestruct::Handlers::ErbHandler::CHAIN.create( @site, handler_file("erb-utf-page.html.erb") ) )
page.prepare!
page.content.should == "# coding: UTF-8\nBesøg fra Danmark\n"
page.content
end

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

<p>Besøg fra Danmark</p>
2 changes: 2 additions & 0 deletions spec/test-data/handlers/erb-utf-page.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# coding: UTF-8
Besøg fra Danmark

0 comments on commit aee0a38

Please sign in to comment.