Class: Asciidoctor::Extensions::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/extensions.rb

Overview

The primary entry point into the extension system.

Registry holds the extensions which have been registered and activated, has methods for registering or defining a processor and looks up extensions stored in the registry during parsing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(groups = {}) ⇒ Registry

Returns a new instance of Registry.



735
736
737
738
# File 'lib/asciidoctor/extensions.rb', line 735

def initialize groups = {}
  @groups = groups
  reset
end

Instance Attribute Details

#documentObject (readonly)

Returns the Document on which the extensions in this registry are being used.



730
731
732
# File 'lib/asciidoctor/extensions.rb', line 730

def document
  @document
end

#groupsObject (readonly)

Returns the Hash of Group classes, instances, and/or Procs that have been registered with this registry.



733
734
735
# File 'lib/asciidoctor/extensions.rb', line 733

def groups
  @groups
end

Instance Method Details

#activate(document) ⇒ Registry

Activates all the global extension Groups and the extension Groups associated with this registry.

Parameters:

  • document

    the Document on which the extensions are to be used.

Returns:

  • (Registry)

    Returns the instance of this Registry.



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/asciidoctor/extensions.rb', line 746

def activate document
  reset if @document
  @document = document
  unless (ext_groups = Extensions.groups.values + @groups.values).empty?
    ext_groups.each do |group|
      case group
      when ::Proc
        case group.arity
        when 0, -1
          instance_exec(&group)
        when 1
          group.call self
        end
      when ::Class
        group.new.activate self
      else
        group.activate self
      end
    end
  end
  self
end

#block(*args, &block) ⇒ Extension

Registers a BlockProcessor with the extension registry to process the block content (i.e., delimited block or paragraph) in the AsciiDoc source annotated with the specified block name (i.e., style).

The BlockProcessor may be one of four types:

  • A BlockProcessor subclass

  • An instance of a BlockProcessor subclass

  • The String name of a BlockProcessor subclass

  • A method block (i.e., Proc) that conforms to the BlockProcessor contract

Unless the BlockProcessor is passed as the method block, it must be the first argument to this method. The second argument is the name (coersed to a Symbol) of the AsciiDoc block content (i.e., delimited block or paragraph) that this processor is registered to handle. If a block name is not passed as an argument, it gets read from the name property of the BlockProcessor instance. If a name still cannot be determined, an error is raised.

Examples:

# as a BlockProcessor subclass
block ShoutBlock
# as a BlockProcessor subclass with an explicit block name
block ShoutBlock, :shout
# as an instance of a BlockProcessor subclass
block ShoutBlock.new
# as an instance of a BlockProcessor subclass with an explicit block name
block ShoutBlock.new, :shout
# as a name of a BlockProcessor subclass
block 'ShoutBlock'
# as a name of a BlockProcessor subclass with an explicit block name
block 'ShoutBlock', :shout
# as a method block
block do
  named :shout
  process do |parent, reader, attrs|
    ...
  end
end
# as a method block with an explicit block name
block :shout do
  process do |parent, reader, attrs|
    ...
  end
end

Returns:

  • (Extension)

    Returns an instance of the Extension proxy object that is stored in the registry and manages the instance of this BlockProcessor.



1103
1104
1105
# File 'lib/asciidoctor/extensions.rb', line 1103

def block *args, &block
  add_syntax_processor :block, args, &block
end

#block_macro(*args, &block) ⇒ Extension

Registers a BlockMacroProcessor with the extension registry to process a block macro with the specified name.

The BlockMacroProcessor may be one of four types:

  • A BlockMacroProcessor subclass

  • An instance of a BlockMacroProcessor subclass

  • The String name of a BlockMacroProcessor subclass

  • A method block (i.e., Proc) that conforms to the BlockMacroProcessor contract

Unless the BlockMacroProcessor is passed as the method block, it must be the first argument to this method. The second argument is the name (coersed to a Symbol) of the AsciiDoc block macro that this processor is registered to handle. If a block macro name is not passed as an argument, it gets read from the name property of the BlockMacroProcessor instance. If a name still cannot be determined, an error is raised.

Examples:

