Class: VivlioStarter::CLI::PreProcessCommands::MermaidRenderer
- Inherits:
-
Object
- Object
- VivlioStarter::CLI::PreProcessCommands::MermaidRenderer
- 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
-
#available? ⇒ Boolean
mmdc の実行が可能か(コマンドが解決でき --version が起動するか)。.
-
#render(source, format:, font_family: nil, theme: DEFAULT_THEME) ⇒ String?
mermaid ソースを 1 つ描画してバイト列を返す。失敗時は nil。.
-
#version ⇒ Object
導入済み mmdc のバージョン文字列(キャッシュキーの一部・図の再生成判定に使う)。 取得できないときは空文字(キーの決定性は保たれる)。.
Instance Method Details
#available? ⇒ Boolean
mmdc の実行が可能か(コマンドが解決でき --version が起動するか)。
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。
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.}") nil end |
#version ⇒ Object
導入済み 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 |