nanoc
nanoc copied to clipboard
Multiple filters support for layouts.
Hello !
I would like to first say, thank you for all your work on nanoc. I love it ! This issue is a feature request (maybe it's already possible, but I couldn't figure out how).
I would like to apply multiple filters to a layout - at the moment I could only figure out how to apply one filter (eg erb
). The reason is that I would like to run the erb
filter, followed by a custom filter that minifies the result of the erb
filter.
The solution I came up with is this - but it is less than ideal because it implies using erb
all the time, and I didn't see an easy way to avoid that.
Gist:
# lib/filters/minify_filter.rb
class MinifyFilter < Nanoc::Filters::ERB
identifier :minify_erb
type :text
def run(content, params={})
content = super(content, params)
file = tmp_write(content)
run_minify(file.path)
File.read(file.path).tap { file.tap(&:unlink).close }
end
private
def run_minify(path)
system "minify", path, "--type", "html", "-o", path
end
def content_dir
source = config[:data_sources].find { _1[:type] == "filesystem" }
source[:content_dir] || File.join(Dir.getwd, "content")
end
def tmp_write(content)
file = Tempfile.new('minify')
file.write(content)
file.tap(&:flush)
end
end
# Rules
layout '**/*', :minify_erb