Class: Brandish::Processors::Common::Asset Abstract

Inherits:
Brandish::Processor::Base show all
Includes:
Brandish::Processor::Block, Brandish::Processor::Command, Paths
Defined in:
lib/brandish/processors/common/asset.rb,
lib/brandish/processors/common/asset/paths.rb

Overview

This class is abstract.

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

HTML::Script, HTML::Style

Defined Under Namespace

Modules: Paths

Instance Attribute Summary

Attributes inherited from Brandish::Processor::Base

#context

Class Method Summary collapse

Instance Method Summary collapse

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

included, #process_block

Methods included from Brandish::Processor::Command

included, #process_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.

Parameters:

  • name (::String)

    The name of the engine. This is used for the value of the "type" pair.

  • syntax (::Symbol)

    The syntax type for the engine. If it's :command, the element should be a command node. If it's :block, the element should be a block node. If it's :all or :_, it can be either.

  • symbol (::Symbol, nil) (defaults to: nil)

    The method to call to add the asset.



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.

Returns:

  • ({::String => (::Symbol, ::Proc<void>)})


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.

Returns:

  • (::String)


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

#performnil

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.

Returns:

  • (nil)


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