diff --git a/example/app/posts.rb b/example/app/posts.rb index 93a6239..c105623 100644 --- a/example/app/posts.rb +++ b/example/app/posts.rb @@ -8,7 +8,7 @@ require "ace/filters" # - metadata # - config class Post < Ace::Item - before Ace::LayoutFilter, layout: "post.html" + before Ace::TemplateFilter, layout: "post.html" def document Nokogiri::HTML(self.content) diff --git a/example/app/tags.rb b/example/app/tags.rb index 12d7d7e..e2d0168 100644 --- a/example/app/tags.rb +++ b/example/app/tags.rb @@ -1,7 +1,7 @@ # encoding: utf-8 class Tag < Ace::Item - before Ace::LayoutFilter, layout: "tag.html" + before Ace::TemplateFilter, layout: "tag.html" end class TagPagesGenerator diff --git a/lib/ace/filters/layout.rb b/lib/ace/filters/layout.rb index 70a8241..6c18504 100644 --- a/lib/ace/filters/layout.rb +++ b/lib/ace/filters/layout.rb @@ -1,26 +1,10 @@ # encoding: utf-8 -require "ace/filters" -require "template-inheritance" +require_relative "template" -layouts = File.join(Dir.pwd, "layouts") -unless TemplateInheritance::Template.paths.include?(layouts) - TemplateInheritance::Template.paths.unshift(layouts) -end +warn "~ TemplateFilter is deprecated, use TemplateFilter from now on." module Ace - class LayoutFilter < Filter - class Scope - include Ace::Helpers - end - - def initialize(options) - @path = options[:layout] - end - - def call(item, content) - template = TemplateInheritance::Template.new(@path, Scope.new) - return template.render(item: item) - end + class TemplateFilter < TemplateFilter end end diff --git a/lib/ace/filters/template.rb b/lib/ace/filters/template.rb index 05c2026..9344028 100644 --- a/lib/ace/filters/template.rb +++ b/lib/ace/filters/template.rb @@ -3,25 +3,41 @@ require "ace/filters" require "template-inheritance" -layouts = File.join(Dir.pwd, "layouts") -unless TemplateInheritance::Template.paths.include?(layouts) - TemplateInheritance::Template.paths.unshift(layouts) -end - -TemplateInheritance::Template.paths << File.join(Dir.pwd, "content") - module Ace class TemplateFilter < Filter - TEMPLATE_EXTS_PATTERN = /\.(haml|erb|erubis)$/i + class Scope + include Ace::Helpers + end + + def self.add_to_paths(directory) + unless TemplateInheritance::Template.paths.include?(directory) + TemplateInheritance::Template.paths.unshift(directory) + end + end + + def initialize(options = Hash.new) + @path = options[:layout] + end def call(item, content) - if item.output_path.match(TEMPLATE_EXTS_PATTERN) - item.output_path = item.output_path.split(".")[0..-2].join(".") + if @path.nil? + @path = item.original_path.sub("content/", "") end - relative_path = item.original_path.sub("content/", "") - template = TemplateInheritance::Template.new(relative_path) + parts = item.output_path.split(".") + if parts.length == 2 # template.haml + item.output_path = "#{parts[0]}.html" + elsif parts.length == 3 # template.html.haml or template.xml.haml + item.output_path = "#{parts[0]}.#{parts[1]}" + else + raise "Template can be named either with one suffix as template.haml or with two of them as template.html.haml resp. template.xml.haml." + end + + template = TemplateInheritance::Template.new(@path, Scope.new) return template.render(item: item) end end end + +Ace::TemplateFilter.add_to_paths(File.join(Dir.pwd, "layouts")) +Ace::TemplateFilter.add_to_paths(File.join(Dir.pwd, "content")) diff --git a/lib/ace/version.rb b/lib/ace/version.rb index 103ce13..9ccb4bd 100644 --- a/lib/ace/version.rb +++ b/lib/ace/version.rb @@ -1,5 +1,5 @@ # encoding: utf-8 module Ace - VERSION = "0.3" + VERSION = "0.3.2" end