Class: Brandish::Processors::Common::Asset Abstract
- Inherits:
-
Brandish::Processor::Base
- Object
- Brandish::Processor::Base
- Brandish::Processors::Common::Asset
- Defined in:
- lib/brandish/processors/common/asset.rb,
lib/brandish/processors/common/asset/paths.rb
Overview
Implement engines using Asset.engine and register the processor using Brandish::Processor::Base.register.
Allows assets to be included in the documents. This is similar to the markup processor in that it uses a concept of engines in order to power different kinds of assets.
Options:
:asset_load_paths
- Optional. The load paths for the asset processor. These are used to resolve the asset file names, and are passed directly to the Brandish::PathSet created.
Pairs:
"src"
,"file"
,"name"
, or"link"
- Required. At least one of these options are required. They all perform the same function. This defines the name or path of the asset to add or process."type"
- Required. The type of the asset to process. This defines how the asset is handled.
Direct Known Subclasses
Defined Under Namespace
Modules: Paths
Instance Attribute Summary
Attributes inherited from Brandish::Processor::Base
Class Method Summary collapse
-
.engine(name, syntax, symbol = nil, &block) ⇒ void
private
Defines an engine for use on the subclass.
-
.engines ⇒ {::String => (::Symbol, ::Proc<void>)}
private
The engines defined for the subclass.
Instance Method Summary collapse
-
#load_asset_file ⇒ ::String
Finds the value for the asset file.
-
#perform ⇒ nil
Adds the asset.
Methods included from Paths
#asset_kind_extension, #asset_kind_path, #asset_load_paths, #load_file_paths, #output_assets_path, #uri_path
Methods included from Brandish::Processor::Block
Methods included from Brandish::Processor::Command
Methods inherited from Brandish::Processor::Base
#accept, #call, #initialize, #postprocess, #process_block, #process_command, #process_root, #process_text, register, #setup
Constructor Details
This class inherits a constructor from Brandish::Processor::Base
Class Method Details
.engine(name, syntax, symbol = nil, &block) ⇒ 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.
Defines an engine for use on the subclass. This should not be used on the parent class (Brandish::Processors::Common::Asset). This takes the name of the engine, the syntax for the engine, and the processor to perform the asset processor.
If both a third argument and a block are provided, then the block takes precedence.
67 68 69 70 |
# File 'lib/brandish/processors/common/asset.rb', line 67 def self.engine(name, syntax, symbol = nil, &block) block ||= proc { send(symbol) } engines[name.to_s] = [syntax, block] end |
.engines ⇒ {::String => (::Symbol, ::Proc<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.
The engines defined for the subclass. This should not be used on the parent class (Brandish::Processors::Common::Asset). This returns a key-value pair for the engines. The key is the "name" of the format; this is used for the `"type" pair. The value is a tuple containing two values: the syntax of the block/command options, and a proc that takes no arguments to include the asset.
45 46 47 |
# File 'lib/brandish/processors/common/asset.rb', line 45 def self.engines @_engines ||= {} end |
Instance Method Details
#load_asset_file ⇒ ::String
Finds the value for the asset file. This tries four different
pairs before giving up: "src"
, "file"
, "name"
, and "link"
.
If none of these could be found, it fails.
93 94 95 96 97 98 99 |
# File 'lib/brandish/processors/common/asset.rb', line 93 def load_asset_file file = @pairs["src"] || @pairs["file"] || @pairs["name"] || @pairs["link"] return file if file fail PairError.new("Expected one of src, file, name, or link, " \ "got nothing", @node.location) end |
#perform ⇒ nil
Adds the asset. If the engine cannot be found, it returns
nil
, effectively ignoring the node. This helps enable cross-format
support without the use of if
nodes. This always returns nil
,
because outputting the asset is up to the Output
processor for the inclusion of the asset.
79 80 81 82 83 84 85 86 |
# File 'lib/brandish/processors/common/asset.rb', line 79 def perform return unless (engine = find_engine) syntax, block = engine fail_syntax_error(syntax) if syntax == :block && !@body || syntax == :command && @body instance_exec(&block) nil end |