Class: Vivlio::Starter::Commands::PdfReadCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/pdf/pdf_read_command.rb

Overview

PDF を Markdown に変換するコマンド

Standard Mode(テキストのみ)と Enhanced Mode(HexaPDF + 画像 + OCR)の2 段階で機能を提供する。vivlio-starter-pdf gem がインストール済みなら自動的に Enhanced Mode へ切り替わる。

Defined Under Namespace

Classes: Error, InvalidInputError, MissingPdfError

Constant Summary collapse

NUMERIC_PILLAR_PREFIX_REGEX =

「1.2 見出しテキスト」形式の柱(ランニングヘッド)を検出する正規表現

/\A[0-90-9]+(?:[.-][0-90-9]+)*\.?\s+.+\z/
DASHED_PAGE_NUMBER_REGEX =

「— 42 —」形式のダッシュ付きページ番号を検出する正規表現

/\A[-–—]\s*[0-90-9ivxlcdmIVXLCDM]+\s*[-–—]\z/
SOURCES_DIR =

PDF 格納ディレクトリの既定名

'sources'
MAX_AUTO_CHAPTER =

自動割り当て章番号の上限(01〜98)

98

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ PdfReadCommand

Returns a new instance of PdfReadCommand.

Parameters:

  • input (String)

    章トークンまたは PDF ファイルパス

  • options (Hash) (defaults to: {})

    CLI から渡されるオプション



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

def initialize(input, options = {})
  @input = input.to_s.strip
  @options = options || {}
  @resolver = CLI::TokenResolver::Resolver.new
  @catalog_entries = nil
  @plugin_checked = false
  @plugin_available = false
end

Instance Method Details

#callHash

PDF → Markdown 変換を実行する入力を解決し、モードに応じて Standard / Enhanced の変換パイプラインを呼び出す

Returns:

  • (Hash)

    :mode, :entry, :markdown_path, :source_pdf_path, :pages を含む結果

Raises:



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
# File 'lib/vivlio/starter/cli/pdf/pdf_read_command.rb', line 55

def call
  raise InvalidInputError, 'PDF を指定してください' if input.empty?

  entry, pdf_path = resolve_entry_and_pdf
  entry = ensure_unique_output_entry(entry)
  mode = resolved_mode
  log_enhanced_install_hint if mode == :standard

  ensure_entry_has_slug!(entry)
  add_catalog_entry_if_needed(entry)

  CLI::Common.log_action("[pdf:read] PDF からテキストを抽出します (#{entry.basename}, mode=#{mode})")

  result = mode == :enhanced ? convert_enhanced(pdf_path, entry) : convert_standard(pdf_path, entry)

  CLI::Common.log_success("[pdf:read] 変換が完了しました -> #{result[:markdown_path]}")

  {
    mode:,
    entry:,
    markdown_path: result[:markdown_path],
    source_pdf_path: pdf_path,
    pages: result[:pages]
  }
end