lib/nanoc/base/compilation/compiler_dsl.rb in nanoc-3.6.7 vs lib/nanoc/base/compilation/compiler_dsl.rb in nanoc-3.6.8

- old
+ new

@@ -23,10 +23,15 @@ # # @yield The block that will be executed before site compilation starts # # @return [void] def preprocess(&block) + if @rules_collection.preprocessor + warn 'WARNING: A preprocess block is already defined. Defining ' \ + 'another preprocess block overrides the previously one.' + end + @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 * @@ -58,13 +63,13 @@ # @example Compiling the `:raw` rep of the `/bar/` item # # compile '/bar/', :rep => :raw do # # do nothing # end - def compile(identifier, params={}, &block) + def compile(identifier, params = {}, &block) # Require block - raise ArgumentError.new("#compile requires a block") unless block_given? + raise ArgumentError.new('#compile requires a block') unless block_given? # Get rep name rep_name = params[:rep] || :default # Create rule @@ -102,13 +107,13 @@ # @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) + def route(identifier, params = {}, &block) # Require block - raise ArgumentError.new("#route requires a block") unless block_given? + raise ArgumentError.new('#route requires a block') unless block_given? # Get rep name rep_name = params[:rep] || :default snapshot_name = params[:snapshot] || :last @@ -139,11 +144,11 @@ # layout '/default/', :erb # # @example Using custom filter arguments for a layout # # layout '/custom/', :haml, :format => :html5 - def layout(identifier, filter_name, params={}) + def layout(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 @@ -168,19 +173,19 @@ # passthrough '/foo/' # # @example Copying the `:raw` rep of the `/bar/` item as-is # # passthrough '/bar/', :rep => :raw - def passthrough(identifier, params={}) + def passthrough(identifier, params = {}) # Require no block - raise ArgumentError.new("#passthrough does not require a block") if block_given? + 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_block = proc {} compilation_rule = Rule.new(identifier_to_regex(identifier), rep_name, compilation_block) @rules_collection.add_item_compilation_rule(compilation_rule) # Create routing rule routing_block = proc do @@ -211,19 +216,19 @@ # @return [void] # # @example Suppressing compilation and output for all all `/foo/*` items. # # ignore '/foo/*' - def ignore(identifier, params={}) - raise ArgumentError.new("#ignore does not require a block") if block_given? + def ignore(identifier, params = {}) + raise ArgumentError.new('#ignore does not require a block') if block_given? rep_name = params[:rep] || :default - compilation_rule = Rule.new(identifier_to_regex(identifier), rep_name, proc { }) + compilation_rule = Rule.new(identifier_to_regex(identifier), rep_name, proc {}) @rules_collection.add_item_compilation_rule(compilation_rule) - routing_rule = Rule.new(identifier_to_regex(identifier), rep_name, proc { }, :snapshot_name => :last) + routing_rule = Rule.new(identifier_to_regex(identifier), rep_name, proc {}, :snapshot_name => :last) @rules_collection.add_item_routing_rule(routing_rule) end # Includes an additional rules file in the current rules collection. # @@ -239,11 +244,11 @@ # include_rules 'rules/content' def include_rules(name) filename = [ "#{name}", "#{name}.rb", "./#{name}", "./#{name}.rb" ].find { |f| File.file?(f) } raise Nanoc::Errors::NoRulesFileFound.new if filename.nil? - self.instance_eval(File.read(filename), filename) + instance_eval(File.read(filename), filename) end private # Converts the given identifier, which can contain the '*' or '+' @@ -252,11 +257,11 @@ # into /^foo\/(.*?)\/bar$/ and 'foo+' is transformed into /^foo(.+?)/. def identifier_to_regex(identifier) if identifier.is_a? String # Add leading/trailing slashes if necessary new_identifier = identifier.dup - new_identifier[/^/] = '/' if identifier[0,1] != '/' - new_identifier[/$/] = '/' unless [ '*', '/' ].include?(identifier[-1,1]) + new_identifier[/^/] = '/' if identifier[0, 1] != '/' + new_identifier[/$/] = '/' unless [ '*', '/' ].include?(identifier[-1, 1]) /^#{new_identifier.gsub('*', '(.*?)').gsub('+', '(.+?)')}$/ else identifier end