Module: Vivlio::Starter::CLI::Build::OutlineExtractor

Defined in:
lib/vivlio/starter/cli/build/outline_extractor.rb

Overview

HTML 見出し抽出・PDF アウトライン生成モジュール

Constant Summary collapse

APPX_RANGE =

章レンジ(定数)

(90..98)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.last_outline_debug_infoObject

Returns the value of attribute last_outline_debug_info.



36
37
38
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 36

def last_outline_debug_info
  @last_outline_debug_info
end

Class Method Details

.add_outline_from_headings!(pdf_path, html_files, max_level: 3, start_page: 1) ⇒ Object

PDFにアウトラインを付与



155
156
157
158
159
160
161
162
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 155

def add_outline_from_headings!(pdf_path, html_files, max_level: 3, start_page: 1)
  require 'vivlio/starter/cli/pdf/provider'

  entries = heading_page_entries(pdf_path, html_files, max_level: max_level, start_page: start_page)
  return if entries.empty?

  Vivlio::Starter::Pdf.provider.add_outline!(pdf_path, entries, max_level: max_level)
end

.add_toc_entry(items, chapter_ranges, chapter_order, search_helpers) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 541

def add_toc_entry(items, chapter_ranges, chapter_order, search_helpers)
  return items unless chapter_ranges['_toc']

  toc_range = chapter_ranges['_toc']
  toc_page = search_helpers[:search_markers].call(['目次'], toc_range[0], toc_range[1]) || toc_range[0]

  return items if items.any? { |it| it[:chapter] == '_toc' }

  insert_index = items.index do |it|
    chapter_order.index(it[:chapter])&.> chapter_order.index('_toc')
  end || items.length
  items.insert(insert_index, { level: 1, text: '目次', page: toc_page, chapter: '_toc', id: nil })
  items
end

.appendix_label_for_basename(basename) ⇒ Object

付録ラベル取得



43
44
45
46
47
48
49
50
51
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 43

def appendix_label_for_basename(basename)
  entry = TokenResolver::Resolver.new.resolve_file(basename)
  return nil unless entry.number && APPX_RANGE.include?(entry.number.to_i)

  letter = Common.appendix_number_to_letter(entry.number)
  return nil unless letter

  "付録#{letter.upcase}"
end

.build_chapter_display_text(basename, heading) ⇒ Object



514
515
516
517
518
519
520
521
522
523
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 514

def build_chapter_display_text(basename, heading)
  display_text = heading[:text]

  if heading[:appendix_label]
    prepend_label_if_needed(display_text, heading[:appendix_label].to_s.strip)
  else
    number_display = resolve_chapter_number_display(basename, heading)
    prepend_label_if_needed(display_text, number_display)
  end
end

.build_chapter_order(html_basenames) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 235

def build_chapter_order(html_basenames)
  chapter_order = Build::SectionBuilder.chapter_order_from(html_basenames)
  # 新仕様: _titlepage, _legalpage を使用
  frontmatter_sequence = %w[_titlepage _legalpage 00-preface _toc]
  # 巻末の順序: 用語集 → 終わりに → 索引
  # 索引・用語集が無効の場合は除外
  backmatter_sequence = if IndexCommands.index_enabled?
                          %w[_glossarypage 99-postface _indexpage]
                        else
                          %w[99-postface]
                        end

  # 巻末ページを除外した章順序
  main_chapters = chapter_order.reject { |bn| backmatter_sequence.include?(bn) }

  (frontmatter_sequence + main_chapters + backmatter_sequence).uniq
end

.build_chapter_paths(html_paths) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 226

def build_chapter_paths(html_paths)
  chapter_paths = {}
  html_paths.each do |path|
    bn = File.basename(path, '.html')
    chapter_paths[bn] = path
  end
  chapter_paths
end

.build_display_text(basename, heading) ⇒ Object



503
504
505
506
507
508
509
510
511
512
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 503

def build_display_text(basename, heading)
  return heading[:text] unless heading[:level].to_i == 1

  case basename
  when '99-colophon'
    '奥付'
  else
    build_chapter_display_text(basename, heading)
  end
