lib/nanoc/base/compilation/compiler_dsl.rb in nanoc-4.0.2 vs lib/nanoc/base/compilation/compiler_dsl.rb in nanoc-4.1.0a1
- old
+ new
@@ -48,12 +48,11 @@
# rep as a block argument.
#
# @param [String] identifier A pattern matching identifiers of items that
# should be compiled using this rule
#
- # @option params [Symbol] :rep (:default) The name of the representation
- # that should be compiled using this rule
+ # @param [Symbol] rep The name of the representation
#
# @yield The block that will be executed when an item matching this
# compilation rule needs to be compiled
#
# @return [void]
@@ -67,19 +66,14 @@
# @example Compiling the `:raw` rep of the `/bar/` item
#
# compile '/bar/', :rep => :raw do
# # do nothing
# end
- def compile(identifier, params = {}, &block)
- # Require block
+ def compile(identifier, rep: :default, &block)
raise ArgumentError.new('#compile requires a block') unless block_given?
- # Get rep name
- rep_name = params[:rep] || :default
-
- # Create rule
- rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, block)
+ rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, block)
@rules_collection.add_item_compilation_rule(rule)
end
# Creates a routing rule for all items whose identifier match the
# given identifier, which may either be a string containing the `*`
@@ -92,12 +86,11 @@
# and passing the rep as a block argument.
#
# @param [String] identifier A pattern matching identifiers of items that
# should be routed using this rule
#
- # @option params [Symbol] :rep (:default) The name of the representation
- # that should be routed using this rule
+ # @param [Symbol] :rep The name of the representation
#
# @yield The block that will be executed when an item matching this
# compilation rule needs to be routed
#
# @return [void]
@@ -111,20 +104,14 @@
# @example Routing the `:raw` rep of the `/bar/` item
#
# route '/bar/', :rep => :raw do
# '/raw' + item.identifier + 'index.txt'
# end
- def route(identifier, params = {}, &block)
- # Require block
+ def route(identifier, rep: :default, snapshot: :last, &block)
raise ArgumentError.new('#route requires a block') unless block_given?
- # Get rep name
- rep_name = params[:rep] || :default
- snapshot_name = params[:snapshot] || :last
-
- # Create rule
- rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, block, snapshot_name: snapshot_name)
+ rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, block, snapshot_name: snapshot)
@rules_collection.add_item_routing_rule(rule)
end
# Creates a layout rule for all layouts whose identifier match the given
# identifier, which may either be a string containing the * wildcard, or a
@@ -164,12 +151,11 @@
# `:default`; this can be changed by giving an explicit `:rep` parameter.
#
# @param [String] identifier A pattern matching identifiers of items that
# should be processed using this meta-rule
#
- # @option params [Symbol] :rep (:default) The name of the representation
- # that should be routed using this rule
+ # @param [Symbol] rep The name of the representation
#
# @return [void]
#
# @since 3.2.0
#
@@ -178,20 +164,15 @@
# passthrough '/foo/'
#
# @example Copying the `:raw` rep of the `/bar/` item as-is
#
# passthrough '/bar/', :rep => :raw
- def passthrough(identifier, params = {})
- # Require no block
+ def passthrough(identifier, rep: :default)
raise ArgumentError.new('#passthrough does not require a block') if block_given?
- # Get rep name
- rep_name = params[:rep] || :default
-
- # Create compilation rule
compilation_block = proc {}
- compilation_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, compilation_block)
+ compilation_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, compilation_block)
@rules_collection.add_item_compilation_rule(compilation_rule)
# Create routing rule
routing_block = proc do
if item.identifier.full?
@@ -202,11 +183,11 @@
# ATM item[:content_filename] is nil for items coming from the static
# data source.
item[:extension].nil? || (item[:content_filename].nil? && item.identifier =~ %r{#{item[:extension]}/$}) ? item.identifier.chop : item.identifier.chop + '.' + item[:extension]
end
end
- routing_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, routing_block, snapshot_name: :last)
+ routing_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, routing_block, snapshot_name: :last)
@rules_collection.add_item_routing_rule(routing_rule)
end
# Creates a pair of compilation and routing rules that indicate that the
# specified item(s) should be ignored, e.g. compiled and routed with an
@@ -217,27 +198,24 @@
# `:default`; this can be changed by giving an explicit `:rep` parameter.
#
# @param [String] identifier A pattern matching identifiers of items that
# should be processed using this meta-rule
#
- # @option params [Symbol] :rep (:default) The name of the representation
- # that should be routed using this rule
+ # @param [Symbol] rep The name of the representation
#
# @return [void]
#
# @example Suppressing compilation and output for all all `/foo/*` items.
#
# ignore '/foo/*'
- def ignore(identifier, params = {})
+ def ignore(identifier, rep: :default)
raise ArgumentError.new('#ignore does not require a block') if block_given?
- rep_name = params[:rep] || :default
-
- compilation_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, proc {})
+ compilation_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, proc {})
@rules_collection.add_item_compilation_rule(compilation_rule)
- routing_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep_name, proc {}, snapshot_name: :last)
+ routing_rule = Nanoc::Int::Rule.new(create_pattern(identifier), rep, proc {}, snapshot_name: :last)
@rules_collection.add_item_routing_rule(routing_rule)
end
# Includes an additional rules file in the current rules collection.
#
@@ -253,10 +231,24 @@
# include_rules 'rules/content'
def include_rules(name)
filename = ["#{name}", "#{name}.rb", "./#{name}", "./#{name}.rb"].find { |f| File.file?(f) }
raise Nanoc::Int::Errors::NoRulesFileFound.new if filename.nil?
- @rules_collection.parse(filename)
+ Nanoc::Int::RulesLoader.new(@config, @rules_collection).parse(filename)
+ end
+
+ # Creates a postprocessor block that will be executed after all data is
+ # loaded and the site is compiled.
+ #
+ # @yield The block that will be executed after site compilation completes
+ #
+ # @return [void]
+ def postprocess(&block)
+ if @rules_collection.postprocessors[rules_filename]
+ warn 'WARNING: A postprocess block is already defined. Defining ' \
+ 'another postprocess block overrides the previously one.'
+ end
+ @rules_collection.postprocessors[rules_filename] = block
end
# @api private
def create_pattern(arg)
case @config[:string_pattern_type]