Sha256: c6422bc3e1a26342a7a23f9b48e23309a808926e74d3ff9dc9a366f762b5db79

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require 'open3'
require 'tempfile'
require 'tmpdir'

module LiquidDiagrams
  module Rendering
    module_function

    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

    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liquid-diagrams-0.4.0 lib/liquid_diagrams/rendering.rb