# as a BlockMacroProcessor subclass
block_macro GistBlockMacro
# as a BlockMacroProcessor subclass with an explicit macro name
block_macro GistBlockMacro, :gist
# as an instance of a BlockMacroProcessor subclass
block_macro GistBlockMacro.new
# as an instance of a BlockMacroProcessor subclass with an explicit macro name
block_macro GistBlockMacro.new, :gist
# as a name of a BlockMacroProcessor subclass
block_macro 'GistBlockMacro'
# as a name of a BlockMacroProcessor subclass with an explicit macro name
block_macro 'GistBlockMacro', :gist
# as a method block
block_macro do
  named :gist
  process do |parent, target, attrs|
    ...
  end
end
# as a method block with an explicit macro name
block_macro :gist do
  process do |parent, target, attrs|
    ...
  end
end

Returns:

  • (Extension)

    Returns an instance of the Extension proxy object that is stored in the registry and manages the instance of this BlockMacroProcessor.



1188
1189
1190
# File 'lib/asciidoctor/extensions.rb', line 1188

def block_macro *args, &block
  add_syntax_processor :block_macro, args, &block
end

#block_macros?Boolean

Checks whether any BlockMacroProcessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any BlockMacroProcessor extensions are registered.



1195
1196
1197
# File 'lib/asciidoctor/extensions.rb', line 1195

def block_macros?
  !!@block_macro_extensions
end

#blocks?Boolean

Checks whether any BlockProcessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any BlockProcessor extensions are registered.



1110
1111
1112
# File 'lib/asciidoctor/extensions.rb', line 1110

def blocks?
  !!@block_extensions
end

#docinfo_processor(*args, &block) ⇒ Extension

Registers an DocinfoProcessor with the extension registry to add additional docinfo to the document.

The DocinfoProcessor may be one of four types:

  • A DocinfoProcessor subclass

  • An instance of a DocinfoProcessor subclass

  • The String name of a DocinfoProcessor subclass

  • A method block (i.e., Proc) that conforms to the DocinfoProcessor contract

Unless the DocinfoProcessor is passed as the method block, it must be the first argument to this method.

Examples:

# as an DocinfoProcessor subclass
docinfo_processor MetaRobotsDocinfoProcessor
# as an instance of a DocinfoProcessor subclass with an explicit location
docinfo_processor JQueryDocinfoProcessor.new, location: :footer
# as a name of a DocinfoProcessor subclass
docinfo_processor 'MetaRobotsDocinfoProcessor'
# as a method block
docinfo_processor do
  process do |doc|
    at_location :footer
    'footer content'
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this DocinfoProcessor.



1016
1017
1018
# File 'lib/asciidoctor/extensions.rb', line 1016

def docinfo_processor *args, &block
  add_document_processor :docinfo_processor, args, &block
end

#docinfo_processors(location = nil) ⇒ Array

Retrieves the Extension proxy objects for all the DocinfoProcessor instances stored in this registry.

Parameters:

  • location (defaults to: nil)

    A Symbol for selecting docinfo extensions at a given location (:head or :footer) (default: nil)

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



1043
1044
1045
# File 'lib/asciidoctor/extensions.rb', line 1043

def docinfo_processors location = nil
  @docinfo_processor_extensions && location ? @docinfo_processor_extensions.select {|ext| ext.config[:location] == location } : (@docinfo_processor_extensions || [])
end

#docinfo_processors?(location = nil) ⇒ Boolean

Checks whether any DocinfoProcessor extensions have been registered.

Parameters:

  • location (defaults to: nil)

    A Symbol for selecting docinfo extensions at a given location (:head or :footer) (default: nil)

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any DocinfoProcessor extensions are registered.



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/asciidoctor/extensions.rb', line 1025

def docinfo_processors? location = nil
  if @docinfo_processor_extensions
    if location
      @docinfo_processor_extensions.any? {|ext| ext.config[:location] == location }
    else
      true
    end
  else
    false
  end
end

#find_block_extension(name) ⇒ Extension

Retrieves the Extension proxy object for the BlockProcessor registered to handle block content with the name.

Parameters:

  • name

    the String or Symbol (coersed to a Symbol) macro name

