Class: Nanoc::CompilerDSL
- Inherits:
-
Object
- Object
- Nanoc::CompilerDSL
- Defined in:
- lib/nanoc/base/compilation/compiler_dsl.rb
Overview
Contains methods that will be executed by the site’s Rules
file.
Instance Attribute Summary (collapse)
-
- (String) rules_filename
The current rules filename.
Instance Method Summary (collapse)
-
- (void) compile(identifier, params = {}) { ... }
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.
-
- (void) ignore(identifier, params = {})
Creates a pair of compilation and routing rules that indicate that the specified item(s) should be ignored, e.g.
-
- (void) include_rules(name)
Includes an additional rules file in the current rules collection.
-
- (CompilerDSL) initialize(rules_collection, config)
constructor
private
Creates a new compiler DSL for the given collection of rules.
-
- (void) layout(identifier, filter_name, params = {})
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.
-
- (void) passthrough(identifier, params = {})
Creates a pair of compilation and routing rules that indicate that the specified item(s) should be copied to the output folder as-is.
-
- (void) preprocess { ... }
Creates a preprocessor block that will be executed after all data is loaded, but before the site is compiled.
-
- (void) route(identifier, params = {}) { ... }
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.
Constructor Details
- (CompilerDSL) initialize(rules_collection, config)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new compiler DSL for the given collection of rules.
19 20 21 22 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 19 def initialize(rules_collection, config) @rules_collection = rules_collection @config = config end |
Instance Attribute Details
- (String) rules_filename
The current rules filename.
9 10 11 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 9 def rules_filename @rules_filename end |
Instance Method Details
- (void) compile(identifier, params = {}) { ... }
This method returns an undefined value.
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.
This rule will be applicable to reps with a name equal to :default
;
this can be changed by giving an explicit :rep
parameter.
An item rep will be compiled by calling the given block and passing the rep as a block argument.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 70 def compile(identifier, params = {}, &block) # Require block raise ArgumentError.new('#compile requires a block') unless block_given? # Get rep name rep_name = params[:rep] || :default # Create rule rule = Rule.new(identifier_to_regex(identifier), rep_name, block) @rules_collection.add_item_compilation_rule(rule) end |
- (void) ignore(identifier, params = {})
This method returns an undefined value.
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
empty rule. 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.
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 223 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 {}) @rules_collection.add_item_compilation_rule(compilation_rule) routing_rule = Rule.new(identifier_to_regex(identifier), rep_name, proc {}, snapshot_name: :last) @rules_collection.add_item_routing_rule(routing_rule) end |
- (void) include_rules(name)
This method returns an undefined value.
Includes an additional rules file in the current rules collection.
247 248 249 250 251 252 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 247 def include_rules(name) filename = ["#{name}", "#{name}.rb", "./#{name}", "./#{name}.rb"].find { |f| File.file?(f) } raise Nanoc::Errors::NoRulesFileFound.new if filename.nil? @rules_collection.parse(filename) end |
- (void) layout(identifier, filter_name, params = {})
This method returns an undefined value.
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 using the filter specified in the second argument. The params hash contains filter arguments that will be passed to the filter.
151 152 153 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 151 def layout(identifier, filter_name, params = {}) @rules_collection.layout_filter_mapping[identifier_to_regex(identifier)] = [filter_name, params] end |
- (void) passthrough(identifier, params = {})
This method returns an undefined value.
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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 180 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) # Create routing rule routing_block = proc do # This is a temporary solution until an item can map back to its data # source. # 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 routing_rule = Rule.new(identifier_to_regex(identifier), rep_name, routing_block, snapshot_name: :last) @rules_collection.add_item_routing_rule(routing_rule) end |
- (void) preprocess { ... }
This method returns an undefined value.
Creates a preprocessor block that will be executed after all data is loaded, but before the site is compiled.
30 31 32 33 34 35 36 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 30 def preprocess(&block) if @rules_collection.preprocessors[rules_filename] warn 'WARNING: A preprocess block is already defined. Defining ' \ 'another preprocess block overrides the previously one.' end @rules_collection.preprocessors[rules_filename] = block end |
- (void) route(identifier, params = {}) { ... }
This method returns an undefined value.
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.
This rule will be applicable to reps with a name equal to :default
;
this can be changed by giving an explicit :rep
parameter.
The path of an item rep will be determined by calling the given block and passing the rep as a block argument.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/nanoc/base/compilation/compiler_dsl.rb', line 114 def route(identifier, params = {}, &block) # Require 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 = Rule.new(identifier_to_regex(identifier), rep_name, block, snapshot_name: snapshot_name) @rules_collection.add_item_routing_rule(rule) end |