Class: Nanoc3::CompilerDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/base/compiler_dsl.rb

Overview

Contains methods that will be executed by the site’s Rules file.

Instance Method Summary

Constructor Details

- (CompilerDSL) initialize(site)

Creates a new compiler DSL for the given compiler.

Parameters:



11
12
13
# File 'lib/nanoc3/base/compiler_dsl.rb', line 11

def initialize(site)
  @site = site
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.

Examples:

Compiling the default rep of the /foo/ item

compile '/foo/' do
  rep.filter :erb
end

Compiling the :raw rep of the /bar/ item

compile '/bar/', :rep => :raw do
  # do nothing
end

Parameters:

  • (String) identifier

    A pattern matching identifiers of items that should be compiled using this rule

Options Hash (params):

  • (Symbol) :rep — default: :default

    The name of the representation that should be compiled using this rule

Yields:

  • The block that will be executed when an item matching this compilation rule needs to be compiled

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nanoc3/base/compiler_dsl.rb', line 57

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)
  @site.compiler.item_compilation_rules << rule
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.

Examples:

Specifying the filter to use for a layout

layout '/default/', :erb

Using custom filter arguments for a layout

layout '/custom/',  :haml, :format => :html5

Parameters:

  • (String) identifier

    A pattern matching identifiers of layouts that should be filtered using this rule

  • (Symbol) filter_name

    The name of the filter that should be run when processing the layout

  • (Hash) params (defaults to: {})

    Extra filter arguments that should be passed to the filter when processing the layout (see Nanoc3::Filter#run)



137
138
139
# File 'lib/nanoc3/base/compiler_dsl.rb', line 137

def layout(identifier, filter_name, params={})
  @site.compiler.layout_filter_mapping[identifier_to_regex(identifier)] = [ filter_name, params ]
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.

Yields:

  • The block that will be executed before site compilation starts



21
22
23
# File 'lib/nanoc3/base/compiler_dsl.rb', line 21

def preprocess(&block)
  @site.preprocessor = 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.

Examples:

Routing the default rep of the /foo/ item

route '/foo/' do
  item.identifier + 'index.html'
end

Routing the :raw rep of the /bar/ item

route '/bar/', :rep => :raw do
  '/raw' + item.identifier + 'index.txt'
end

Parameters:

  • (String) identifier

    A pattern matching identifiers of items that should be routed using this rule

Options Hash (params):

  • (Symbol) :rep — default: :default

    The name of the representation that should be routed using this rule

Yields:

  • The block that will be executed when an item matching this compilation rule needs to be routed

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/nanoc3/base/compiler_dsl.rb', line 101

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

  # Create rule
  rule = Rule.new(identifier_to_regex(identifier), rep_name, block)
  @site.compiler.item_routing_rules << rule
end