Returns:

  • (Extension)

    Returns the Extension object stored in the registry that proxies the corresponding BlockProcessor or nil if a match is not found.



1130
1131
1132
# File 'lib/asciidoctor/extensions.rb', line 1130

def find_block_extension name
  @block_extensions&.[] name.to_sym
end

#find_block_macro_extension(name) ⇒ Extension

Retrieves the Extension proxy object for the BlockMacroProcessor registered to handle a block macro with the specified name.

Parameters:

  • name

    the String or Symbol (coersed to a Symbol) macro name

Returns:

  • (Extension)

    Returns the Extension object stored in the registry that proxies the corresponding BlockMacroProcessor or nil if a match is not found.



1219
1220
1221
# File 'lib/asciidoctor/extensions.rb', line 1219

def find_block_macro_extension name
  @block_macro_extensions&.[] name.to_sym
end

#find_inline_macro_extension(name) ⇒ Extension

Retrieves the Extension proxy object for the InlineMacroProcessor registered to handle an inline macro with the specified name.

Parameters:

  • name

    the String or Symbol (coersed to a Symbol) macro name

Returns:

  • (Extension)

    Returns the Extension object stored in the registry that proxies the corresponding InlineMacroProcessor or nil if a match is not found.



1306
1307
1308
# File 'lib/asciidoctor/extensions.rb', line 1306

def find_inline_macro_extension name
  @inline_macro_extensions&.[] name.to_sym
end

#include_processor(*args, &block) ⇒ Extension

Registers an IncludeProcessor with the extension registry to have a shot at handling the include directive.

The IncludeProcessor may be one of four types:

  • A IncludeProcessor subclass

  • An instance of a IncludeProcessor subclass

  • The String name of a IncludeProcessor subclass

  • A method block (i.e., Proc) that conforms to the IncludeProcessor contract

Unless the IncludeProcessor is passed as the method block, it must be the first argument to this method.

Examples:

# as an IncludeProcessor subclass
include_processor GitIncludeProcessor
# as an instance of a Postprocessor subclass
include_processor GitIncludeProcessor.new
# as a name of a Postprocessor subclass
include_processor 'GitIncludeProcessor'
# as a method block
include_processor do
  process do |document, output|
    ...
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this IncludeProcessor.



963
964
965
# File 'lib/asciidoctor/extensions.rb', line 963

def include_processor *args, &block
  add_document_processor :include_processor, args, &block
end

#include_processorsArray

Retrieves the Extension proxy objects for all the IncludeProcessor instances stored in this registry.

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



978
979
980
# File 'lib/asciidoctor/extensions.rb', line 978

def include_processors
  @include_processor_extensions
end

#include_processors?Boolean

Checks whether any IncludeProcessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any IncludeProcessor extensions are registered.



970
971
972
# File 'lib/asciidoctor/extensions.rb', line 970

def include_processors?
  !!@include_processor_extensions
end

#inline_macro(*args, &block) ⇒ Extension

Registers a InlineMacroProcessor with the extension registry to process an inline macro with the specified name.

The InlineMacroProcessor may be one of four types:

  • An InlineMacroProcessor subclass

  • An instance of an InlineMacroProcessor subclass

  • The String name of an InlineMacroProcessor subclass

  • A method block (i.e., Proc) that conforms to the InlineMacroProcessor contract

Unless the InlineMacroProcessor is passed as the method block, it must be the first argument to this method. The second argument is the name (coersed to a Symbol) of the AsciiDoc block macro that this processor is registered to handle. If a block macro name is not passed as an argument, it gets read from the name property of the InlineMacroProcessor instance. If a name still cannot be determined, an error is raised.

Examples:

# as an InlineMacroProcessor subclass
inline_macro ChromeInlineMacro
# as an InlineMacroProcessor subclass with an explicit macro name
inline_macro ChromeInlineMacro, :chrome
# as an instance of an InlineMacroProcessor subclass
inline_macro ChromeInlineMacro.new
# as an instance of an InlineMacroProcessor subclass with an explicit macro name
inline_macro ChromeInlineMacro.new, :chrome
# as a name of an InlineMacroProcessor subclass
inline_macro 'ChromeInlineMacro'
# as a name of an InlineMacroProcessor subclass with an explicit macro name
inline_macro 'ChromeInlineMacro', :chrome
# as a method block
inline_macro do
  named :chrome
  process do |parent, target, attrs|
    ...
  end
