diff --git a/Rules b/Rules index 697f8d3..ed05db5 100644 --- a/Rules +++ b/Rules @@ -20,16 +20,15 @@ tidy = `which tidy || which tidy5`.chomp buildenv = ENV["buildenv"] || "development" ## -# DSL extensions -Nanoc::RuleDSL::CompilationRuleContext.prepend(Nanoc::RuleDSL::Filter) -Nanoc::RuleDSL::CompilerDSL.prepend(Nanoc::RuleDSL::RequireRules) +# Extensions +Nanoc::RuleDSL::CompilationRuleContext.prepend(Nanoc::Extension::AnonymousFilter) +Nanoc::RuleDSL::CompilerDSL.prepend(Nanoc::Extension::RequireRules) ## # Plugins Nanoc::Webpack .default_argv .replace([*Nanoc::Webpack.default_argv, "--config", "webpack.#{buildenv}.js"].uniq) - Nanoc::Tidy .default_argv .replace([*Nanoc::Tidy.default_argv, "--config", "webpack.#{buildenv}.js"].uniq) diff --git a/nanoc/lib/nanoc/extension/anonymous_filter.rb b/nanoc/lib/nanoc/extension/anonymous_filter.rb new file mode 100644 index 0000000..15e1664 --- /dev/null +++ b/nanoc/lib/nanoc/extension/anonymous_filter.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Nanoc::Extension + ## + # The AnonymousFilter extension adds support for Proc-based + # filters. See the Rules file to learn how this module is + # added to nanoc. + module AnonymousFilter + require "securerandom" + ## + # @example + # compile "/sitemap.xml.erb" do + # filter(:erb) + # filter Proc.new { _1.each_line.reject { |s| s.strip.empty? } } + # write("/sitemap.xml") + # end + # @param [Proc, Symbol] fn + # Symbol, or Proc for an anonymous filter + # @param [Hash] options + # Filter options + # @return [void] + def filter(fn, options = {}) + if Proc === fn + id = anonymous_id + Nanoc::Filter.define(id) { fn.call(_1, _2) } + super(id, options) + else + super(fn, options) + end + end + + private + + def anonymous_id + [ + "__nanoc", + item.identifier.to_s.gsub(%r{[./\\]}, "_")[/[A-Za-z0-9_]+/], + SecureRandom.alphanumeric + ].join("_").to_sym + end + end +end diff --git a/nanoc/lib/nanoc/ruledsl/require_rules.rb b/nanoc/lib/nanoc/extension/require_rules.rb similarity index 71% rename from nanoc/lib/nanoc/ruledsl/require_rules.rb rename to nanoc/lib/nanoc/extension/require_rules.rb index 5880ceb..3d0c7ab 100644 --- a/nanoc/lib/nanoc/ruledsl/require_rules.rb +++ b/nanoc/lib/nanoc/extension/require_rules.rb @@ -1,20 +1,19 @@ # frozen_string_literal: true -module Nanoc::RuleDSL +module Nanoc::Extension + ## + # The RequireRules extension adds a method that can help + # break up the Rules file into multiple separate files. + # See the Rules file to learn how this module is added + # to nanoc. module RequireRules ## # @example # require_rules "nanoc/rules/assets" - # + # require_rules "nanoc/rules/index", {locales: ["en", "ar", "fa"]} # @param [String] path - # The path to a file - # # @param [Hash] locals - # A hash of locals - # # @param [Binding] target - # Binding context - # # @return [void] def require_rules(path, locals = {}, target = binding) locals.each { target.local_variable_set(_1, _2) } diff --git a/nanoc/lib/nanoc/ruledsl/filter.rb b/nanoc/lib/nanoc/ruledsl/filter.rb deleted file mode 100644 index 5cb9c93..0000000 --- a/nanoc/lib/nanoc/ruledsl/filter.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -module Nanoc::RuleDSL - module Filter - ## - # @example - # compile "/sitemap.xml.erb" do - # filter(:erb) - # filter Proc.new { _1.each_line.reject { |s| s.strip,empty? } } - # write("/sitemap.xml") - # end - # - # @param [Proc, Symbol] fn - # The name of a Symbol, or Proc for an anonymous filter - # - # @param [Hash] options - # Filter options - # - # @return [void] - def filter(fn, options = {}) - if Proc === fn - id = random_id - Nanoc::Filter.define(id) { fn.call(_1, _2) } - super(id, options) - else - super(fn, options) - end - end - - private - - def random_id - name = item.identifier.to_s.gsub(%r{[./\\]}, "_")[/[A-Za-z0-9_]+/] - random = SecureRandom.alphanumeric - :"__nanoc_#{name}_#{random}" - end - end -end