Skip to content

Commit

Permalink
Merge pull request #240 from mojavelinux/per-output-engine-config
Browse files Browse the repository at this point in the history
support engine options overrides by output type
  • Loading branch information
LightGuard committed Mar 11, 2013
2 parents f04d1e6 + b49413f commit ecd0470
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 23 deletions.
24 changes: 20 additions & 4 deletions lib/awestruct/config/default-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ content_syntax:
adoc: asciidoc
ad: asciidoc

haml:
default:
:attr_wrapper: '"'
:escape_attrs: :once
html:
:format: :html5

slim:
default:
:sort_attrs: false
:disable_escape: true
html:
:format: :html5

asciidoctor:
:backend: html5
:attributes:
imagesdir: '/images'
stylesdir: '/stylesheets'
default:
:backend: html5
:safe: 1
:attributes:
imagesdir: /images
stylesdir: /stylesheets
23 changes: 21 additions & 2 deletions lib/awestruct/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ def load_site_yaml(profile)
if ( ( k == 'profiles' ) && ( ! profile.nil? ) )
profile_data = ( v[profile] || {} )
else
site.send( "#{k}=", v )
site.send( "#{k}=", merge_data( site.send( "#{k}" ), v ) )
end
end if data
site.profile = profile

profile_data.each do |k,v|
site.send( "#{k}=", v )
site.send( "#{k}=", merge_data( site.send( "#{k}" ), v ) )
end
end
end
Expand All @@ -146,6 +146,25 @@ def load_yaml(yaml_path)
site.send( "#{name}=", massage_yaml( data ) )
end

def merge_data(existing, new)
if existing.kind_of? Hash
existing.inject({}) do |merged, (k,v)|
if new.has_key? k
if v.kind_of? Hash
merged[k] = v.merge new[k]
else
merged[k] = new[k]
end
else
merged[k] = v
end
merged
end
else
new
end
end

def massage_yaml(obj)
result = obj
case ( obj )
Expand Down
6 changes: 4 additions & 2 deletions lib/awestruct/extensions/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def execute(site)
# check for a date inside the page first
if (page.date?)
page.relative_source_path =~ /^#{@path_prefix}\/(.*)\..*$/
date = page.date;
date = page.date
if date.kind_of? String
date = Time.parse page.date
date = Time.parse date
elsif date.kind_of? Date
date = date.to_time
end
year = date.year
month = sprintf( "%02d", date.month )
Expand Down
35 changes: 29 additions & 6 deletions lib/awestruct/handlers/base_tilt_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def output_extension
if !mime.nil?
return '.js' if mime.eql? 'application/javascript'
return '.html' if mime.eql? 'text/html'
return '.xml' if mime.eql? 'text/xml'
return '.css' if mime.eql? 'text/css'
return '.html' # if all else falls trough
end
end
return ".html"
return '.html'
end

def content_syntax
Expand All @@ -65,12 +66,34 @@ def content_syntax
def options
opts = {}

extension = input_extension[1..-1].to_sym
extension_options = site[extension] unless site[extension].nil?
opts.merge! extension_options unless extension_options.nil?
engine_name = Tilt[path].name.gsub(/(Tilt|:|Template)/i, '').downcase.to_sym
engine_options = site[engine_name]
unless engine_options.nil?
if engine_options.has_key? 'default'
opts.merge! engine_options['default']
if engine_options.has_key? output_extension[1..-1]
opts.merge! engine_options[output_extension[1..-1]]
end
else
opts.merge! engine_options
end
end

engine_options = site[ Tilt[path].name.gsub(/(Tilt|:|Template)/i, '').downcase.to_sym ]
opts.merge! engine_options unless engine_options.nil?
# config overrides for specific file extension if different from engine name
extension = input_extension[1..-1].to_sym
unless engine_name == extension
extension_options = site[extension] unless site[extension].nil?
unless extension_options.nil?
if extension_options.has_key? 'default'
opts.merge! extension_options['default']
if extension_options.has_key? output_extension[1..-1]
opts.merge! extension_options[output_extension[1..-1]]
end
else
opts.merge! extension_options
end
end
end

return opts
end
Expand Down
6 changes: 3 additions & 3 deletions lib/awestruct/handlers/tilt_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def match(path)
end

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

def match(path)
if match = EXT_REGEX.match(path)
if match[0] == '.slim' && !defined?(Slim)
if match[0] == '.slim' && Tilt[path].nil?
require 'slim'
end
true
Expand Down Expand Up @@ -70,4 +70,4 @@ def initialize_engine
Haml::Filters.register_tilt_filter 'Textile'
end
end
end
end
4 changes: 2 additions & 2 deletions spec/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
engine = Awestruct::Engine.new(config)
engine.load_default_site_yaml

engine.site.asciidoctor.backend.should == 'html5'
engine.site.asciidoctor['default'][:backend].should == 'html5'
end

it "should be able to override default with site" do
Expand All @@ -21,7 +21,7 @@
engine.load_default_site_yaml
engine.load_site_yaml( 'development' )

engine.site.asciidoctor.attributes.backend.should == 'html4'
engine.site.asciidoctor['default'][:attributes]['backend'].should == 'html4'
end

it "should be able to load site.yml with the correct profile" do
Expand Down
5 changes: 3 additions & 2 deletions spec/test-data/engine/_config/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ profiles:
title: Awestruction!

asciidoctor:
attributes:
backend: html4
default:
:attributes:
backend: html4
4 changes: 2 additions & 2 deletions spec/tilt_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def create_context
end

it "should override engine configuration options over output_extension" do
@site.asciidoc.property = 'test'
@site.asciidoctor.property = 'test1'
@site.asciidoctor.property = 'test'
@site.asciidoc.property = 'test1'

file_handler = Awestruct::Handlers::FileHandler.new( @site, handler_file( "asciidoc-page.asciidoc" ) )
handler = Awestruct::Handlers::TiltHandler.new( @site, file_handler )
Expand Down

0 comments on commit ecd0470

Please sign in to comment.