Class: Docxgen::Templates::Parser
- Inherits:
-
Object
- Object
- Docxgen::Templates::Parser
- Defined in:
- lib/docxgen/templates/parser.rb
Constant Summary collapse
- VARIABLE_REGEX =
/({{[[:space:]]?(([[:alpha:]]|_)+([[:alnum:]]|_|\.)*(?<!\.))[[:space:]]?}})/
Class Method Summary collapse
Class Method Details
.find_variables(tr) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/docxgen/templates/parser.rb', line 26 def self.find_variables(tr) tr.to_s.scan(VARIABLE_REGEX).map do |match| src, path, *_ = match { src: src, path: path.split(".").reject(&:empty?).map do |p| Integer(p) rescue next p.to_sym p.to_i end } end end |
.tr_substitute!(tr, data, remove_missing: true) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/docxgen/templates/parser.rb', line 8 def self.tr_substitute!(tr, data, remove_missing: true) found_variables = self.find_variables(tr) errors = [] found_variables.each do |var| value = data.dig(*var[:path]) errors.push("No value provided for variable: #{var[:src]}") if value.nil? next if value.nil? && !remove_missing # Don't replace variables with empty string if remove_missing is false tr.substitute(var[:src], value || "") end [tr, errors] end |