Class: Uniword::Generation::StructuredTextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/generation/structured_text_parser.rb

Overview

Parses structured text files into content elements.

Supports two input formats:

  • YAML (.yml/.yaml): content array with element, text, style keys

  • Markdown (.md): heading levels, notes, examples via conventions

Returns an array of hashes, each with :element, :text, :style, and optional :children keys.

Examples:

Parse YAML content

elements = StructuredTextParser.parse("content.yml")

Parse Markdown content

elements = StructuredTextParser.parse("document.md")

Class Method Summary collapse

Class Method Details

.parse(input_path) ⇒ Array<Hash>

Parse a structured text file into content elements.

Parameters:

  • input_path (String)

    Path to .yml, .yaml, or .md file

Returns:

  • (Array<Hash>)

    Array of content element hashes

Raises:

  • (ArgumentError)

    if file format is unsupported



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uniword/generation/structured_text_parser.rb', line 27

def self.parse(input_path)
  validate_path(input_path)

  ext = File.extname(input_path).downcase
  content = File.read(input_path, encoding: "UTF-8")

  case ext
  when ".yml", ".yaml"
    parse_yaml(content)
  when ".md"
    parse_markdown(content)
  else
    raise ArgumentError,
          "Unsupported format: #{ext}. Use .yml, .yaml, or .md"
  end
end