end

.build_outline_items(headings_by_chapter, chapter_ranges, _chapter_order, search_helpers, total_pages) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 468

def build_outline_items(headings_by_chapter, chapter_ranges, _chapter_order, search_helpers, total_pages)
  search_markers = search_helpers[:search_markers]
  items = []
  fallback_items = []

  headings_by_chapter.each do |bn, headings|
    range = chapter_ranges[bn]
    next unless range

    headings.each do |heading|
      page = if bn == '_toc'
               range[0]
             else
               search_terms = (Array(heading[:search_terms]) + [heading[:text],
                                                                heading[:appendix_label]]).compact.map do |s|
                 s.to_s.strip
               end.reject(&:empty?).uniq
               found_page = search_markers.call(search_terms, range[0], range[1])
               found_page ||= search_markers.call(search_terms, range[0], total_pages)
               unless found_page
                 fallback_items << { chapter: bn, text: heading[:text], target_page: range[0],
                                     search_terms: search_terms }
                 found_page = range[0]
               end
               found_page
             end

      display_text = build_display_text(bn, heading)
      items << { level: heading[:level], text: display_text, page: page, chapter: bn, id: heading[:id] }
    end
  end

  [items, fallback_items]
end

.build_page_range_context(chapter_ranges, chapter_starts, chapter_markers, search_markers, from_base, total_pages, preface_pages, toc_pages) ⇒ Object

ページ範囲計算用のコンテキストハッシュを構築



379
380
381
382
383
384
385
386
387
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 379

def build_page_range_context(chapter_ranges, chapter_starts, chapter_markers,
                             search_markers, from_base, total_pages, preface_pages, toc_pages)
  {
    chapter_ranges: chapter_ranges, chapter_starts: chapter_starts,
    chapter_markers: chapter_markers, search_markers: search_markers,
    from_base: from_base, total_pages: total_pages,
    preface_pages: preface_pages, toc_pages: toc_pages
  }
end

.build_search_helpers(pdf_path, total_pages) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 277

def build_search_helpers(pdf_path, total_pages)
  page_cache = {}
  normalized_cache = {}
  normalize = ->(str) { str.to_s.gsub(/[[:space:]\u00A0\u2000-\u200B\u202F\u205F\u3000]+/, '') }

  fetch_page_text = lambda do |page|
    page = page.to_i.clamp(1, total_pages)
    page_cache[page] ||= `pdftotext -f #{page} -l #{page} "#{pdf_path}" - 2>/dev/null`
  end

  find_page_in_pdf = lambda do |term, from_page, to_page|
    term = term.to_s.strip
    return nil if term.empty?

    normalized_term = normalize.call(term)
    from_page = from_page.to_i.clamp(1, total_pages)
    to_page = to_page.to_i.clamp(from_page, total_pages)
    return nil if from_page > to_page

    (from_page..to_page).each do |page|
      text = fetch_page_text.call(page)
      next if text.nil? || text.empty?
      return page if text.include?(term)

      normalized_text = normalized_cache[page] ||= normalize.call(text)
      return page if !normalized_term.empty? && normalized_text.include?(normalized_term)
    end
    nil
  end

  search_markers = lambda do |markers, from_page, to_page|
    Array(markers).each do |term|
      page = find_page_in_pdf.call(term, from_page, to_page)
      return page if page
    end
    nil
  end

  { search_markers: search_markers }
end

.build_search_terms(number_text, title_text, heading_attr, text, appendix_label) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 193

def build_search_terms(number_text, title_text, heading_attr, text, appendix_label)
  search_terms = []
  if number_text && !number_text.empty?
    search_terms << "#{number_text}#{title_text}" unless title_text.empty?
    search_terms << "#{number_text} #{title_text}" unless title_text.empty?
    search_terms << number_text
  end
  search_terms << heading_attr unless heading_attr.empty?
  search_terms << text
  search_terms << appendix_label.to_s unless appendix_label.to_s.empty?
  search_terms.compact.map { |t| t.to_s.strip }.reject(&:empty?).uniq