end
# as a method block with an explicit macro name
inline_macro :chrome do
  process do |parent, target, attrs|
    ...
  end
end

Returns:

  • (Extension)

    Returns an instance of the Extension proxy object that is stored in the registry and manages the instance of this InlineMacroProcessor.



1277
1278
1279
# File 'lib/asciidoctor/extensions.rb', line 1277

def inline_macro *args, &block
  add_syntax_processor :inline_macro, args, &block
end

#inline_macrosArray

Retrieves the Extension proxy objects for all InlineMacroProcessor instances in this registry.

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



1314
1315
1316
# File 'lib/asciidoctor/extensions.rb', line 1314

def inline_macros
  (@inline_macro_extensions || {}).values
end

#inline_macros?Boolean

Checks whether any InlineMacroProcessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any IncludeMacroProcessor extensions are registered.



1284
1285
1286
# File 'lib/asciidoctor/extensions.rb', line 1284

def inline_macros?
  !!@inline_macro_extensions
end

#postprocessor(*args, &block) ⇒ Extension

Registers a Postprocessor with the extension registry to process the output after conversion is complete.

The Postprocessor may be one of four types:

  • A Postprocessor subclass

  • An instance of a Postprocessor subclass

  • The String name of a Postprocessor subclass

  • A method block (i.e., Proc) that conforms to the Postprocessor contract

Unless the Postprocessor is passed as the method block, it must be the first argument to this method.

Examples:

# as a Postprocessor subclass
postprocessor AnalyticsPostprocessor
# as an instance of a Postprocessor subclass
postprocessor AnalyticsPostprocessor.new
# as a name of a Postprocessor subclass
postprocessor 'AnalyticsPostprocessor'
# as a method block
postprocessor do
  process do |document, output|
    ...
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this Postprocessor.



911
912
913
# File 'lib/asciidoctor/extensions.rb', line 911

def postprocessor *args, &block
  add_document_processor :postprocessor, args, &block
end

#postprocessorsArray

Retrieves the Extension proxy objects for all Postprocessor instances in this registry.

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



926
927
928
# File 'lib/asciidoctor/extensions.rb', line 926

def postprocessors
  @postprocessor_extensions
end

#postprocessors?Boolean

Checks whether any Postprocessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any Postprocessor extensions are registered.



918
919
920
# File 'lib/asciidoctor/extensions.rb', line 918

def postprocessors?
  !!@postprocessor_extensions
end

#prefer(*args, &block) ⇒ Extension

Inserts the document processor Extension instance as the first processor of its kind in the extension registry.

Examples:

prefer :include_processor do
  process do |document, reader, target, attrs|
    ...
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this processor.



1331
1332
1333
1334
1335
1336
# File 'lib/asciidoctor/extensions.rb', line 1331

