Module: Temml

Defined in:
lib/temml.rb,
lib/temml/engine.rb,
lib/temml/version.rb

Overview

SPDX-License-Identifier: CC0-1.0 AND MIT

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
'0.31.0'
TEMML_VERSION =
'0.13.5'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.execjs_runtime ⇒ Object

The ExecJS runtime factory, default: -> { ExecJS.runtime }. Set this before calling any other methods to use a different runtime.

This proc is guaranteed to be called at most once.



47
48
49
# File 'lib/temml.rb', line 47

def execjs_runtime
  @execjs_runtime
end

Class Method Details

.gem_path ⇒ Object



60
61
62
63
# File 'lib/temml.rb', line 60

def gem_path
  @gem_path ||=
    File.expand_path(File.join(File.dirname(__FILE__), '..'))
end

.render(math, throwOnError: true) ⇒ String

Note:

This method is thread-safe as long as your ExecJS runtime is thread-safe. MiniRacer is the recommended runtime.

Renders the given math expression to MathML via temml.renderToString.

Additional options must be passed as anonymous keyword arguments. See https://temml.org/docs/en/administration.html#options

Parameters:

  • math (String) —

    The math (Latex) expression

  • throwOnError (Boolean) (defaults to: true) —

    Whether to raise on error. If false, renders the error message instead (even in case of ParseError, unlike the native temml).

Returns:

  • (String) —

    MathML. If strings respond to html_safe, the result will be HTML-safe.



31
32
33
34
35
36
37
38
39
40
# File 'lib/temml.rb', line 31

def render(math, throwOnError: true, **)
  maybe_html_safe temml_context.call(
    'temml.renderToString',
    math,
    throwOnError:,
    **
  )
rescue ExecJS::ProgramError => e
  raise e if throwOnError
end

.temml_context ⇒ Object



49
50
51
52
53
# File 'lib/temml.rb', line 49

def temml_context
  @load_context_mutex.synchronize do
    @temml_context ||= @execjs_runtime.call.compile File.read temml_js_path
  end
end

.temml_js_path ⇒ Object



55
56
57
58
# File 'lib/temml.rb', line 55

def temml_js_path
  File.expand_path File.join('vendor', 'temml', 'javascripts', 'temml.js'),
                   gem_path
end