Class: Asciidoctor::Katex::StemConverterDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/asciidoctor/katex/stem_converter_decorator.rb

Overview

The converter decorator that renders delimited math expressions in block and inline latexmath stem nodes after.

Instance Method Summary collapse

Constructor Details

#initialize(converter, math_renderer) ⇒ StemConverterDecorator

Returns a new instance of StemConverterDecorator.

Parameters:

  • converter (Asciidoctor::Converter)

    the decorated converter to delegate all method calls to.

  • math_renderer (#call)

    callable that accepts a math expression [String] and options [Hash], and returns a rendered expression [String].


14
15
16
17
18
19
# File 'lib/asciidoctor/katex/stem_converter_decorator.rb', line 14

def initialize(converter, math_renderer)
  super(converter)
  @math_renderer = math_renderer
  @block_re = regexp_from_delimiters(::Asciidoctor::BLOCK_MATH_DELIMITERS[:latexmath])
  @inline_re = regexp_from_delimiters(::Asciidoctor::INLINE_MATH_DELIMITERS[:latexmath])
end

Instance Method Details

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

Returns output of the converter.

Parameters:

  • node (Asciidoctor::AbstractNode)

    the node to convert.

  • transform (String, nil) (defaults to: nil)

    the conversion method to call.

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

    options to pass to the converter.

Returns:

  • (String)

    output of the converter.


25
26
27
28
29
30
31
32
33
34
# File 'lib/asciidoctor/katex/stem_converter_decorator.rb', line 25

def convert(node, transform = nil, opts = {})
  # Call the underlying converter.
  output = __getobj__.convert(node, transform, opts)

  if latexmath? node
    render_latexmath(output, node)
  else
    output
  end
end

#latexmath?(node) ⇒ Boolean (protected)

Returns true if the given node is a block or inline latexmath, false otherwise.

Parameters:

  • node (Asciidoctor::AbstractNode)

    the AST node to test.

Returns:

  • (Boolean)

    true if the given node is a block or inline latexmath, false otherwise.


54
55
56
57
58
59
60
61
62
# File 'lib/asciidoctor/katex/stem_converter_decorator.rb', line 54

def latexmath?(node)
  case node.node_name
  when 'stem'
    return true if node.style == 'latexmath'
  when 'inline_quoted'
    return true if node.type == :latexmath
  end
  false
end

#render_latexmath(output, node) ⇒ String (protected)

Returns a copy of output with math expression rendered.

Parameters:

  • output (String)

    the converted node with delimited math expression.

  • node (Asciidoctor::AbstractNode)

    the AST node.

Returns:

  • (String)

    a copy of output with math expression rendered.


41
42
43
44
45
46
47
48
49
# File 'lib/asciidoctor/katex/stem_converter_decorator.rb', line 41

def render_latexmath(output, node)
  @throw_on_error ||= node.document.attr('katex-throw-on-error', false)
  isblock = node.block?

  output.sub(isblock ? @block_re : @inline_re) do
    math = decode_html_entities(::Regexp.last_match[1].strip)
    @math_renderer.call(math, display_mode: isblock, throw_on_error: @throw_on_error)
  end
end