Class: VivlioStarter::CLI::IndexCommands::UnifiedPageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/index/unified_page_builder.rb

Defined Under Namespace

Classes: Limitation

Constant Summary collapse

KANA_ROWS =

五十音の行判定用マッピング(ひらがな・カタカナ両対応)

{
  '' => /^[あ-おぁ-ぉア-オァ-ォ]/,
  '' => /^[か-ごゕゖカ-ゴヵヶ]/,
  '' => /^[さ-ぞサ-ゾ]/,
  '' => /^[た-どタ-ド]/,
  '' => /^[な-のナ-ノ]/,
  '' => /^[は-ぽハ-ポ]/,
  '' => /^[ま-もマ-モ]/,
  '' => /^[や-よゃゅょヤ-ヨャュョ]/,
  '' => /^[ら-ろラ-ロ]/,
  '' => /^[わ-んゎワ-ンヮ]/
}.freeze
SYMBOL_ROW_LABEL =
'記号'
NUMBER_ROW_LABEL =
'数字'
DIGIT_REGEX =
/\A[0-90-9]\z/
ALPHA_ROWS =
('A'..'Z').to_h do |letter|
  [letter, /^[#{letter.downcase}#{letter}]/]
end.freeze
INDEX_OUTPUT_FILE =

出力先はワークスペースの html/(P4 §3.4-1)

File.join(Common::BUILD_HTML_DIR, '_indexpage.html')
GLOSSARY_OUTPUT_FILE =
File.join(Common::BUILD_HTML_DIR, '_glossarypage.html')
REFERENCE_STYLES =

参照の出し方(book.yml の index.reference_style)

%w[main_and_sub main_only all].freeze
DEFAULT_REFERENCE_STYLE =
'main_and_sub'
DEFAULT_MAX_SUB_REFERENCES =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glossary_config: {}, index_config: nil) ⇒ UnifiedPageBuilder

Returns a new instance of UnifiedPageBuilder.



63
64
65
66
67
68
69
70
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 63

def initialize(glossary_config: {}, index_config: nil)
  @index_data = {}
  @glossary_config = glossary_config
  @index_config = index_config || Common::CONFIG.index.to_h
  @glossary_backlinks = {}
  # 間引いた語(no silent caps: 絞ったことは呼び出し元から必ず報告する)
  @limited_reference_terms = []
end

Instance Attribute Details

#index_dataObject (readonly)

Returns the value of attribute index_data.



61
62
63
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 61

def index_data
  @index_data
end

#limited_reference_termsObject (readonly)

Returns the value of attribute limited_reference_terms.



61
62
63
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 61

def limited_reference_terms
  @limited_reference_terms
end

Instance Method Details

#build_glossary!(terms) ⇒ String?

用語集ページを生成

Parameters:

  • terms (Array<Hash>)

    用語集対象の用語リスト(flags に g を含む)

Returns:

  • (String, nil)

    出力ファイルパス、または nil



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 102

def build_glossary!(terms)
  if terms.nil? || terms.empty?
    Common.log_info('用語集に登録された用語がありません')
    cleanup_stale_file!(GLOSSARY_OUTPUT_FILE)
    return nil
  end

  # バックリンクはそのビルドのスキャン結果(中間 YAML)から導出する(R2)。
  # 辞書の backlink_sources は読まない——前回ビルドの幽霊リンクを持ち込まないため
  @glossary_backlinks = load_glossary_backlinks

  sorted_terms = terms.sort_by { it['yomi'] || it['term'] }
  html = generate_glossary_html(sorted_terms)
  FileUtils.mkdir_p(File.dirname(GLOSSARY_OUTPUT_FILE))
  File.write(GLOSSARY_OUTPUT_FILE, html, encoding: 'utf-8')
  Common.log_success("用語集ページを生成しました: #{GLOSSARY_OUTPUT_FILE}")
  GLOSSARY_OUTPUT_FILE
end

#build_index!String?

索引ページを生成

Returns:

  • (String, nil)

    出力ファイルパス、または nil



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 76

def build_index!
  unless File.exist?(Common::INDEX_MATCHES_FILE)
    Common.log_warn("索引データが見つかりません: #{Common::INDEX_MATCHES_FILE}")
    return nil
  end

  load_index_data!

  if @index_data.empty?
    Common.log_info('索引に登録された用語がありません')
    cleanup_stale_file!(INDEX_OUTPUT_FILE)
    return nil
  end

  html = generate_index_html
  FileUtils.mkdir_p(File.dirname(INDEX_OUTPUT_FILE))
  File.write(INDEX_OUTPUT_FILE, html, encoding: 'utf-8')
  Common.log_success("索引ページを生成しました: #{INDEX_OUTPUT_FILE}")
  INDEX_OUTPUT_FILE
end

#cleanup_stale_file!(file) ⇒ Object

以前のビルドで残ったファイルを削除



132
133
134
135
136
137
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 132

def cleanup_stale_file!(file)
  return unless File.exist?(file)

  FileUtils.rm_f(file)
  Common.log_info("#{file} を削除しました")
end

#reference_limitationObject



127
128
129
# File 'lib/vivlio_starter/cli/index/unified_page_builder.rb', line 127

def reference_limitation
  Limitation.new(style: reference_style, limit: max_sub_references, terms: @limited_reference_terms)
end