Module: Asciidoctor::Converter

Extended by:
DefaultFactory
Included in:
Base
Defined in:
lib/asciidoctor/converter.rb

Overview

A module for defining converters that are used to convert AbstractNode objects in a parsed AsciiDoc document to an output (aka backend) format such as HTML or DocBook.

A Converter is typically instantiated each time an AsciiDoc document is processed (i.e., parsed and converted). Implementing a custom converter entails:

  • Including the Converter module in a converter class and implementing the #convert method or extending the Base class and implementing the dispatch methods that map to each node.

  • Optionally registering the converter with one or more backend names statically using the register_for DSL method contributed by the Config module.

Examples:

class TextConverter
  include Asciidoctor::Converter
  register_for 'text'
  def initialize *args
    super
    outfilesuffix '.txt'
  end
  def convert node, transform = node.node_name, opts = nil
    case transform
    when 'document', 'section'
      [node.title, node.content].join %(\n\n)
    when 'paragraph'
      (node.content.tr ?\n, ' ') << ?\n
    else
      (transform.start_with? 'inline_') ? node.text : node.content
    end
  end
end
puts Asciidoctor.convert_file 'sample.adoc', backend: :text, safe: :safe
class Html5Converter < (Asciidoctor::Converter.for 'html5')
  register_for 'html5'
  def convert_paragraph node
    %(<p>#{node.content}</p>)
  end
end
puts Asciidoctor.convert_file 'sample.adoc', safe: :safe

Defined Under Namespace

Modules: BackendTraits, Config, DefaultFactory, Factory Classes: Base, CompositeConverter, CustomFactory, DefaultFactoryProxy, DocBook5Converter, Html5Converter, ManPageConverter, TemplateConverter

Constant Summary

Constants included from DefaultFactory

DefaultFactory::PROVIDED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultFactory

for, register, unregister_all

Methods included from Factory

#converters, #create, create, default, #for, new, #register

Instance Attribute Details

#backendObject (readonly)

The String backend name that this converter is handling.


48
49
50
# File 'lib/asciidoctor/converter.rb', line 48

def backend
  @backend
end

Class Method Details

.derive_backend_traits(backend, basebackend = nil) ⇒ Hash

Derive backend traits (basebackend, filetype, outfilesuffix, htmlsyntax) from the given backend.

Parameters:

  • backend

    the String backend from which to derive the traits

  • basebackend (defaults to: nil)

    the String basebackend to use in favor of deriving one from the backend (optional, default: nil)

Returns:

  • (Hash)

    Returns the backend traits for the given backend as a Hash.


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asciidoctor/converter.rb', line 91

def self.derive_backend_traits backend, basebackend = nil
  return {} unless backend
  if (outfilesuffix = DEFAULT_EXTENSIONS[(basebackend ||= backend.sub TrailingDigitsRx, '')])
    filetype = outfilesuffix.slice 1, outfilesuffix.length
  else
    outfilesuffix = %(.#{filetype = basebackend})
  end
  filetype == 'html' ?
    { basebackend: basebackend, filetype: filetype, htmlsyntax: 'html', outfilesuffix: outfilesuffix } :
    { basebackend: basebackend, filetype: filetype, outfilesuffix: outfilesuffix }
end

Instance Method Details

#convert(node, transform = nil, opts = nil) ⇒ String

Converts an AbstractNode using the given transform.

This method must be implemented by a concrete converter class.

Parameters:

  • node

    The concrete instance of AbstractNode to convert.

  • transform (defaults to: nil)

    An optional String transform that hints at which transformation should be applied to this node. If a transform is not given, the transform is often derived from the value of the AbstractNode#node_name property. (optional, default: nil)

  • opts (defaults to: nil)

    An optional Hash of options hints about how to convert the node. (optional, default: nil)

Returns:

  • (String)

    Returns the String result.

Raises:

  • (::NotImplementedError)

71
72
73
# File 'lib/asciidoctor/converter.rb', line 71

def convert node, transform = nil, opts = nil
  raise ::NotImplementedError, %(#{self.class} (backend: #{@backend}) must implement the ##{__method__} method)
end

#handles?(transform) ⇒ Boolean

Reports whether the current converter is able to convert this node (by its transform name). Used by the CompositeConverter to select which converter to use to handle a given node. Returns true by default.

Parameters:

  • transform

    the String name of the node transformation (typically the node name).

Returns:

  • (Boolean)

    Returns a Boolean indicating whether this converter can handle the specified transform.


81
82
83
# File 'lib/asciidoctor/converter.rb', line 81

def handles? transform
  true
end

#initialize(backend, opts = {}) ⇒ Converter

Creates a new instance of this Asciidoctor::Converter.

Parameters:

  • backend

    The String backend name (aka format) to which this converter converts.

  • opts (defaults to: {})

    An options Hash (optional, default: {})

Returns:

  • (Converter)

    Returns a new Converter instance.


56
57
58
# File 'lib/asciidoctor/converter.rb', line 56

def initialize backend, opts = {}
  @backend = backend
end