class ReVIEW::ImgGraph

Attributes

graph_maps[R]

Public Class Methods

new(config, target_name, path_name: '_review_graph') click to toggle source
# File lib/review/img_graph.rb, line 14
def initialize(config, target_name, path_name: '_review_graph')
  @config = config
  @target_name = target_name
  @logger = ReVIEW.logger
  @graph_dir = File.join(@config['imagedir'], target_name, path_name)
  @graph_maps = {}
end

Public Instance Methods

cleanup_graphimg() click to toggle source
# File lib/review/img_graph.rb, line 24
def cleanup_graphimg
  FileUtils.rm_rf(@graph_dir)
end
defer_mermaid_image(str, key) click to toggle source
# File lib/review/img_graph.rb, line 36
def defer_mermaid_image(str, key)
  @graph_maps[key] = { type: 'mermaid', content: str }
  File.join('.', @config['imagedir'], @target_name, "#{key}.#{graph_ext}")
end
graph_ext() click to toggle source
# File lib/review/img_graph.rb, line 28
def graph_ext
  if %w[html markdown rst].include?(@target_name)
    'svg'
  else
    'pdf'
  end
end
make_mermaid_images() click to toggle source
# File lib/review/img_graph.rb, line 50
def make_mermaid_images
  mermaid_graph_maps = @graph_maps.select { |_k, v| v[:type] == 'mermaid' }
  return if mermaid_graph_maps.empty?

  FileUtils.mkdir_p(File.join(@graph_dir, 'mermaid'))
  mermaid_graph_maps.each_pair do |key, val|
    File.write(File.join(@graph_dir, 'mermaid', "#{key}.html"), mermaid_html(val[:content]))
  end
  @logger.info 'calling Playwright'

  begin
    require 'playwrightrunner'
    PlaywrightRunner.mermaids_to_images(
      { playwright_path: @config['playwright_options']['playwright_path'],
        selfcrop: @config['playwright_options']['selfcrop'],
        pdfcrop_path: @config['playwright_options']['pdfcrop_path'],
        pdftocairo_path: @config['playwright_options']['pdftocairo_path'] },
      src: File.join(@graph_dir, 'mermaid'),
      dest: File.join(@config['imagedir'], @target_name),
      type: graph_ext
    )
  rescue SystemCallError => e
    raise ApplicationError, "converting mermaid failed: #{e}"
  rescue LoadError
    raise ApplicationError, 'could not handle Mermaid of //graph in this builder.'
  end
  FileUtils.rm_rf(File.join(@graph_dir, 'mermaid'))
end
mermaid_html(content) click to toggle source
# File lib/review/img_graph.rb, line 41
    def mermaid_html(content)
      <<-EOB
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><script type="module">import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs'; mermaid.initialize({ startOnLoad: true });</script></head>
<body><div><pre class="mermaid">#{content}</pre></div></body></html>
EOB
    end