Class: VivlioStarter::CLI::PreProcessCommands::CrossReferenceProcessor::ReferenceReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/pre_process/cross_reference_processor.rb

Overview

参照置換クラス

Constant Summary collapse

REFERENCE_PATTERN =
/(?<![a-zA-Z0-9_.])@([\w-]+)/
PAGEREF_PATTERN =

ページ番号つき参照(at-directive-tier1-spec.md §2.4.2)。 generic の REFERENCE_PATTERN はコロンの手前までしか見ない(= @pageref だけを拾う)ため、 必ず generic より先に処理する。

/@pageref:([\w-]+)/
BARE_PAGEREF_PATTERN =

引数を書き忘れた裸の @pageref。generic 側では予約語として黙って素通しされるので、 ここで捕まえて書式を案内する。

/@pageref\b(?!:)/
MASKED_SPAN_PATTERN =

参照走査から除外するスパン(インライン code 以外の正当な @ 出現箇所):

  • Markdown リンク/画像 text: リンクテキスト・URL とも @ は正当な表現 (npm スコープ名 npmjs.com/@vivliostyle/cli 等)
  • 単独の角括弧 [ … ]: 索引・用語集の手動登録([用語|読み]・[@用語])や脚注参照 [^url1]
  • 裸 URL: リンク脚注化が追記する脚注定義行([^url1]: https://…/@scope/pkg)など、 角括弧の外に現れる URL 内の @
%r{`+[^`]*`+|!?\[[^\]]*\](?:\([^)]*\))?|https?://[^\s)]+}

Instance Method Summary collapse

Constructor Details

#initialize(content, labels_map, filename) ⇒ ReferenceReplacer

Returns a new instance of ReferenceReplacer.



581
582
583
584
585
586
587
# File 'lib/vivlio_starter/cli/pre_process/cross_reference_processor.rb', line 581

def initialize(content, labels_map, filename)
  @content = content
  @labels_map = labels_map
  @filename = filename
  @errors = []
  @used_ids = Set.new
end

Instance Method Details

#replaceObject



589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/vivlio_starter/cli/pre_process/cross_reference_processor.rb', line 589

def replace
  # コードブロックの除外は Masking(唯一の実装)へ委ねる。
  code_lines = CrossReferenceProcessor.code_line_numbers(@content)
  result = @content.lines.map.with_index(1) do |line, num|
    in_code = code_lines.include?(num)
    # 定義行(キャプション `** タイトル @id **` / 見出し `## タイトル @id`)は
    # 参照としてカウントしない(孤立ラベル検出が定義行を「使用済み」と誤認するため)
    next line if !in_code && definition_line?(line)

    in_code ? line : replace_in_line(line, num)
  end
  { content: result.join, errors: @errors, used_ids: @used_ids }
end