lib/nanoc3/base/compilation/compiler_dsl.rb in nanoc3-3.2.0a3 vs lib/nanoc3/base/compilation/compiler_dsl.rb in nanoc3-3.2.0a4

- old
+ new

@@ -3,30 +3,28 @@ module Nanoc3 # Contains methods that will be executed by the site’s `Rules` file. class CompilerDSL - # @return [Nanoc3::Compiler] The compiler where this DSL belongs to. - attr_reader :compiler - - # Creates a new compiler DSL for the given compiler. + # Creates a new compiler DSL for the given collection of rules. # # @api private # - # @param [Nanoc3::Site] site The site this DSL belongs to - def initialize(compiler) - @compiler = compiler + # @param [Nanoc3::RulesCollection] rules_collection The collection of rules + # to modify when loading this DSL + def initialize(rules_collection) + @rules_collection = rules_collection end # Creates a preprocessor block that will be executed after all data is # loaded, but before the site is compiled. # # @yield The block that will be executed before site compilation starts # # @return [void] def preprocess(&block) - compiler.preprocessor = block + @rules_collection.preprocessor = block end # Creates a compilation rule for all items whose identifier match the # given identifier, which may either be a string containing the * # wildcard, or a regular expression. @@ -66,11 +64,11 @@ # Get rep name rep_name = params[:rep] || :default # Create rule rule = Rule.new(identifier_to_regex(identifier), rep_name, block) - compiler.item_compilation_rules << rule + @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 `*` # wildcard, or a regular expression. @@ -111,11 +109,11 @@ rep_name = params[:rep] || :default snapshot_name = params[:snapshot] || :last # Create rule rule = Rule.new(identifier_to_regex(identifier), rep_name, block, :snapshot_name => snapshot_name) - compiler.item_routing_rules << rule + @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 # regular expression. The layouts matching the identifier will be filtered @@ -139,10 +137,55 @@ # # @example Using custom filter arguments for a layout # # layout '/custom/', :haml, :format => :html5 def layout(identifier, filter_name, params={}) - compiler.layout_filter_mapping[identifier_to_regex(identifier)] = [ filter_name, params ] + @rules_collection.layout_filter_mapping[identifier_to_regex(identifier)] = [ filter_name, params ] + end + + # Creates a pair of compilation and routing rules that indicate that the + # specified item(s) should be copied to the output folder as-is. The items + # are selected using an identifier, which may either be a string + # containing the `*` wildcard, or a regular expression. + # + # This meta-rule will be applicable to reps with a name equal to + # `: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 + # + # @return [void] + # + # @since 3.2.0 + # + # @example Copying the `/foo/` item as-is + # + # passthrough '/foo/' + # + # @example Copying the `:raw` rep of the `/bar/` item as-is + # + # passthrough '/bar/', :rep => :raw + def passthrough(identifier, params={}) + # Require no block + 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 = Rule.new(identifier_to_regex(identifier), rep_name, compilation_block) + @rules_collection.add_item_compilation_rule(compilation_rule, :before) + + # Create routing rule + routing_block = proc do + item.identifier.chop + '.' + item[:extension] + end + routing_rule = Rule.new(identifier_to_regex(identifier), rep_name, routing_block) + @rules_collection.add_item_routing_rule(routing_rule, :before) end private # Converts the given identifier, which can contain the '*' or '+'