class Asciidoctor::Extensions::Processor

Public: An abstract base class for document and syntax processors.

This class provides access to a class-level Hash for holding default configuration options defined using the {Processor.option} method. This style of default configuration is specific to the native Ruby environment and is only consulted inside the initializer. An overriding configuration Hash can be passed to the initializer. Once the processor is initialized, the configuration is accessed using the {Processor#config} instance variable.

Instances of the Processor class provide convenience methods for creating AST nodes, such as Block and Inline, and for parsing child content.

Attributes

config[R]

Public: Get the configuration Hash for this processor instance.

Public Class Methods

config() click to toggle source

Public: Get the static configuration for this processor class.

Returns a configuration [Hash]

# File lib/asciidoctor/extensions.rb, line 40
def config
  @config ||= {}
end
extend_dsl()
Alias for: use_dsl
include_dsl()
Alias for: use_dsl
new(config = {}) click to toggle source
# File lib/asciidoctor/extensions.rb, line 82
def initialize config = {}
  @config = self.class.config.merge config
end
option(key, default_value) click to toggle source

Public: Assigns a default value for the specified option that gets applied to all instances of this processor.

Examples

option :contexts, [:open, :paragraph]

Returns nothing

# File lib/asciidoctor/extensions.rb, line 52
def option key, default_value
  config[key] = default_value
end
use_dsl() click to toggle source

Include the DSL class for this processor into this processor class or instance.

This method automatically detects whether to use the include or extend keyword based on what is appropriate.

NOTE Inspiration for this DSL design comes from corcoran.io/2013/09/04/simple-pattern-ruby-dsl/

Returns nothing

# File lib/asciidoctor/extensions.rb, line 64
def use_dsl
  if self.name.nil_or_empty?
    # NOTE contants(false) doesn't exist in Ruby 1.8.7
    #include const_get :DSL if constants(false).grep :DSL
    include const_get :DSL if constants.grep :DSL
  else
    # NOTE contants(false) doesn't exist in Ruby 1.8.7
    #extend const_get :DSL if constants(false).grep :DSL
    extend const_get :DSL if constants.grep :DSL
  end
end
Also aliased as: extend_dsl, include_dsl

Public Instance Methods

create_block(parent, context, source, attrs, opts = {}) click to toggle source
# File lib/asciidoctor/extensions.rb, line 145
def create_block parent, context, source, attrs, opts = {}
  Block.new parent, context, { :source => source, :attributes => attrs }.merge(opts)
end
create_image_block(parent, attrs, opts = {}) click to toggle source
# File lib/asciidoctor/extensions.rb, line 149
def create_image_block parent, attrs, opts = {}
  create_block parent, :image, nil, attrs, opts
end
create_inline(parent, context, text, opts = {}) click to toggle source
# File lib/asciidoctor/extensions.rb, line 153
def create_inline parent, context, text, opts = {}
  Inline.new parent, context, text, opts
end
create_section(parent, title, attrs, opts = {}) click to toggle source

Public: Creates a new Section node.

Creates a Section node in the same manner as the parser.

parent - The parent Section (or Document) of this new Section. title - The String title of the new Section. attrs - A Hash of attributes to control how the section is built.

Use the style attribute to set the name of a special section (ex. appendix).
Use the id attribute to assign an explicit ID or set the value to false to
disable automatic ID generation (when sectids document attribute is set).

opts - An optional Hash of options (default: {}):

:level    - [Integer] The level to assign to this section; defaults to
            one greater than the parent level (optional).
:numbered - [Boolean] A flag to force numbering, which falls back to the 
            state of the sectnums document attribute (optional).

Returns a [Section] node with all properties properly initialized.

# File lib/asciidoctor/extensions.rb, line 113
def create_section parent, title, attrs, opts = {}
  doc = parent.document
  doctype, level = doc.doctype, (opts[:level] || parent.level + 1)
  if (style = attrs.delete 'style')
    if style == 'abstract' && doctype == 'book'
      sectname, level = 'chapter', 1
    else
      sectname, special = style, true
      level = 1 if level == 0
    end
  elsif doctype == 'book'
    sectname = level == 0 ? 'part' : (level == 1 ? 'chapter' : 'section')
  elsif doctype == 'manpage' && (title.casecmp 'synopsis') == 0
    sectname, special = 'synopsis', true
  else
    sectname = 'section'
  end
  sect = Section.new parent, level, false
  sect.title, sect.sectname = title, sectname
  if special
    sect.special = true
    sect.numbered = true if opts.fetch :numbered, (style == 'appendix')
  elsif opts.fetch :numbered, (level > 0 && (doc.attributes.key? 'sectnums'))
    sect.numbered = sect.special ? (parent.context == :section && parent.numbered) : true
  end
  unless (id = attrs.delete 'id') == false
    sect.id = attrs['id'] = id || ((doc.attributes.key? 'sectids') ? (Section.generate_id sect.title, doc) : nil)
  end
  sect.update_attributes attrs
  sect
end
parse_content(parent, content, attributes = nil) click to toggle source

Public: Parses blocks in the content and attaches the block to the parent.

Returns The parent node into which the blocks are parsed.

# File lib/asciidoctor/extensions.rb, line 162
def parse_content parent, content, attributes = nil
  reader = Reader === content ? content : (Reader.new content)
  while ((block = Parser.next_block reader, parent, (attributes ? attributes.dup : {})) && parent << block) ||
      reader.has_more_lines?
  end
  parent
end
process(*args) click to toggle source
# File lib/asciidoctor/extensions.rb, line 90
def process *args
  raise ::NotImplementedError, %(Asciidoctor::Extensions::Processor subclass must implement ##{__method__} method)
end
update_config(config) click to toggle source
# File lib/asciidoctor/extensions.rb, line 86
def update_config config
  @config.update config
end