Class: Nanoc2::Filter

Inherits:
Plugin
  • Object
show all
Defined in:
lib/nanoc2/base/filter.rb

Overview

Nanoc2::Filter is responsible for filtering pages and textual assets (binary assets are filtered using Nanoc2::BinaryFilter). It is the (abstract) superclass for all textual filters. Subclasses should override the run method.

Constant Summary collapse

EXTENSIONS_MAP =

Deprecated

{}

Constants inherited from Plugin

Plugin::MAP

Instance Method Summary collapse

Methods inherited from Plugin

identifier, identifiers, named, register

Constructor Details

#initialize(obj_rep, other_assigns = {}) ⇒ Filter

Creates a new filter for the given object (page or asset) and site.

kind

The kind of object that is passed. Can be either :page or :asset.

obj_rep

A proxy for the page or asset representation (Nanoc2::PageRep or Nanoc2::AssetRep) that should be compiled by this filter.

obj

A proxy for the page or asset’s page (Nanoc2::Page or Nanoc2::Asset).

site

The site (Nanoc2::Site) this filter belongs to.

other_assigns

A hash containing other variables that should be made available during filtering.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nanoc2/base/filter.rb', line 27

def initialize(obj_rep, other_assigns={})
  # Determine kind
  @kind = obj_rep.is_a?(Nanoc2::PageRep) ? :page : :asset

  # Set object
  @obj_rep = obj_rep
  @obj     = (@kind == :page ? @obj_rep.page : @obj_rep.asset)

  # Set page/asset and page/asset reps
  if @kind == :page
    @page       = @obj
    @page_rep   = @obj_rep
  else
    @asset      = @obj
    @asset_rep  = @obj_rep
  end

  # Set site
  @site = @obj.site

  # Set other assigns
  @other_assigns  = other_assigns
end

Instance Method Details

#assignsObject

Returns a hash with data that should be available.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nanoc2/base/filter.rb', line 61

def assigns
  @assigns ||= @other_assigns.merge({
    :_obj_rep   => @obj_rep,
    :_obj       => @obj,
    :page_rep   => @kind == :page  ? @page_rep.to_proxy  : nil,
    :page       => @kind == :page  ? @page.to_proxy      : nil,
    :asset_rep  => @kind == :asset ? @asset_rep.to_proxy : nil,
    :asset      => @kind == :asset ? @asset.to_proxy     : nil,
    :pages      => @site.pages.map    { |obj| obj.to_proxy },
    :assets     => @site.assets.map   { |obj| obj.to_proxy },
    :layouts    => @site.layouts.map  { |obj| obj.to_proxy },
    :config     => @site.config,
    :site       => @site
  })
end

#filenameObject

Returns the filename associated with the item that is being filtered. The returned filename is in the format “page <path> (rep <name>)”.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nanoc2/base/filter.rb', line 79

def filename
  if assigns[:layout]
    "layout #{assigns[:layout].path}"
  elsif assigns[:page]
    "page #{assigns[:_obj].path} (rep #{assigns[:_obj_rep].name})"
  elsif assigns[:asset]
    "asset #{assigns[:_obj].path} (rep #{assigns[:_obj_rep].name})"
  else
    '?'
  end
end

#run(content) ⇒ Object

Runs the filter. This method returns the filtered content.

content

The unprocessed content that should be filtered.

Subclasses must implement this method.

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/nanoc2/base/filter.rb', line 56

def run(content)
  raise NotImplementedError.new("Nanoc2::Filter subclasses must implement #run")
end