Module: VivlioStarter::CLI::PreProcessCommands::ThemeValidator

Defined in:
lib/vivlio_starter/cli/pre_process/theme_validator.rb

Overview

theme 設定の検証と著者向け警告

Constant Summary collapse

VALID_COLORS =
FrontmatterGenerator::ALLOWED_COLORS

Class Method Summary collapse

Class Method Details

.image_source(raw) ⇒ Object

frontispiece / ornament は「スカラー=画像名だけの短縮形」と 「マッピング=画像名+寸法(heading_chars 等)」の両方を受けるため、画像名を取り出す。 スカラーのまま Data を渡すと画像名の代わりに #<data …> を検証して必ず警告になる。



82
83
84
# File 'lib/vivlio_starter/cli/pre_process/theme_validator.rb', line 82

def image_source(raw)
  raw.is_a?(String) || raw.is_a?(Symbol) ? raw : raw&.dig(:image)
end

.valid_color?(value) ⇒ Boolean

color が既定色名・各種 HEX 記法のいずれかとして受理できるか (FrontmatterGenerator.parse_theme_color の受理条件と一致させる)

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/vivlio_starter/cli/pre_process/theme_validator.rb', line 88

def valid_color?(value)
  t = value.downcase
  return true if VALID_COLORS.include?(t)

  t.match?(/\A#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\z/i) ||
    t.match?(/\A(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\z/i) ||
    t.match?(/\A0x(?:[0-9a-f]{6}|[0-9a-f]{8})\z/i)
end

.validate!(cfg = Common::CONFIG) ⇒ Object

theme 設定を検証し、問題があれば警告する。

Parameters:

  • cfg (Object) (defaults to: Common::CONFIG)

    Common::CONFIG 相当(省略時は Common::CONFIG)



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vivlio_starter/cli/pre_process/theme_validator.rb', line 36

def validate!(cfg = Common::CONFIG)
  theme_cfg = cfg && cfg[:theme]
  return unless theme_cfg

  # color は image / simple どちらのスタイルでも使われるため常に検証する
  validate_color(theme_cfg[:color])

  # simple スタイルは扉絵・飾り画像を使わないため画像検証はスキップする
  return if theme_cfg[:style].to_s.strip.downcase == 'simple'

  validate_image(:frontispiece, image_source(theme_cfg[:frontispiece]), variant: :portrait)
  validate_image(:ornament, image_source(theme_cfg[:ornament]), variant: :landscape)
end

.validate_color(raw) ⇒ Object

theme.color の妥当性を検証する



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vivlio_starter/cli/pre_process/theme_validator.rb', line 51

def validate_color(raw)
  value = raw.to_s.strip
  return if value.empty? # 未指定は既定色(yellow)
  return if valid_color?(value)

  Common.log_warn(
    "theme.color '#{raw}' は無効な色名です。既定色(yellow)でビルドを続行します。",
    detail: "指定できる色: #{VALID_COLORS.join(' / ')}" \
            "または '#ff0000' のような HEX(#rrggbb / #rrggbbaa)"
  )
end

.validate_image(kind, source, variant:) ⇒ Object

theme.frontispiece / theme.ornament の画像存在を検証する

Parameters:

  • kind (:frontispiece, :ornament)

    警告メッセージ用の種別

  • source (String, nil)

    画像名(未指定・URL は検証対象外)

  • variant (:portrait, :landscape)

    判定するバリアント



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vivlio_starter/cli/pre_process/theme_validator.rb', line 67

def validate_image(kind, source, variant:)
  return if source.nil? || source.to_s.strip.empty? # 未指定は既定画像
  return if ThemeImageResolver.theme_image_available?(source, variant: variant)

  fallback = ThemeImageResolver::FALLBACK_THEME_IMAGE_SLUG
  Common.log_warn(
    "theme.#{kind} の画像 '#{source}' が見つかりません。既定画像(#{fallback})で代用します。",
    detail: "stylesheets/images/#{source}.webp を配置するか、" \
            'バンドル画像名(sakura・himawari など)またはスペルを確認してください。'
  )
end