Class: Vivlio::Starter::CLI::IndexCommands::UnifiedPageBuilder

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

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_MATCH_FILE =
'_index_matches.yml'
INDEX_OUTPUT_FILE =
'_indexpage.html'
GLOSSARY_OUTPUT_FILE =
'_glossarypage.html'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glossary_config: {}) ⇒ UnifiedPageBuilder

Returns a new instance of UnifiedPageBuilder.



59
60
61
62
63
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 59

def initialize(glossary_config: {})
  @index_data = {}
  @hierarchical_index = HierarchicalIndex.new
  @glossary_config = glossary_config
end

Instance Attribute Details

#hierarchical_indexObject (readonly)

Returns the value of attribute hierarchical_index.



57
58
59
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 57

def hierarchical_index
  @hierarchical_index
end

#index_dataObject (readonly)

Returns the value of attribute index_data.



57
58
59
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 57

def index_data
  @index_data
end

Instance Method Details

#build_glossary!(terms) ⇒ String?

用語集ページを生成

Parameters:

  • terms (Array<Hash>)

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

Returns:

  • (String, nil)

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



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 94

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

  sorted_terms = terms.sort_by { it['yomi'] || it['term'] }
  html = generate_glossary_html(sorted_terms)
  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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 69

def build_index!
  unless File.exist?(INDEX_MATCH_FILE)
    Common.log_warn("索引データが見つかりません: #{INDEX_MATCH_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
  File.write(INDEX_OUTPUT_FILE, html, encoding: 'utf-8')
  Common.log_success("索引ページを生成しました: #{INDEX_OUTPUT_FILE}")
  INDEX_OUTPUT_FILE
end

#cleanup_stale_file!(file) ⇒ Object

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



109
110
111
112
113
114
# File 'lib/vivlio/starter/cli/index/unified_page_builder.rb', line 109

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

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