Module: Coradoc::CoreModel::FrontmatterBlock::TextSplitter
- Defined in:
- lib/coradoc/core_model/frontmatter/text_splitter.rb
Overview
Format-agnostic text splitter for the YAML frontmatter block convention (‘—n…n—n` at the very start of a document).
Lives under FrontmatterBlock alongside Codec, SchemaResolver, and FieldTransform — together they form the complete frontmatter machinery (MECE):
TextSplitter — text → (frontmatter_text, body_text)
Codec — frontmatter_text ↔ typed FrontmatterBlock
SchemaResolver — typed FrontmatterBlock → validation errors
FieldTransform — typed FrontmatterBlock → transformed block
Format gems (Markdown, AsciiDoc, …) call this splitter at the top of their parse pipeline so frontmatter never reaches the format’s block parser. Single source of truth (DRY).
Defined Under Namespace
Classes: Result
Constant Summary collapse
- OPEN_DELIMITER =
'---'- CLOSE_DELIMITERS =
%w[--- ...].freeze
Class Method Summary collapse
Class Method Details
.call(text) ⇒ Result
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/coradoc/core_model/frontmatter/text_splitter.rb', line 37 def call(text) return Result.new(frontmatter: nil, body: '') if text.nil? || text.empty? lines = text.lines return empty_with(text) unless opens_frontmatter?(lines.first) close_index = find_close_line(lines, 1) return empty_with(text) if close_index.nil? frontmatter = lines[1...close_index].join body = lines[(close_index + 1)..].join body = body.sub(/\A\n+/, '') if body.start_with?("\n") Result.new(frontmatter: frontmatter, body: body) end |