From 7ea303361565d69d3fe087f4f3d4a29a1aaa6b98 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 21 Jun 2024 22:45:31 -0300 Subject: [PATCH] Add Nanoc::Extension::* --- Rules | 7 ++-- nanoc/lib/nanoc/extension/anonymous_filter.rb | 42 +++++++++++++++++++ .../{ruledsl => extension}/require_rules.rb | 15 ++++--- nanoc/lib/nanoc/ruledsl/filter.rb | 38 ----------------- 4 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 nanoc/lib/nanoc/extension/anonymous_filter.rb rename nanoc/lib/nanoc/{ruledsl => extension}/require_rules.rb (71%) delete mode 100644 nanoc/lib/nanoc/ruledsl/filter.rb diff --git a/Rules b/Rules index 697f8d3bd..ed05db5fe 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 000000000..15e16642d --- /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 5880ceb66..3d0c7ab3e 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 5cb9c9391..000000000 --- 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