Class: Nanoc::RuleDSL::CompilerDSL Private
- Inherits:
-
Core::Context
- Object
- Core::Context
- Nanoc::RuleDSL::CompilerDSL
- Defined in:
- lib/nanoc/rule_dsl/compiler_dsl.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
-
#rules_filename ⇒ String
private
The current rules filename.
Instance Method Summary collapse
-
#compile(identifier, rep: :default) { ... } ⇒ void
private
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.
- #create_pattern(arg) ⇒ Object private
-
#ignore(identifier, rep: :default) ⇒ void
private
Creates a pair of compilation and routing rules that indicate that the specified item(s) should be ignored, e.g.
-
#include_rules(name) ⇒ void
private
Includes an additional rules file in the current rules collection.
-
#initialize(rules_collection, config) ⇒ CompilerDSL
constructor
private
Creates a new compiler DSL for the given collection of rules.
-
#layout(identifier, filter_name, params = {}) ⇒ void
private
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.
-
#passthrough(identifier, rep: :default) ⇒ void
private
Creates a pair of compilation and routing rules that indicate that the specified item(s) should be copied to the output folder as-is.
-
#postprocess { ... } ⇒ void
private
Creates a postprocessor block that will be executed after all data is loaded and the site is compiled.
-
#preprocess { ... } ⇒ void
private
Creates a preprocessor block that will be executed after all data is loaded, but before the site is compiled.
-
#route(identifier, rep: :default, snapshot: :last) { ... } ⇒ void
private
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
#initialize(rules_collection, config) ⇒ CompilerDSL
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.
20 21 22 23 24 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 20 def initialize(rules_collection, config) @rules_collection = rules_collection @config = config super({ config: }) end |
Instance Attribute Details
#rules_filename ⇒ String
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.
The current rules filename.
10 11 12 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 10 def rules_filename @rules_filename end |
Instance Method Details
#compile(identifier, rep: :default) { ... } ⇒ void
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.
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.
71 72 73 74 75 76 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 71 def compile(identifier, rep: :default, &block) raise ArgumentError.new('#compile requires a block') unless block_given? rule = Nanoc::RuleDSL::CompilationRule.new(create_pattern(identifier), rep, block) @rules_collection.add_item_compilation_rule(rule) end |
#create_pattern(arg) ⇒ Object
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.
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 254 def create_pattern(arg) case @config[:string_pattern_type] when 'glob' Nanoc::Core::Pattern.from(arg) when 'legacy' Nanoc::Core::Pattern.from(identifier_to_regex(arg)) else raise( Nanoc::Core::TrivialError, "Invalid string_pattern_type: #{@config[:string_pattern_type]}", ) end end |
#ignore(identifier, rep: :default) ⇒ void
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.
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.
210 211 212 213 214 215 216 217 218 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 210 def ignore(identifier, rep: :default) raise ArgumentError.new('#ignore does not require a block') if block_given? compilation_rule = Nanoc::RuleDSL::CompilationRule.new(create_pattern(identifier), rep, proc {}) @rules_collection.add_item_compilation_rule(compilation_rule) routing_rule = Nanoc::RuleDSL::RoutingRule.new(create_pattern(identifier), rep, proc {}, snapshot_name: :last) @rules_collection.add_item_routing_rule(routing_rule) end |
#include_rules(name) ⇒ void
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.
This method returns an undefined value.
Includes an additional rules file in the current rules collection.
232 233 234 235 236 237 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 232 def include_rules(name) filename = [name.to_s, "#{name}.rb", "./#{name}", "./#{name}.rb"].find { |f| File.file?(f) } raise Nanoc::RuleDSL::Errors::NoRulesFileFound.new if filename.nil? Nanoc::RuleDSL::RulesLoader.new(@config, @rules_collection).parse(filename) end |
#layout(identifier, filter_name, params = {}) ⇒ void
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.
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.
142 143 144 145 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 142 def layout(identifier, filter_name, params = {}) pattern = Nanoc::Core::Pattern.from(create_pattern(identifier)) @rules_collection.layout_filter_mapping[pattern] = [filter_name, params] end |
#passthrough(identifier, rep: :default) ⇒ void
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.
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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 169 def passthrough(identifier, rep: :default) raise ArgumentError.new('#passthrough does not require a block') if block_given? compilation_block = proc {} compilation_rule = Nanoc::RuleDSL::CompilationRule.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? item.identifier.to_s else # 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 end routing_rule = Nanoc::RuleDSL::RoutingRule.new(create_pattern(identifier), rep, routing_block, snapshot_name: :last) @rules_collection.add_item_routing_rule(routing_rule) end |
#postprocess { ... } ⇒ void
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.
This method returns an undefined value.
Creates a postprocessor block that will be executed after all data is loaded and the site is compiled.
245 246 247 248 249 250 251 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 245 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 |
#preprocess { ... } ⇒ void
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.
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.
32 33 34 35 36 37 38 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 32 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 |
#route(identifier, rep: :default, snapshot: :last) { ... } ⇒ void
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.
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.
111 112 113 114 115 116 |
# File 'lib/nanoc/rule_dsl/compiler_dsl.rb', line 111 def route(identifier, rep: :default, snapshot: :last, &block) raise ArgumentError.new('#route requires a block') unless block_given? rule = Nanoc::RuleDSL::RoutingRule.new(create_pattern(identifier), rep, block, snapshot_name: snapshot) @rules_collection.add_item_routing_rule(rule) end |