Class: VivlioStarter::CLI::Lint::DictManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/lint/dict_manager.rb

Overview

辞書ファイルのロードとキャッシュを管理する

Constant Summary collapse

PROJECT_DICT_DIR =

辞書の探索先。プロジェクト直下の config/(vs new が配置し、ユーザーが追加・編集できる)を 優先し、無ければ gem 同梱の scaffold コピーへフォールバックする。 ※ gem は lib/ 配下しかパッケージしない(gemspec の files が bin,lib/**/*)ため、 リポジトリ直下の config/ を指していた旧実装ではインストール済み gem で辞書が 0 件になり、 技術用語が軒並み誤検知されていた(CWD 相対の config/ なら開発リポジトリでもユーザー プロジェクトでも実在し、.textlintrc.yml の参照方法とも一貫する)。

'config/spellcheck_dictionaries'
PACKAGED_DICT_DIR =
File.expand_path('../../../project_scaffold/config/spellcheck_dictionaries', __dir__)
CACHE_DIR =
'.cache/spellcheck-dictionaries'
DICT_BASE_URL =
'https://raw.githubusercontent.com/streetsidesoftware/' \
'cspell-dicts/main/dictionaries'
GLOSSARY_PATH =
'config/index_glossary_terms.yml'

Instance Method Summary collapse

Instance Method Details

#build_word_map(config) ⇒ Hash

辞書をマージして word_map を返す

Parameters:

  • config (Object, nil)

    Common::CONFIG.spellcheck の値

Returns:

  • (Hash)

    { downcase_word => display_word }



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vivlio_starter/cli/lint/dict_manager.rb', line 48

def build_word_map(config)
  words = {}
  (bundled_dict_names + extra_dicts(config)).uniq.each do |name|
    path = resolve_path(name)
    load_into_word_map(path, words) if path
  end
  load_glossary_terms(words)
  # 除外リストは YAML なので read_user_words(YAML パーサ)で読む。
  # 辞書ファイルと同じ行単位の読み方をすると `- "High-Score"` が `-High-Score`
  # として登録され、ハイフンを含む語の除外が効かなくなる。
  read_user_words.each { register_word(words, it) }
  words
end

#bundled_dirObject

実際に使う辞書ディレクトリ(プロジェクト優先・gem 同梱フォールバック)



41
42
43
# File 'lib/vivlio_starter/cli/lint/dict_manager.rb', line 41

def bundled_dir
  Dir.exist?(PROJECT_DICT_DIR) ? PROJECT_DICT_DIR : PACKAGED_DICT_DIR
end

#register_user_words(new_words) ⇒ Object

未知語をユーザー辞書(user_dict_path)へ登録する。 既存語+新規語を大文字小文字無視で一意化し、辞書順に並べ替えてファイルを書き直す (編集過程の重複や未整列も毎回整える)。実際に追加した語の配列を返す。



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vivlio_starter/cli/lint/dict_manager.rb', line 65

def register_user_words(new_words)
  candidates = Array(new_words).map { it.to_s.strip }.reject(&:empty?).uniq
  return [] if candidates.empty?

  existing = read_user_words
  existing_down = existing.map(&:downcase)
  added = candidates.reject { |w| existing_down.include?(w.downcase) }
  return [] if added.empty?

  merged = (existing + added).uniq { it.downcase }.sort_by(&:downcase)
  write_user_words(merged)
  added
end

#user_dict_pathObject

スペルチェックの除外リスト(このプロジェクト固有の許可語)のパス。 プロジェクト直下の config/ に置く(隠しフォルダでなく見つけやすい。別の本でも 使いたければこのファイルをコピーすればよい)。vs lint --register の追記先。

名前は textlint 側の textlint_allowlist.yml と対にしてある——接頭辞が どのツールか、後半が何をするファイルかを表す(textlint_rewrite.yml が 「直す」、*_allowlist.yml が「指摘しない」)。

定数でなくメソッドにして呼び出し時の CWD を読む(テストで chdir しても追従する)。



36
37
38
# File 'lib/vivlio_starter/cli/lint/dict_manager.rb', line 36

def user_dict_path
  File.join('config', 'spellcheck_allowlist.yml')
end