end

.calculate_chapter_ranges(chapter_order, chapter_markers, search_helpers, from_base, total_pages) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 318

def calculate_chapter_ranges(chapter_order, chapter_markers, search_helpers, from_base, total_pages)
  preface_pages = (Build::Utilities.page_count('00-preface.pdf') || '0').to_i
  toc_pages = (Build::Utilities.page_count('_toc.pdf') || '0').to_i

  # 00-preface.pdfが存在しない場合、テキスト検索で目次の位置を特定し、前書きのページ数を推測
  if preface_pages.zero? && toc_pages.positive?
    # 前書きは3ページ目から開始(_titlepage_legalpage.pdfの2ページ後)
    # 目次の位置をテキスト検索で特定
    toc_start = search_helpers[:search_markers].call(['目次'], 3, total_pages)
    if toc_start && toc_start > 3
      preface_pages = toc_start - 3 # 前書きのページ数を推測
    end
  end

  chapter_starts = {}
  chapter_ranges = {}
  # 本文章(01-89)の最初の章を特定(_titlepage, _legalpage, 00-preface, _toc は除外)
  resolver = TokenResolver::Resolver.new
  first_chapter_bn = chapter_order.find do |token|
    entry = resolver.resolve_file(token)
    entry.number&.to_i&.between?(1, 89)
  end

  ctx = build_page_range_context(
    chapter_ranges, chapter_starts, chapter_markers,
    search_helpers[:search_markers], from_base, total_pages, preface_pages, toc_pages
  )

  prev_bn = nil
  chapter_order.each do |bn|
    start_page, end_page = calculate_page_range(bn, ctx, first_chapter_bn, prev_bn)
    update_previous_chapter_end(chapter_ranges, prev_bn, start_page, total_pages)

    chapter_starts[bn] = start_page
    chapter_ranges[bn] = [start_page, end_page]
    prev_bn = bn
  end

  clamp_all_ranges(chapter_ranges, from_base, total_pages)
end

.calculate_page_range(basename, ctx, first_chapter_bn, prev_bn) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 389

def calculate_page_range(basename, ctx, first_chapter_bn, prev_bn)
  case basename
  when '_titlepage' then page_range_titlepage(ctx)
  when '_legalpage' then page_range_legalpage(ctx)
  when '00-preface' then page_range_preface(ctx)
  when '_toc'       then page_range_toc(ctx)
  when '99-colophon' then [ctx[:total_pages], ctx[:total_pages]]
  when '_glossarypage' then page_range_backmatter(basename, ctx, prev_bn, ['用語集'])
  when '99-postface' then page_range_backmatter(basename, ctx, prev_bn, ['終わりに'])
  when '_indexpage' then page_range_backmatter(basename, ctx, prev_bn, ['索引'])
  when first_chapter_bn then page_range_first_chapter(ctx, prev_bn)
  else page_range_default(basename, ctx, prev_bn)
  end
end

.clamp_all_ranges(chapter_ranges, from_base, total_pages) ⇒ Object



367
368
369
370
371
372
373
374
375
376
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 367

def clamp_all_ranges(chapter_ranges, from_base, total_pages)
  chapter_ranges.each_value do |rng|
    next unless rng

    rng[0] = rng[0].clamp(from_base, total_pages)
    rng[1] = rng[1].clamp(rng[0], total_pages)
  end

  chapter_ranges
end

.extract_all_headings(chapter_order, chapter_paths, max_level) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 253

def extract_all_headings(chapter_order, chapter_paths, max_level)
  headings_by_chapter = Hash.new { |h, k| h[k] = [] }
  chapter_markers = {}

  chapter_order.each do |bn|
    path = chapter_paths[bn]
    headings = if path
                 extract_headings_from_html_file(path, max_level: max_level,
                                                       include_appendix_label: true)
               else
                 []
               end
    headings_by_chapter[bn].concat(headings) if headings.any?

    primary = headings.find { |h| h[:level] == 1 } || headings.first
    if primary
      markers = Array(primary[:search_terms]) + [primary[:text]]
      chapter_markers[bn] = markers.compact.map { |s| s.to_s.strip }.reject(&:empty?).uniq
    end
  end

  [headings_by_chapter, chapter_markers]