def prefer *args, &block
  extension = ProcessorExtension === (arg0 = args.shift) ? arg0 : (send arg0, *args, &block)
  extensions_store = instance_variable_get %(@#{extension.kind}_extensions).to_sym
  extensions_store.unshift extensions_store.delete extension
  extension
end

#preprocessor(*args, &block) ⇒ Extension

Registers a Preprocessor with the extension registry to process the AsciiDoc source before parsing begins.

The Preprocessor may be one of four types:

  • A Preprocessor subclass

  • An instance of a Preprocessor subclass

  • The String name of a Preprocessor subclass

  • A method block (i.e., Proc) that conforms to the Preprocessor contract

Unless the Preprocessor is passed as the method block, it must be the first argument to this method.

Examples:

# as a Preprocessor subclass
preprocessor FrontMatterPreprocessor
# as an instance of a Preprocessor subclass
preprocessor FrontMatterPreprocessor.new
# as a name of a Preprocessor subclass
preprocessor 'FrontMatterPreprocessor'
# as a method block
preprocessor do
  process do |doc, reader|
    ...
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this Preprocessor.



802
803
804
# File 'lib/asciidoctor/extensions.rb', line 802

def preprocessor *args, &block
  add_document_processor :preprocessor, args, &block
end

#preprocessorsArray

Retrieves the Extension proxy objects for all Preprocessor instances in this registry.

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



817
818
819
# File 'lib/asciidoctor/extensions.rb', line 817

def preprocessors
  @preprocessor_extensions
end

#preprocessors?Boolean

Checks whether any Preprocessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any Preprocessor extensions are registered.



809
810
811
# File 'lib/asciidoctor/extensions.rb', line 809

def preprocessors?
  !!@preprocessor_extensions
end

#registered_for_block?(name, context) ⇒ Extension

Checks whether any BlockProcessor extensions are registered to handle the specified block name appearing on the specified context.

Returns:

  • (Extension)

    Returns the Extension proxy object for the BlockProcessor that matches the block name and context or false if no match is found.



1119
1120
1121
# File 'lib/asciidoctor/extensions.rb', line 1119

def registered_for_block? name, context
  (ext = @block_extensions&.[] name.to_sym) ? (ext.config[:contexts].include? context) && ext : false
end

#registered_for_block_macro?(name) ⇒ Extension

Checks whether any BlockMacroProcessor extensions are registered to handle the block macro with the specified name.

Parameters:

  • name

    the String or Symbol (coersed to a Symbol) macro name

Returns:

  • (Extension)

    Returns the Extension proxy object for the BlockMacroProcessor that matches the macro name or false if no match is found. – TODO only allow blank target if format is :short



1208
1209
1210
# File 'lib/asciidoctor/extensions.rb', line 1208

def registered_for_block_macro? name
  (@block_macro_extensions&.[] name.to_sym) || false
end

#registered_for_inline_macro?(name) ⇒ Extension

Checks whether any InlineMacroProcessor extensions are registered to handle the inline macro with the specified name.

Parameters:

  • name

    the String or Symbol (coersed to a Symbol) macro name

Returns:

  • (Extension)

    Returns the Extension proxy object for the InlineMacroProcessor that matches the macro name or false if no match is found.



1295
1296
1297
# File 'lib/asciidoctor/extensions.rb', line 1295

def registered_for_inline_macro? name
  (@inline_macro_extensions&.[] name.to_sym) || false
end

#tree_processor(*args, &block) ⇒ Extension Also known as: treeprocessor

Registers a TreeProcessor with the extension registry to process the AsciiDoc source after parsing is complete.

The TreeProcessor may be one of four types:

  • A TreeProcessor subclass

  • An instance of a TreeProcessor subclass

  • The String name of a TreeProcessor subclass

  • A method block (i.e., Proc) that conforms to the TreeProcessor contract

Unless the TreeProcessor is passed as the method block, it must be the first argument to this method.

Examples:

# as a TreeProcessor subclass
tree_processor ShellTreeProcessor
# as an instance of a TreeProcessor subclass
tree_processor ShellTreeProcessor.new
# as a name of a TreeProcessor subclass
tree_processor 'ShellTreeProcessor'
# as a method block
tree_processor do
  process do |document|
    ...
  end
end

Returns:

  • (Extension)

    Returns the Extension stored in the registry that proxies the instance of this TreeProcessor.



854
855
856
# File 'lib/asciidoctor/extensions.rb', line 854

def tree_processor *args, &block
  add_document_processor :tree_processor, args, &block
end

#tree_processorsArray Also known as: treeprocessors

Retrieves the Extension proxy objects for all TreeProcessor instances in this registry.

Returns:

  • (Array)

    Returns an Array of Extension proxy objects.



869
870
871
# File 'lib/asciidoctor/extensions.rb', line 869

def tree_processors
  @tree_processor_extensions
end

#tree_processors?Boolean Also known as: treeprocessors?

Checks whether any TreeProcessor extensions have been registered.

Returns:

  • (Boolean)

    Returns a Boolean indicating whether any TreeProcessor extensions are registered.



861
862
863
# File 'lib/asciidoctor/extensions.rb', line 861

def tree_processors?
  !!@tree_processor_extensions
end