nanoc / features

Collection of feature requests
2 stars 1 forks source link

Allow reusing a set of filter/layout/... #58

Open sdalu opened 3 years ago

sdalu commented 3 years ago

When writing rules some of them can have a similar set of filter/layout/... it would be useful to provide a mechanism to reuse some block of code

For example:

# Define a set of filter/layout that can be reuse later
apply :markdown do
  # Set of complex filter/layout/...
  filter: kramdow, .. complex set of options ..
  filter :foo
  filter :bar
  ...
end

# Compiling by reussing set of filter/layout/...
compile 'blog/**/*.md' do
  apply :markdown
  ...
end
compile '**/*.md' do
  apply :markdown
  ...
end
denisdefreyne commented 3 years ago

Thanks for the suggestion! I’ve moved this to the features repository to keep track of it.

dseomn commented 1 month ago

I got some code working in the Rules file to do this, but I'm inexperienced at ruby and this might be very very hacky:

module Nanoc::RuleDSL
  class CompilationRuleContext < RuleContext
    def minify_html
      filter(
        :external,
        exec: 'minify',
        options: [
          '--type=html',
          '--html-keep-document-tags',
        ],
      )
      nil
    end
  end
end

compile %r{^/pages/([^/]+)/index\.html\.erb$} do |slug,|
  filter :erb
  layout '/page.html.erb'
  layout '/base.html.erb'
  minify_html
  write "/#{slug}/index.html"
end