end

.extract_headings_from_html_file(path, max_level:, include_appendix_label: true) ⇒ Object

HTMLファイルから見出しを抽出



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 54

def extract_headings_from_html_file(path, max_level:, include_appendix_label: true)
  html = File.read(path, encoding: 'utf-8')
  doc  = Nokogiri::HTML.parse(html)
  basename = File.basename(path, '.html')
  headings = []
  selector = (1..max_level).map { |lvl| "h#{lvl}" }.join(',')
  doc.css(selector).each do |node|
    lvl = node.name.delete_prefix('h').to_i
    next unless lvl.positive? && lvl <= max_level

    text = node["data-h#{lvl}"].to_s.strip
    text = node['data-heading'].to_s.strip if text.empty?
    text = node.text.to_s.strip if text.empty?
    next if text.empty?

    appendix_label = nil
    appendix_label = appendix_label_for_basename(basename) if include_appendix_label && lvl == 1

    chapter_token = node['data-chapter'].to_s.strip
    chapter_token = basename if chapter_token.empty?
    heading_attr = node['data-heading'].to_s.strip

    number_text = extract_number_text(node, lvl)
    title_text = extract_title_text(node, lvl, text)

    search_terms = build_search_terms(number_text, title_text, heading_attr, text, appendix_label)

    headings << {
      level: lvl,
      text: text,
      chapter: chapter_token,
      id: node['id'].to_s.strip,
      appendix_label: appendix_label,
      search_terms: search_terms,
      number_display: number_text
    }
  end
  headings
end

.extract_headings_from_markdown_file(path, max_level: 2) ⇒ Object

Markdownファイルから見出しを抽出



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 95

def extract_headings_from_markdown_file(path, max_level: 2)
  headings = []
  return headings unless File.exist?(path)

  title = nil
  subtitles = []
  File.foreach(path, encoding: 'utf-8') do |line|
    stripped = line.strip
    if max_level >= 1 && title.nil? && stripped.start_with?('# ')
      title = stripped.sub('\A#\\s+', '').strip
      next
    end
    subtitles << stripped.sub('\A##\\s+', '').strip if max_level >= 2 && stripped.start_with?('## ')
    break if max_level <= 2 && !title.nil? && !subtitles.empty?
  end
  headings << { level: 1, text: title } if title && !title.empty?
  if max_level >= 2
    subtitles.each do |text|
      next if text.empty?

      headings << { level: 2, text: text }
    end
  end
  headings
end

.extract_number_text(node, lvl) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 166

def extract_number_text(node, lvl)
  case lvl
  when 1
    val = node['data-chapter-number-display'].to_s.strip
    val = node.at_css('span.chapter-number')&.text&.strip if val.empty?
    val
  when 2
    val = node['data-section-number-display'].to_s.strip
    val = node.at_css('span.section-number')&.text&.strip if val.empty?
    val
  when 3
    val = node['data-subsection-number-display'].to_s.strip
    val = node.at_css('span.subsection-marker')&.text&.strip if val.empty?
    val
  end
end

.extract_title_text(node, lvl, fallback) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 183

def extract_title_text(node, lvl, fallback)
  title_text = case lvl
               when 1 then node['data-chapter-title'].to_s.strip
               when 2 then node['data-section-title'].to_s.strip
               when 3 then node['data-subsection-title'].to_s.strip
               else ''
               end
  title_text.empty? ? fallback : title_text
end

.heading_page_entries(pdf_path, html_paths, max_level: 3, start_page: 1) ⇒ Object

見出しとページの対応を取得



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 122

