Skip to content

Commit

Permalink
Merge pull request #325 from LightGuard/issue_323
Browse files Browse the repository at this point in the history
Fixing #323
  • Loading branch information
LightGuard committed Jul 31, 2013
2 parents 30b9092 + 620ccdb commit 5eb4832
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 22 deletions.
26 changes: 22 additions & 4 deletions lib/awestruct/handlers/asciidoctor_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,34 @@
module Awestruct
module Handlers

class AsciidoctorMatcher
class AsciidoctorTiltMatcher < TiltMatcher
# Use a lightweight lookup to avoid loading Tilt templates for
# non-matching paths. Once we are sure this is a match, then
# attempt to load the Tilt template for AsciiDoc files.
def match(path)
engine = Tilt[path]
engine == Tilt::AsciidoctorTemplate
# formal lookup as implemented in Tilt
pattern = File.basename(path.downcase)
registered = false
until pattern.empty? || (registered = Tilt.registered?(pattern))
# shave pattern down to next extension
pattern = pattern.sub(/^[^.]*\.?/, '')
end
if registered && (Tilt.mappings[pattern] || []).include?(Tilt::AsciidoctorTemplate)
begin
Tilt[File.basename(path)]
rescue LoadError
# swallowing error as it will be picked up again by primary TiltHandler
false
end
else
false
end
end
end

class AsciidoctorHandler < BaseTiltHandler

