Class: VivlioStarter::CLI::IndexCommands::HierarchicalIndex

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

Overview

階層化索引ビルダー

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHierarchicalIndex

Returns a new instance of HierarchicalIndex.



32
33
34
35
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 32

def initialize
  @entries = Hash.new { |h, k| h[k] = [] }
  @hierarchy = Hash.new { |h, k| h[k] = Set[] }
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



30
31
32
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 30

def entries
  @entries
end

#hierarchyObject (readonly)

Returns the value of attribute hierarchy.



30
31
32
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 30

def hierarchy
  @hierarchy
end

Instance Method Details

#add_entry(term, link, parent: nil) ⇒ Object

エントリを追加

Parameters:

  • term (String)

    用語

  • link (String)

    リンク先

  • parent (String, nil) (defaults to: nil)

    親用語(階層化用)



41
42
43
44
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 41

def add_entry(term, link, parent: nil)
  @entries[term] << link
  @hierarchy[parent] << term if parent
end

#calculate_page_ranges(page_numbers) ⇒ Array<String>

ページ範囲を計算(連続ページをまとめる)

Parameters:

  • page_numbers (Array<Integer>)

    ページ番号の配列

Returns:

  • (Array<String>)

    範囲表記の配列(例: ["10-12", "15", "20-22"])



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 61

def calculate_page_ranges(page_numbers)
  return [] if page_numbers.empty?

  sorted = page_numbers.uniq.sort
  ranges = []
  range_start = sorted.first
  range_end = sorted.first

  sorted[1..].each do |num|
    if num == range_end + 1
      range_end = num
    else
      ranges << format_range(range_start, range_end)
      range_start = num
      range_end = num
    end
  end
  ranges << format_range(range_start, range_end)

  ranges
end

#children_of(term) ⇒ Object

用語の子用語を取得



96
97
98
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 96

def children_of(term)
  @hierarchy[term].to_a
end

#deduplicate_same_page!Object

同一ページの重複を排除 HTMLファイル名が同じリンクをまとめる



48
49
50
51
52
53
54
55
56
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 48

def deduplicate_same_page!
  @entries.each do |term, links|
    # ファイル名ごとにグループ化
    grouped = links.group_by { |link| link.split('#').first }

    # 各ファイルの最初のリンクのみを保持
    @entries[term] = grouped.map { |_, group| group.first }
  end
end

#entry_countObject

エントリ数を取得



101
102
103
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 101

def entry_count
  @entries.size
end

#get_hierarchyHash

階層構造を取得

Returns:

  • (Hash)

    親用語 => 子用語の Set のハッシュ



85
86
87
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 85

def get_hierarchy
  @hierarchy.transform_values(&:to_a)
end

リンク総数を取得



106
107
108
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 106

def link_count
  @entries.values.sum(&:size)
end

#root_termsObject

ルートレベルの用語を取得(親を持たない用語)



90
91
92
93
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 90

def root_terms
  all_children = @hierarchy.values.flatten.to_set
  @entries.keys.reject { all_children.include?(it) }
end