Class: ReVIEW::ImgGraph

Inherits:
Object show all
Includes:
Loggable
Defined in:
lib/review/img_graph.rb

Instance Attribute Summary collapse

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Loggable

#app_error, #debug, #error, #error!, #warn

Constructor Details

#initialize(config, target_name, path_name: '_review_graph') ⇒ ImgGraph

Returns a new instance of ImgGraph.



14
15
16
17
18
19
20
# 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

Instance Attribute Details

#graph_mapsObject (readonly)

Returns the value of attribute graph_maps.



22
23
24
# File 'lib/review/img_graph.rb', line 22

def graph_maps
  @graph_maps
end

Instance Method Details

#cleanup_graphimgObject



24
25
26
# File 'lib/review/img_graph.rb', line 24

def cleanup_graphimg
  FileUtils.rm_rf(@graph_dir)
end

#defer_mermaid_image(str, key) ⇒ Object



36
37
38
39
# 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_extObject



28
29
30
31
32
33
34
# 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_imagesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



41
42
43
44
45
46
47
48
# 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