Module: LiquidDiagrams::Rendering

Included in:
BasicRenderer
Defined in:
lib/liquid_diagrams/rendering.rb

Class Method Summary collapse

Class Method Details

.render_with_command(command, output = :stdout, **options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/liquid_diagrams/rendering.rb', line 33

def render_with_command(command, output = :stdout, **options)
  begin
    stdout, stderr, status = Open3.capture3(command, **options)
  rescue Errno::ENOENT
    raise Errors::CommandNotFoundError, command.split(' ')[0]
  end

  unless status.success?
    msg = "#{command}: #{stderr.empty? ? stdout : stderr}"

    raise Errors::RenderingFailedError, msg
  end

  output == :stdout ? stdout : File.read(output)
end

.render_with_stdin_stdout(command, content) ⇒ Object



11
12
13
14
15
16
# File 'lib/liquid_diagrams/rendering.rb', line 11

def render_with_stdin_stdout(command, content)
  options = yield command if block_given?
  command = "#{command} #{options}".strip

  render_with_command(command, :stdout, stdin_data: content)
end

.render_with_tempfile(command, content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/liquid_diagrams/rendering.rb', line 18

def render_with_tempfile(command, content)
  input = Tempfile.new('input')
  output = Tempfile.new(%w[output .svg])

  File.write(input.path, content)

  options = yield input.path, output.path
  command = "#{command} #{options}".strip

  render_with_command(command, output.path)
ensure
  input.close!
  output.close!
end