CHAIN = Awestruct::HandlerChain.new( Awestruct::Handlers::AsciidoctorMatcher.new(),
CHAIN = Awestruct::HandlerChain.new( Awestruct::Handlers::AsciidoctorTiltMatcher.new(),
Awestruct::Handlers::FileHandler,
Awestruct::Handlers::FrontMatterHandler,
Awestruct::Handlers::AsciidoctorHandler,
Expand Down
44 changes: 34 additions & 10 deletions lib/awestruct/handlers/base_tilt_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@

module Awestruct
module Handlers

class TiltMatcher
# Returns the Tilt template class if a portion of the path is registered
# to a Tilt template and the Tilt template can be loaded. Returns false
# if no portions of the path are registered to a Tilt template or the
# Tilt template cannot be loaded.
def match(path)
begin
Tilt[File.basename(path)]
rescue LoadError => e
$LOG.warn(%(Copying #{path} to generated site without processing; missing required gem -- #{e.message.split(/ *-- */).last} (or equivalent)))
false
end
end
end

class BaseTiltHandler < BaseHandler

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

def source_file_name
Expand All @@ -22,8 +38,8 @@ def double_extension?
end

def simple_name
base = File.basename( source_file_name, File.extname( source_file_name ))
return File.basename( base, File.extname( base ) ) if double_extension?
base = File.basename(source_file_name, File.extname(source_file_name))
return File.basename(base, File.extname(base)) if double_extension?
return base
end

Expand All @@ -32,11 +48,11 @@ def output_filename
end

def input_extension
File.extname( source_file_name )
File.extname(source_file_name)
end

def output_extension
return File.extname( File.basename( source_file_name, File.extname( source_file_name ))) if double_extension?
return File.extname(File.basename(source_file_name, File.extname(source_file_name))) if double_extension?

template = Tilt[path]
if !template.nil?
Expand All @@ -55,7 +71,7 @@ def output_extension
def content_syntax
# Check configuration for override, else convert extension to sym
extension = input_extension[1..-1]
if ( !site[:content_syntax].nil? && !site[:content_syntax].empty?)
if (!site[:content_syntax].nil? && !site[:content_syntax].empty?)
syntax = site[:content_syntax][extension]
return syntax.to_sym unless syntax.nil? or syntax.empty?
end
Expand Down Expand Up @@ -103,10 +119,18 @@ def options

def rendered_content(context, with_layouts=true)
$LOG.debug "invoking tilt for #{delegate.path.to_s} with_layouts = #{with_layouts}" if $LOG.debug?
template = Tilt::new(delegate.path.to_s, delegate.content_line_offset + 1, options) { |engine|
delegate.rendered_content( context, with_layouts )
}
template.render( context )
begin
template = Tilt::new(delegate.path.to_s, delegate.content_line_offset + 1, options) { |engine|
delegate.rendered_content(context, with_layouts)
}
return template.render(context)
rescue LoadError => e
$LOG.error "Could not load template library required for rendering #{delegate.path.to_s}, please see rendered output for more information" if $LOG.error?
return "<h1>#{e.message}</h1><h2>Rendering file #{delegate.path.to_s} resulted in a failure.</h2><p>Backtrace: #{e.backtrace.join '<br>'}</p>"
rescue Exception => e
$LOG.error "An error during rendering #{delegate.path.to_s} occurred, please see rendered output for more information" if $LOG.error?
return "<h1>#{e.message}</h1><h2>Rendering file #{delegate.path.to_s} resulted in a failure.</h2><h3>Line: #{e.line if e.respond_to?(:line)}</h3><p>Backtrace: #{e.backtrace.join '<br>'}</p>"
end
end

end
Expand Down
8 changes: 1 addition & 7 deletions lib/awestruct/handlers/tilt_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
module Awestruct
module Handlers

class TiltMatcher
def match(path)
!Tilt[path].nil?
end
end

class NonInterpolatingTiltMatcher
EXT_REGEX = /\.(haml|slim|erb|mustache)$/

def match(path)
if match = EXT_REGEX.match(path)
if match[0] == '.slim' && Tilt[path].nil?
if match[0] == '.slim' && !Tilt.registered?('slim')
require 'slim'
end
true
Expand Down
4 changes: 4 additions & 0 deletions spec/test-data/handlers/haml-error.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%h1
testing
%span
Hello World!
1 change: 1 addition & 0 deletions spec/test-data/handlers/hello.bogus
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xml.em('Hello World!')
67 changes: 66 additions & 1 deletion spec/tilt_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@

require 'logger'
require 'awestruct/config'
#require 'awestruct/engine'
require 'awestruct/handlers/file_handler'
require 'awestruct/handlers/tilt_handler'

require 'hashery'
require 'tilt/template'


module Tilt
class BogusTemplate < Template
self.default_mime_type = 'text/html'

def self.engine_initialized?
defined? ::Bogus::Document
end

def initialize_engine
require_template_library 'fake-gem-name'
end

def evaluate(scope, locals, &block)
@output ||= "bogus, bogus, bogus"
end

def allows_script?
false
end
end
end


describe Awestruct::Handlers::TiltHandler do

Expand Down Expand Up @@ -76,4 +102,43 @@ def create_context
handler.output_filename.should eql 'warp-1.0.0.Alpha2.html'

end

context 'when loading an engine not installed' do
specify 'should not throw exceptions; instead have the error in the rendered output' do
# setup
Tilt::register Tilt::BogusTemplate, '.bogus',
log = StringIO.new
$LOG = Logger.new(log)
$LOG.level = Logger::DEBUG
@site.dir = Pathname.new( File.dirname(__FILE__) + '/test-data/handlers/' )
path = handler_file( "hello.bogus" )
expect(Awestruct::Handlers::TiltMatcher.new().match(path)).to be_false
expect(log.string).to include('missing required gem')

# we don't even want to process a file if we cannot load its Tilt template
#file_handler = Awestruct::Handlers::FileHandler.new( @site, path )
#handler = Awestruct::Handlers::TiltHandler.new( @site, file_handler )
#content = handler.rendered_content(create_context)

#expect(content).to_not eql ('bogus, bogus, bogus')
#expect(content).to include('load', 'fake-gem-name')
end
end

context 'when rendering a file with an error' do
specify 'should not stop processing, but render the error as the file' do
# setup
log = StringIO.new
$LOG = Logger.new(log)
$LOG.level = Logger::DEBUG
@site.dir = Pathname.new( File.dirname(__FILE__) + '/test-data/handlers/' )
file_handler = Awestruct::Handlers::FileHandler.new( @site, handler_file( "haml-error.html.haml" ) )
handler = Awestruct::Handlers::TiltHandler.new( @site, file_handler )
content = handler.rendered_content(create_context)

expect(content).to_not be_empty
expect(content).to include('Illegal', 'nesting', 'Line', '2')
end
end

end

0 comments on commit 5eb4832

Please sign in to comment.