Add Nanoc::Extension::*

This commit is contained in:
0x1eef 2024-06-21 22:45:31 -03:00
parent 20908968e5
commit 7ea3033615
4 changed files with 52 additions and 50 deletions

7
Rules
View file

@ -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)

View file

@ -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

View file

@ -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) }

View file

@ -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