Module: VivlioStarter::CLI::Build::VivliostyleConfigWriter

Defined in:
lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb

Overview

用途別 entries / vivliostyle config の生成モジュール

Class Method Summary collapse

Class Method Details

.config_content(entries_basename:, output:) ⇒ Object

config 全文を組み立てる。メタデータの解決(resolve_title/author/language・ EpubBuilder.resolve_page_size)は EPUB config(P3-4 §2.6 で EpubBuilder.generate_epub_config! が本モジュールのリゾルバを呼ぶ)と共通。



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 90

def config_content(entries_basename:, output:)
  # JS 単一引用符文字列向けエスケープ。ブロック形で返すことで gsub の置換文字列
  # 内バックリファレンス(\' = 後方一致)解釈を避け、\ と ' を確実に \ で退避する。
  esc = ->(s) { s.to_s.gsub(/[\\']/) { |c| "\\#{c}" } }

  <<~JS
    import entries from './#{entries_basename}';

    // @ts-check
    // ビルドパイプライン専用設定ファイル(自動生成・編集不要)
    /** @type {import('@vivliostyle/cli').VivliostyleConfigSchema} */
    const vivliostyleConfig = {
      title: '#{esc.call(resolve_title)}',
      author: '#{esc.call(resolve_author)}',
      language: '#{esc.call(resolve_language)}',
      size: '#{esc.call(EpubBuilder.resolve_page_size(Common::CONFIG))}',
      readingProgression: 'ltr',
      workspaceDir: '#{Common::BUILD_VIVLIOSTYLE_PDF_DIR}',
      entry: entries,
      output: [
        './#{output}'
      ]
    };

    export default vivliostyleConfig;
  JS
end

.resolve_authorObject

vivliostyle 11 の config スキーマは author / language に 1 文字以上を 要求するため、未設定はプレースホルダへ寄せる(EPUB 経路と同一規則)。



127
128
129
130
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 127

def resolve_author
  author = Common::CONFIG.book.author.to_s.strip
  author.empty? ? '著者名' : author
end

.resolve_languageObject



132
133
134
135
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 132

def resolve_language
  language = Common::CONFIG.book.language.to_s.strip
  language.empty? ? 'ja' : language
end

.resolve_titleObject

main_title + subtitle 合成 → プレースホルダ。



119
120
121
122
123
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 119

def resolve_title
  book = Common::CONFIG.book
  combined = [book.main_title, book.subtitle].compact.map { it.to_s.strip }.reject(&:empty?).join(' ')
  combined.empty? ? '書籍タイトル' : combined
end

.write!(name:, entry_htmls:, output:, dir: Common::BUILD_PDF_DIR) ⇒ String

entries と config をペアで生成する。

Parameters:

  • name (String)

    用途名('sections' / 'front' / 'colophon' / 'single' 等)

  • entry_htmls (Array<String>)

    エントリ HTML(cwd 相対パス・結合順)

  • output (String)

    出力 PDF(cwd 相対パス)

  • dir (String) (defaults to: Common::BUILD_PDF_DIR)

    生成先ディレクトリ(既定: ワークスペースの pdf/)

Returns:

  • (String)

    生成した config ファイルのパス



51
52
53
54
55
56
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 51

def write!(name:, entry_htmls:, output:, dir: Common::BUILD_PDF_DIR)
  FileUtils.mkdir_p(dir)
  entries_file = File.join(dir, "entries.#{name}.js")
  write_entries!(entries_file, entry_htmls)
  write_config_only!(name:, entries_name: name, output:, dir:)
end

.write_config_only!(name:, entries_name:, output:, dir: Common::BUILD_PDF_DIR) ⇒ String

既存の entries.<entries_name>.js を共用し、config だけを生成する (入稿用: 本文 entries を共用して出力ファイル名だけ差し替える・P4 §3.2)。

Returns:

  • (String)

    生成した config ファイルのパス



61
62
63
64
65
66
67
68
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 61

def write_config_only!(name:, entries_name:, output:, dir: Common::BUILD_PDF_DIR)
  FileUtils.mkdir_p(dir)
  config_file = File.join(dir, "vivliostyle.config.#{name}.js")
  File.write(config_file, config_content(entries_basename: "entries.#{entries_name}.js", output:),
             encoding: 'utf-8')
  Common.log_info("[config-writer] #{config_file} を生成しました")
  config_file
end

.write_entries!(path, entry_htmls) ⇒ Object

entries ファイルを書き出す(EntriesCommands の ESM 形式と同一)。 path は cwd 相対のまま './' を前置する(E1: entry は cwd 基準で解決される)。



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vivlio_starter/cli/build/vivliostyle_config_writer.rb', line 72

def write_entries!(path, entry_htmls)
  entries = entry_htmls.map { EntriesCommands.build_entry(it) }
  File.open(path, 'w') do |f|
    f.puts 'export default ['
    entries.each_with_index do |entry, i|
      f.puts '  {'
      f.puts %(    "path": "#{entry[:path]}",)
      f.puts %(    "title": "#{entry[:title]}")
      f.puts "  }#{',' if i < entries.length - 1}"
    end
    f.puts ']'
  end
  path
end