def heading_page_entries(pdf_path, html_paths, max_level: 3, start_page: 1)
  @last_outline_debug_info = nil
  return [] unless validate_inputs(pdf_path, html_paths)

  total_pages = (Build::Utilities.page_count(pdf_path) || '0').to_i
  return [] if total_pages <= 0

  from_base = start_page.to_i.clamp(1, total_pages)
  max_level = max_level.to_i.clamp(1, 6)

  chapter_paths = build_chapter_paths(html_paths)
  chapter_order = build_chapter_order(chapter_paths.keys)
  headings_by_chapter, chapter_markers = extract_all_headings(chapter_order, chapter_paths, max_level)
  search_helpers = build_search_helpers(pdf_path, total_pages)
  chapter_ranges = calculate_chapter_ranges(chapter_order, chapter_markers, search_helpers, from_base,
                                            total_pages)
  items, fallback_items = build_outline_items(headings_by_chapter, chapter_ranges, chapter_order,
                                              search_helpers, total_pages)
  items = add_toc_entry(items, chapter_ranges, chapter_order, search_helpers)

  log_fallback_items(fallback_items) if fallback_items.any?

  @last_outline_debug_info = {
    chapter_order: chapter_order.dup,
    chapter_starts: chapter_ranges.transform_values { |r| r[0] },
    chapter_ranges: chapter_ranges.transform_values(&:dup),
    items: items.map(&:dup)
  }

  items
end

.log_fallback_items(fallback_items) ⇒ Object



556
557
558
559
560
561
562
563
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 556

def log_fallback_items(fallback_items)
  return unless Common.current_log_level >= 3

  Common.log_warn('[Outline] 以下の見出しはページ検出に失敗したため章先頭へフォールバックしました:')
  fallback_items.each do |fb|
    Common.log_warn("  - #{fb[:chapter]} ##{fb[:text]} (fallback page=#{fb[:target_page]})")
  end
end

.page_range_backmatter(basename, ctx, prev_bn, default_markers) ⇒ Object

巻末ページ(用語集、終わりに、索引)のページ範囲を計算



444
445
446
447
448
449
450
451
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 444

def page_range_backmatter(basename, ctx, prev_bn, default_markers)
  search_from = [ctx[:chapter_starts][prev_bn] || ctx[:from_base], ctx[:from_base]].max.clamp(1,
                                                                                              ctx[:total_pages])
  markers = ctx[:chapter_markers][basename] || default_markers
  start_page = search_page_with_fallback(ctx[:search_markers], markers, search_from, ctx[:from_base],
                                         ctx[:total_pages])
  [start_page, ctx[:total_pages]]
end

.page_range_default(basename, ctx, prev_bn) ⇒ Object



453
454
455
456
457
458
459
460
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 453

def page_range_default(basename, ctx, prev_bn)
  search_from = [ctx[:chapter_starts][prev_bn] || ctx[:from_base], ctx[:from_base]].max.clamp(1,
                                                                                              ctx[:total_pages])
  markers = ctx[:chapter_markers][basename] || []
  start_page = search_page_with_fallback(ctx[:search_markers], markers, search_from, ctx[:from_base],
                                         ctx[:total_pages])
  [start_page, ctx[:total_pages]]
end

.page_range_first_chapter(ctx, prev_bn) ⇒ Object



437
438
439
440
441
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 437

def page_range_first_chapter(ctx, prev_bn)
  toc_end = ctx[:chapter_ranges]['_toc']&.[](1)
  start_candidate = toc_end ? toc_end + 1 : (ctx[:chapter_starts][prev_bn] || ctx[:from_base])
  [[start_candidate, ctx[:from_base]].max.clamp(1, ctx[:total_pages]), ctx[:total_pages]]
end

.page_range_legalpage(ctx) ⇒ Object



408
409
410
411
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 408

def page_range_legalpage(ctx)
  page = [2, ctx[:from_base]].max.clamp(1, ctx[:total_pages])
  [page, page]
end

.page_range_preface(ctx) ⇒ Object



413
414
415
416
417
418
419
420
421
422
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 413

