Class: Nanoc::Filter Abstract
- Inherits:
-
Int::Context
- Object
- Int::Context
- Nanoc::Filter
- Extended by:
- Int::PluginRegistry::PluginMethods
- Defined in:
- lib/nanoc/base/compilation/filter.rb
Overview
Subclass and override #run to implement a custom filter.
Nanoc::Filter is responsible for filtering items. It is the superclass for all textual filters.
A filter instance should only be used once. Filters should not be reused since they store state.
When creating a filter with a hash containing assigned variables, those variables will be made available in the ‘@assigns` instance variable and the #assigns method. The assigns itself will also be available as instance variables and instance methods.
Direct Known Subclasses
Nanoc::Filters::AsciiDoc, Nanoc::Filters::BlueCloth, Nanoc::Filters::CoffeeScript, Nanoc::Filters::ColorizeSyntax, Nanoc::Filters::ERB, Nanoc::Filters::Erubis, Nanoc::Filters::Haml, Nanoc::Filters::Handlebars, Nanoc::Filters::Kramdown, Nanoc::Filters::Less, Nanoc::Filters::Markaby, Nanoc::Filters::Maruku, Nanoc::Filters::Mustache, Nanoc::Filters::Pandoc, Nanoc::Filters::RDiscount, Nanoc::Filters::RDoc, Nanoc::Filters::Rainpress, Nanoc::Filters::RedCloth, Nanoc::Filters::Redcarpet, Nanoc::Filters::RelativizePaths, Nanoc::Filters::RubyPants, Nanoc::Filters::Sass, Nanoc::Filters::Slim, Nanoc::Filters::Typogruby, Nanoc::Filters::UglifyJS, Nanoc::Filters::XSL, Nanoc::Filters::YUICompressor
Constant Summary collapse
- TMP_BINARY_ITEMS_DIR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'binary_items'.freeze
Instance Attribute Summary collapse
-
#assigns ⇒ Hash
readonly
private
A hash containing variables that will be made available during filtering.
Class Method Summary collapse
-
.from_binary? ⇒ Boolean
private
True if this filter can be applied to binary item representations, false otherwise.
- .requires(*requires) ⇒ Object
-
.setup ⇒ void
private
Requires the filter’s required library if necessary.
-
.to_binary? ⇒ Boolean
private
True if this filter results in a binary item representation, false otherwise.
-
.type(arg) ⇒ void
Sets the new type for the filter.
Instance Method Summary collapse
-
#depend_on(items) ⇒ void
Creates a dependency from the item that is currently being filtered onto the given collection of items.
-
#filename ⇒ String
private
Returns the filename associated with the item that is being filtered.
-
#initialize(hash = {}) ⇒ Filter
constructor
private
Creates a new filter that has access to the given assigns.
-
#output_filename ⇒ String
Returns a filename that is used to write data to.
-
#run(content_or_filename, params = {}) ⇒ String, void
abstract
Runs the filter on the given content or filename.
-
#setup_and_run(*args) ⇒ Object
private
Sets up the filter and runs the filter.
Methods included from Int::PluginRegistry::PluginMethods
all, identifier, identifiers, named, register
Methods inherited from Int::Context
Constructor Details
#initialize(hash = {}) ⇒ Filter
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 filter that has access to the given assigns.
117 118 119 120 |
# File 'lib/nanoc/base/compilation/filter.rb', line 117 def initialize(hash = {}) @assigns = hash super end |
Instance Attribute Details
#assigns ⇒ Hash (readonly)
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.
A hash containing variables that will be made available during filtering.
36 37 38 |
# File 'lib/nanoc/base/compilation/filter.rb', line 36 def assigns @assigns end |
Class Method Details
.from_binary? ⇒ Boolean
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.
Returns True if this filter can be applied to binary item representations, false otherwise.
71 72 73 |
# File 'lib/nanoc/base/compilation/filter.rb', line 71 def from_binary? (@from || :text) == :binary end |
.requires(*requires) ⇒ void .requires ⇒ Enumerable<String>
90 91 92 93 94 95 96 |
# File 'lib/nanoc/base/compilation/filter.rb', line 90 def requires(*requires) if requires.any? @requires = requires else @requires || [] end end |
.setup ⇒ 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.
Requires the filter’s required library if necessary.
103 104 105 106 107 108 |
# File 'lib/nanoc/base/compilation/filter.rb', line 103 def setup @setup ||= begin requires.each { |r| require r } true end end |
.to_binary? ⇒ Boolean
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.
Returns True if this filter results in a binary item representation, false otherwise.
79 80 81 |
# File 'lib/nanoc/base/compilation/filter.rb', line 79 def to_binary? (@to || :text) == :binary end |
.type(arg) ⇒ void
This method returns an undefined value.
Sets the new type for the filter. The type can be ‘:binary` (default) or `:text`. The given argument can either be a symbol indicating both “from” and “to” types, or a hash where the only key is the “from” type and the only value is the “to” type.
57 58 59 60 61 62 63 64 65 |
# File 'lib/nanoc/base/compilation/filter.rb', line 57 def type(arg) if arg.is_a?(Hash) @from = arg.keys[0] @to = arg.values[0] else @from = arg @to = arg end end |
Instance Method Details
#depend_on(items) ⇒ void
This method returns an undefined value.
Creates a dependency from the item that is currently being filtered onto the given collection of items. In other words, require the given items to be compiled first before this items is processed.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/nanoc/base/compilation/filter.rb', line 185 def depend_on(items) orig_items = items items = items.map { |i| i.is_a?(Nanoc::ItemWithRepsView) ? i.unwrap : i } # Notify dependency_tracker = @assigns[:item]._context.dependency_tracker items.each { |item| dependency_tracker.bounce(item) } # Raise unmet dependency error if necessary items.each do |item| rep = orig_items.sample._context.reps[item].find { |r| !r.compiled? } raise Nanoc::Int::Errors::UnmetDependency.new(rep) if rep end end |
#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.
Returns the filename associated with the item that is being filtered.
It is in the format `item <identifier> (rep <name>)`.
170 171 172 173 174 175 176 177 178 |
# File 'lib/nanoc/base/compilation/filter.rb', line 170 def filename if assigns[:layout] "layout #{assigns[:layout].identifier}" elsif assigns[:item] "item #{assigns[:item].identifier} (rep #{assigns[:item_rep].name})" else '?' end end |
#output_filename ⇒ String
Returns a filename that is used to write data to. This method is only
used on binary items. When running a binary filter on a file, the
resulting file must end up in the location returned by this method.
The returned filename will be absolute, so it is safe to change to
another directory inside the filter.
159 160 161 162 |
# File 'lib/nanoc/base/compilation/filter.rb', line 159 def output_filename @output_filename ||= Nanoc::Int::TempFilenameFactory.instance.create(TMP_BINARY_ITEMS_DIR) end |
#run(content_or_filename, params = {}) ⇒ String, void
Runs the filter on the given content or filename.
147 148 149 |
# File 'lib/nanoc/base/compilation/filter.rb', line 147 def run(content_or_filename, params = {}) # rubocop:disable Lint/UnusedMethodArgument raise NotImplementedError.new('Nanoc::Filter subclasses must implement #run') end |
#setup_and_run(*args) ⇒ Object
128 129 130 131 |
# File 'lib/nanoc/base/compilation/filter.rb', line 128 def setup_and_run(*args) self.class.setup run(*args) end |