Class: VivlioStarter::CLI::PreProcessCommands::MermaidRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/pre_process/mermaid_renderer.rb

Overview

mmdc を呼び出して mermaid ソースを SVG / PNG へ描画する既定レンダラ。

Constant Summary collapse

DEFAULT_THEME =

既定テーマ(§5.2・第一段階はライト固定)。

'neutral'
RASTER_SCALE =

PNG(EPUB/Kindle 用ラスター)の解像度倍率。リーダー側の縮小表示で鮮明に見せる。

2

Instance Method Summary collapse

Instance Method Details

#available?Boolean

mmdc の実行が可能か(コマンドが解決でき --version が起動するか)。

Returns:

  • (Boolean)


44
45
46
# File 'lib/vivlio_starter/cli/pre_process/mermaid_renderer.rb', line 44

def available?
  !mmdc_command.nil?
end

#render(source, format:, font_family: nil, theme: DEFAULT_THEME) ⇒ String?

mermaid ソースを 1 つ描画してバイト列を返す。失敗時は nil。

Parameters:

  • source (String)

    mermaid の図ソース(フェンス内テキスト)

  • format (Symbol)

    :svg(PDF 用ベクタ)/ :png(EPUB/Kindle 用ラスター)

  • font_family (String, nil) (defaults to: nil)

    図中テキストの font-family(§5.1)

  • theme (String) (defaults to: DEFAULT_THEME)

    mermaid テーマ(既定 neutral)

Returns:

  • (String, nil)

    SVG 文字列 / PNG バイト列(失敗時 nil)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vivlio_starter/cli/pre_process/mermaid_renderer.rb', line 63

def render(source, format:, font_family: nil, theme: DEFAULT_THEME)
  cmd = mmdc_command
  return nil if cmd.nil? || source.to_s.strip.empty?

  with_temp_files(source, font_family, format) do |input_path, config_path, puppeteer_path, output_path|
    argv = [cmd, '-i', input_path, '-o', output_path, '-b', 'transparent',
            '-t', theme, '-c', config_path, '-p', puppeteer_path]
    argv.push('-s', RASTER_SCALE.to_s) if format == :png

    _out, status = Open3.capture2e(*argv)
    next nil unless status.success? && File.exist?(output_path) && !File.empty?(output_path)

    format == :svg ? File.read(output_path, encoding: 'utf-8') : File.binread(output_path)
  end
rescue StandardError => e
  Common.log_debug("[mermaid] mmdc 実行でエラー: #{e.class}: #{e.message}")
  nil
end

#versionObject

導入済み mmdc のバージョン文字列(キャッシュキーの一部・図の再生成判定に使う)。 取得できないときは空文字(キーの決定性は保たれる)。



50
51
52
53
54
# File 'lib/vivlio_starter/cli/pre_process/mermaid_renderer.rb', line 50

def version
  return @version if defined?(@version)

  @version = resolve_version
end