Class: Asciidoctor::Katex::Treeprocessor

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

Overview

Asciidoctor processor that renders delimited latexmath expressions using the KaTeX library.

Instance Method Summary collapse

Constructor Details

#initialize(katex_options: {}, katex_renderer: nil, require_stem_attr: true) ⇒ Treeprocessor

Returns a new instance of Treeprocessor

Parameters:

  • katex_options (Hash)

    default options for the KaTeX renderer. This parameter has no effect when katex_renderer is provided.

  • katex_renderer (#call, nil)

    callable that accepts a math expression [String] and options [Hash], and returns a rendered expression [String]. Defaults to KatexAdapter initialized with the katex_options.

  • require_stem_attr (Boolean)

    true to skip when stem attribute is not declared, false to process anyway.


25
26
27
28
29
# File 'lib/asciidoctor/katex/treeprocessor.rb', line 25

def initialize(katex_options: {}, katex_renderer: nil, require_stem_attr: true, **)
  @katex_renderer = katex_renderer || KatexAdapter.new(katex_options)
  @require_stem_attr = require_stem_attr
  super
end

Instance Method Details

#process(document) ⇒ void

Parameters:

  • document (Asciidoctor::Document)

    the document to process.


32
33
34
35
36
37
38
39
# File 'lib/asciidoctor/katex/treeprocessor.rb', line 32

def process(document)
  return if skip? document

  converter = document.instance_variable_get(:@converter)
  decorator = StemConverterDecorator.new(converter, @katex_renderer)
  document.instance_variable_set(:@converter, decorator)
  nil
end

#skip?(document) ⇒ Boolean (protected)

Returns whether to skip processing of the document.

Parameters:

  • document (Asciidoctor::Document)

    the document to process.

Returns:

  • (Boolean)

    whether to skip processing of the document.


45
46
47
# File 'lib/asciidoctor/katex/treeprocessor.rb', line 45

def skip?(document)
  @require_stem_attr && !document.attr?('stem')
end