Class: VivlioStarter::CLI::PreProcessCommands::MathTransformer::NodeRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/pre_process/math_transformer.rb

Overview

Node 上の MathJax を呼び出して LaTeX→SVG する既定レンダラ。 items([latex, display])を stdin で渡し、=> svg を受け取る。

Instance Method Summary collapse

Constructor Details

#initialize(node_command, mathjax_root) ⇒ NodeRenderer

Returns a new instance of NodeRenderer.



239
240
241
242
# File 'lib/vivlio_starter/cli/pre_process/math_transformer.rb', line 239

def initialize(node_command, mathjax_root)
  @node_command = node_command
  @mathjax_root = mathjax_root
end

Instance Method Details

#render_batch(items) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/vivlio_starter/cli/pre_process/math_transformer.rb', line 244

def render_batch(items)
  input = JSON.generate(items)
  output = nil
  env = { 'MATHJAX_ROOT' => @mathjax_root }
  IO.popen(env, [@node_command, SCRIPT_PATH], 'r+', err: File::NULL) do |io|
    io.write(input)
    io.close_write
    output = io.read
  end
  return {} if output.nil? || output.empty?

  JSON.parse(output)
rescue StandardError
  {}
end