Class: VivlioStarter::CLI::Metrics::ChapterParser
- Inherits:
-
Object
- Object
- VivlioStarter::CLI::Metrics::ChapterParser
- 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
-
#initialize(warning_checker) ⇒ ChapterParser
constructor
A new instance of ChapterParser.
-
#parse(path) ⇒ Object
ファイルパスから章メトリクスを生成する.
-
#parse_content(path, content) ⇒ Object
分量は本文(コードと記法を除いた地の文)で数える。 コードが多い章ほど「分量が十分」と判定されてしまう歪みを断つため (
chapter-volume-calibration-data.md§7.1)。.
Constructor Details
#initialize(warning_checker) ⇒ ChapterParser
Returns a new instance of ChapterParser.
27 28 29 |
# File 'lib/vivlio_starter/cli/metrics/chapter_parser.rb', line 27 def initialize(warning_checker) @warning_checker = warning_checker end |
Instance Method Details
#parse(path) ⇒ Object
ファイルパスから章メトリクスを生成する
32 33 34 35 36 37 |
# File 'lib/vivlio_starter/cli/metrics/chapter_parser.rb', line 32 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
分量は本文(コードと記法を除いた地の文)で数える。
コードが多い章ほど「分量が十分」と判定されてしまう歪みを断つため
(chapter-volume-calibration-data.md §7.1)。
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vivlio_starter/cli/metrics/chapter_parser.rb', line 42 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 = Analyzer.prose_length(content) warning = warning_checker.chapter_warning(chapter_num, total_chars) ChapterMetrics.new( path:, title:, chapter_num:, chars: total_chars, sections:, warning: ) end |