def page_range_preface(ctx)
  start_page = [3, ctx[:from_base]].max.clamp(1, ctx[:total_pages])
  end_page = if ctx[:preface_pages].positive?
               (start_page + ctx[:preface_pages] - 1).clamp(1,
                                                            ctx[:total_pages])
             else
               start_page
             end
  [start_page, end_page]
end

.page_range_titlepage(ctx) ⇒ Object



404
405
406
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 404

def page_range_titlepage(ctx)
  [[ctx[:from_base], 1].max, 1]
end

.page_range_toc(ctx) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 424

def page_range_toc(ctx)
  preface_end = ctx[:chapter_ranges]['00-preface']&.[](1) || (3 + ctx[:preface_pages] - 1)
  start_candidate = ctx[:preface_pages].positive? ? preface_end + 1 : 3
  start_page = [start_candidate, ctx[:from_base]].max.clamp(1, ctx[:total_pages])
  end_page = if ctx[:toc_pages].positive?
               (start_page + ctx[:toc_pages] - 1).clamp(1,
                                                        ctx[:total_pages])
             else
               start_page
             end
  [start_page, end_page]
end

.prepend_cover_item(items) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 565

def prepend_cover_item(items)
  book_cfg = begin
    Common::CONFIG.fetch('book', {})
  rescue StandardError
    {}
  end
  main_title = book_cfg.fetch('main_title', '').to_s.strip
  fallback_title = book_cfg.fetch('title', '').to_s.strip
  cover_title = main_title.empty? ? fallback_title : main_title
  cover_title = '表紙' if cover_title.empty?

  return items if items.any? { |it| it[:page].to_i == 1 && it[:text] == cover_title }

  items.unshift({ level: 1, text: cover_title, page: 1, chapter: '_titlepage', id: nil })
  items
end

.prepend_label_if_needed(text, label) ⇒ Object



535
536
537
538
539
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 535

def prepend_label_if_needed(text, label)
  return text if label.to_s.empty? || text.start_with?(label)

  "#{label} #{text}".strip
end

.resolve_chapter_number_display(basename, heading) ⇒ Object



525
526
527
528
529
530
531
532
533
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 525

def resolve_chapter_number_display(basename, heading)
  number_display = heading[:number_display].to_s.strip
  return number_display unless number_display.empty?

  entry = TokenResolver::Resolver.new.resolve_file(basename)
  return '' unless entry.number&.to_i&.between?(11, 89)

  "#{entry.number.to_i - 10}"
end

.search_page_with_fallback(search_markers, markers, search_from, from_base, total_pages) ⇒ Object



462
463
464
465
466
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 462

def search_page_with_fallback(search_markers, markers, search_from, from_base, total_pages)
  start_page = search_markers.call(markers, search_from, total_pages)
  start_page ||= search_markers.call(markers, from_base, total_pages) if search_from > from_base
  start_page || search_from
end

.update_previous_chapter_end(chapter_ranges, prev_bn, start_page, total_pages) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 359

def update_previous_chapter_end(chapter_ranges, prev_bn, start_page, total_pages)
  return unless prev_bn && chapter_ranges[prev_bn]

  prev_end = [start_page - 1, total_pages].min
  prev_end = chapter_ranges[prev_bn][0] if prev_end < chapter_ranges[prev_bn][0]
  chapter_ranges[prev_bn][1] = prev_end
end

.validate_inputs(pdf_path, html_paths) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/vivlio/starter/cli/build/outline_extractor.rb', line 206

def validate_inputs(pdf_path, html_paths)
  unless File.exist?(pdf_path)
    Common.log_warn("[Outline] PDF が見つかりません: #{pdf_path}")
    return false
  end
  if html_paths.nil? || html_paths.empty?
    Common.log_warn('[Outline] HTML ファイルが指定されていません')
    return false
  end
  if html_paths.any? { |path| !File.exist?(path) }
    Common.log_warn('[Outline] HTML ファイルが存在しません')
    return false
  end
  unless system('which pdftotext >/dev/null 2>&1')
    Common.log_warn('[Outline] pdftotext が見つかりません。`brew install poppler` を実行してください。')
    return false
  end
  true
end