Class: Asciidoctor::Rouge::Treeprocessor

Inherits:
Extensions::Treeprocessor
  • Object
show all
Defined in:
lib/asciidoctor/rouge/treeprocessor.rb

Overview

An Asciidoctor extension that highlights source listings using Rouge.

Instance Method Summary collapse

Constructor Details

#initialize(formatter: HtmlFormatter, formatter_opts: {}, callouts_sub: CalloutsSubstitutor, passthroughs_sub: PassthroughsSubstitutor) ⇒ Treeprocessor

Returns a new instance of Treeprocessor.

Parameters:

  • formatter (Class<Rouge::Formatter>) (defaults to: HtmlFormatter)

    the Rouge formatter to use for formatting a token stream from a Rouge lexer. It must respond to method format accepting a token stream and (optionally) a hash of options, producing String. Defaults to HtmlFormatter.

  • formatter_opts (Hash) (defaults to: {})

    options to pass to the formatter. It's used only if formatter's format method has arity > 1. Defaults to empty hash.

  • callouts_sub (#create) (defaults to: CalloutsSubstitutor)

    the callouts substitutor class to use for processing callouts. Defaults to CalloutsSubstitutor.

  • passthroughs_sub (#create) (defaults to: PassthroughsSubstitutor)

    the passthroughs substitutor class to use for processing passthroughs. Defaults to PassthroughsSubstitutor.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asciidoctor/rouge/treeprocessor.rb', line 30

def initialize(formatter: HtmlFormatter,
               formatter_opts: {},
               callouts_sub: CalloutsSubstitutor,
               passthroughs_sub: PassthroughsSubstitutor, **)
  super

  @formatter = formatter
  @formatter_opts = formatter_opts
  @callouts_sub = callouts_sub
  @passthroughs_sub = passthroughs_sub
end

Instance Method Details

#process(document) ⇒ void

Parameters:

  • document (Asciidoctor::Document)

    the document to process.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/asciidoctor/rouge/treeprocessor.rb', line 43

def process(document)
  return unless document.attr? 'source-highlighter', 'rouge'

  document.find_by(context: :listing, style: 'source') do |block|
    process_listing(block)
  end

  # Table cells may contain listing, but Document#find_by does not search
  # inside table, so we must handle it specially.
  document.find_by(context: :table) do |table|
    table.rows.body.each do |row|
      row.each do |cell|
        if (inner = cell.inner_document)
          inner.find_by(context: :listing, style: 'source') do |block|
            process_listing(block)
          end
        end
      end
    end
  end
end