Class: Vivlio::Starter::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-]+)/

Instance Method Summary collapse

Constructor Details

#initialize(content, labels_map, filename) ⇒ ReferenceReplacer

Returns a new instance of ReferenceReplacer.



592
593
594
595
596
597
598
# File 'lib/vivlio/starter/cli/pre_process/cross_reference_processor.rb', line 592

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

Instance Method Details

#replaceObject



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/vivlio/starter/cli/pre_process/cross_reference_processor.rb', line 600

def replace
  in_code = false
  fence_marker = nil
  result = @content.lines.map.with_index(1) do |line, num|
    stripped = line.lstrip
    if (m = stripped.match(/\A(`{3,})/))
      fence = m[1]
      if in_code
        if fence == fence_marker
          in_code = false
          fence_marker = nil
        end
      else
        in_code = true
        fence_marker = fence
      end
    end
    # キャプション定義行(** タイトル @id **)は参照としてカウントしない
    next line if !in_code && CrossReferenceProcessor.extract_caption_label(line)

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