Class: Uniword::Template::TemplateParser

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/template/template_parser.rb

Overview

Parses template syntax from Word document comments.

Extracts template markers (variables, loops, conditionals) from comments attached to document elements. Converts Uniword template syntax into structured marker objects.

Responsibility: Marker extraction only Single Responsibility Principle: Does NOT render or validate

Template Syntax:

  • {variable} - Variable substitution

  • {object{object.property} - Nested property access

  • collection} - Loop start

  • {@end} - Loop/conditional end

  • condition} - Conditional start

  • condition} - Negative conditional

Examples:

Parse simple template

parser = TemplateParser.new(document)
markers = parser.parse
# Returns array of TemplateMarker objects

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ TemplateParser

Initialize parser with document

Parameters:

  • document (Document)

    Document to parse



32
33
34
35
# File 'lib/uniword/template/template_parser.rb', line 32

def initialize(document)
  @document = document
  @markers = []
end

Instance Method Details

#parseArray<TemplateMarker>

Parse all markers from document

Iterates through paragraphs and tables, extracting markers from comments. Returns markers sorted by document position.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/uniword/template/template_parser.rb', line 43

def parse
  @markers = []

  # Parse paragraphs
  parse_paragraphs(@document.paragraphs)

  # Parse tables
  parse_tables(@document.tables) if @document.tables.any?

  # Sort markers by document order
  @markers.sort_by(&:position)
end