Class: LiquidXlsx::TemplateParser

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

Overview

Parses worksheet rows into an AST of template nodes.

Constant Summary collapse

FOR_START =

rubocop:disable Metrics/ClassLength

/\A\{%-?\s*for\s+(\w+)\s+in\s+([\w.]+)\s*-?%\}\z/
FOR_END =
/\A\{%-?\s*endfor\s*-?%\}\z/
IF_START =
/\A\{%-?\s*if\s+((?:(?!%\}).)+)\s*-?%\}\z/
ELSIF =
/\A\{%-?\s*elsif\s+((?:(?!%\}).)+)\s*-?%\}\z/
ELSE_TAG =
/\A\{%-?\s*else\s*-?%\}\z/
ENDIF =
/\A\{%-?\s*endif\s*-?%\}\z/
STRUCTURAL_KEYWORDS =
/\A\{%-?\s*(for|endfor|if|elsif|else|endif)\b/
MIXED_STRUCTURAL =
/\{%-?\s*(?:for|endfor|if|elsif|else|endif)\b/
PURE_STRUCTURAL =
[FOR_START, FOR_END, IF_START, ELSIF, ELSE_TAG, ENDIF].freeze
NON_STRUCTURAL_PAIR_RE =

Matches balanced pairs of non-structural Liquid blocks so they can be masked before MIXED_STRUCTURAL checks.

/
  \{%-?\s*
  (case|unless|capture|comment|raw|tablerow)
  \b
  .*?%\}
  .*?
  \{%-?\s*end\1\s*-?%\}
/mx

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, sheet_name = "Sheet1") ⇒ TemplateParser

Returns a new instance of TemplateParser.



29
30
31
32
33
# File 'lib/liquid_xlsx/template_parser.rb', line 29

def initialize(rows, sheet_name = "Sheet1")
  @rows = rows
  @sheet_name = sheet_name
  @pos = 0
end

Instance Attribute Details

#sheet_nameObject (readonly)

Returns the value of attribute sheet_name.



27
28
29
# File 'lib/liquid_xlsx/template_parser.rb', line 27

def sheet_name
  @sheet_name
end

Instance Method Details

#parseObject



35
36
37
38
39
# File 'lib/liquid_xlsx/template_parser.rb', line 35

def parse
  nodes = []
  nodes << parse_element while @pos < @rows.length
  nodes
end