Module: Jekyll::Diagrams::Rendering

Included in:
BasicRenderer
Defined in:
lib/jekyll-diagrams/rendering.rb

Class Method Summary collapse

Class Method Details

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

Render SVG with command

Parameters:

  • command

    Command to run

  • output (defaults to: :stdout)

    Output path, use :stdout for stdout

  • options

    Extra options passed to Open3.captrue3

Returns:

  • The SVG output



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-diagrams/rendering.rb', line 50

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?
    raise Errors::RenderingFailedError, <<~MSG
      #{command}: #{stderr.empty? ? stdout : stderr}
    MSG
  end

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

.render_with_stdin_stdout(command, content) ⇒ Object

Render SVG with stdin and stdout

Parameters:

  • command

    Command to run

  • content

    Content passed to command

Returns:

  • The SVG output



17
18
19
# File 'lib/jekyll-diagrams/rendering.rb', line 17

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

.render_with_tempfile(command, content) ⇒ Object

Render SVG with tempfile

Parameters:

  • command

    Command to run

  • content

    Content passed to command

  • block

    Result of block will append to command

Returns:

  • The SVG output



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll-diagrams/rendering.rb', line 27

def render_with_tempfile(command, content)
  Dir.mktmpdir('jekyll-diagrams-rendering') do |dir|
    input = Tempfile.new('input', tmpdir: dir)
    output = Tempfile.new(%w[output .svg], tmpdir: dir)

    File.write(input.path, content)

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

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