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


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


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)


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)


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