Module: VivlioStarter::CLI::ChapterRename

Defined in:
lib/vivlio_starter/cli/chapter_rename.rb

Overview

章名の変更に追随すべき処理の登録簿

Defined Under Namespace

Classes: Follower

Constant Summary collapse

FOLLOWERS =

追随先の登録簿。ここへ 1 行足すだけで rename / renumber の両方に効く。

[
  Follower.new(label: 'catalog.yml', handler: method(:follow_catalog)),
  Follower.new(label: '画像ディレクトリ', handler: method(:follow_image_dir)),
  Follower.new(label: '索引辞書', handler: method(:follow_index_dictionary))
].freeze

Class Method Summary collapse

Class Method Details

.follow!(old_basename, new_basename, followers: FOLLOWERS) ⇒ Object

章名の変更を全追随先へ伝える。

1 つが失敗しても止めない。 原稿ファイルの移動は追随より先に済んでいるので、 途中で abort すると「ファイルは新しい名前、catalog は古い名前」という中途半端な 状態が残る。追随できなかったものを名指しで警告して先へ進むほうが復旧しやすい。

Parameters:

  • old_basename (String)

    例 '21-markdown-tutorial'

  • new_basename (String)

    例 '20-markdown-tutorial'

  • followers (Array<Follower>) (defaults to: FOLLOWERS)

    差し替え用(テストで失敗経路を作るため)



79
80
81
82
83
84
85
86
87
88
# File 'lib/vivlio_starter/cli/chapter_rename.rb', line 79

def follow!(old_basename, new_basename, followers: FOLLOWERS)
  followers.each do |follower|
    follower.handler.call(old_basename, new_basename)
  rescue StandardError => e
    Common.log_warn(
      "#{follower.label} が章名の変更に追随できませんでした: #{e.message}",
      detail: "#{old_basename}#{new_basename} の変更を手作業で反映してください"
    )
  end
end

.follow_catalog(old_basename, new_basename) ⇒ Object

catalog.yml の章名を差し替える



37
38
39
# File 'lib/vivlio_starter/cli/chapter_rename.rb', line 37

def follow_catalog(old_basename, new_basename)
  Build::CatalogUpdater.rename_chapter(old_basename, new_basename)
end

.follow_image_dir(old_basename, new_basename) ⇒ Object

images// を移す。 移動先が既にあるときは統合の判断が要るので、上書きせず著者へ委ねる。



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vivlio_starter/cli/chapter_rename.rb', line 43

def follow_image_dir(old_basename, new_basename)
  old_dir = File.join(Common::IMAGES_DIR, old_basename)
  return unless File.directory?(old_dir)

  new_dir = File.join(Common::IMAGES_DIR, new_basename)
  if File.exist?(new_dir)
    Common.log_warn("#{new_dir} が既に存在するため、画像ディレクトリは手動で統合してください")
    return
  end

  FileUtils.mv(old_dir, new_dir)
end

.follow_index_dictionary(old_basename, new_basename) ⇒ Object

索引辞書が持つ章名(main: と scanned_chapters)を差し替える。 main: は著者の判断=一次データなので、実在しない章を指していても捨てられない ——contexts のように「捨てて本文から拾い直す」ことができない。



59
60
61
# File 'lib/vivlio_starter/cli/chapter_rename.rb', line 59

def follow_index_dictionary(old_basename, new_basename)
  UnifiedTermsManager.new.rename_chapter!(old_basename, new_basename)
end