Class: Vivlio::Starter::CLI::Metrics::ChapterParser

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/metrics/chapter_parser.rb

Overview

Markdown から章・節構造を解析する

Constant Summary collapse

H1_PATTERN =
/^#\s+(.+)$/
H2_PATTERN =
/^##\s+(.+)$/
CHAPTER_NUM_PATTERN =
/^(\d+)-/

Instance Method Summary collapse

Constructor Details

#initialize(warning_checker) ⇒ ChapterParser

Returns a new instance of ChapterParser.



25
26
27
# File 'lib/vivlio/starter/cli/metrics/chapter_parser.rb', line 25

def initialize(warning_checker)
  @warning_checker = warning_checker
end

Instance Method Details

#parse(path) ⇒ Object

ファイルパスから章メトリクスを生成する



30
31
32
33
34
35
# File 'lib/vivlio/starter/cli/metrics/chapter_parser.rb', line 30

def parse(path)
  content = File.read(path, encoding: 'UTF-8')
  parse_content(path, content)
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
  blank_chapter(path)
end

#parse_content(path, content) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vivlio/starter/cli/metrics/chapter_parser.rb', line 37

def parse_content(path, content)
  chapter_num = extract_chapter_num(path)
  title = extract_title(content) || File.basename(path, '.md')
  sections = parse_sections(content, chapter_num)
  total_chars = content.delete("\r\n").length

  warning = warning_checker.chapter_warning(chapter_num, total_chars)

  ChapterMetrics.new(
    path:,
    title:,
    chapter_num:,
    chars: total_chars,
    sections:,
    warning:
  )
end