Class: Docx::Builder::XmlProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/docx/builder/xml_processor.rb

Overview

Class responsible for dealing with xml nodes in-between the variables

Class Method Summary collapse

Class Method Details

.clean_spaces_inside_variables(match) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/docx/builder/xml_processor.rb', line 25

def self.clean_spaces_inside_variables(match)
  ret = match.dup
  cleaner_methods = Docx::Builder::Cleaners.methods.grep(/_cleaner/)
  cleaner_methods.each do |clean_method|
    ret = Docx::Builder::Cleaners.send(clean_method, ret)
  end
  ret
end

.clean_variables(document) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/docx/builder/xml_processor.rb', line 7

def self.clean_variables(document)
  document.gsub(/\{\{.*?\}\}/m) do |match|
    ret = match.include?('>') ? join_variable_tags(match) : match
    Docx::Builder.logger.debug("Before cleaning: #{ret}")
    ret = Docx::Builder::Cleaners.single_quote_cleaner(ret)
    ret = insert_null_placeholder(ret)
    Docx::Builder.logger.debug("After cleaning: #{ret}")
    ret
  end
end

.insert_null_placeholder(expression) ⇒ Object



34
35
36
37
38
39
# File 'lib/docx/builder/xml_processor.rb', line 34

def self.insert_null_placeholder(expression)
  placeholder = Docx::Builder.configuration.null_placeholder
  expression.gsub(/\{\{%\s(.+?)\s%\}\}/) do |_m|
    "{{% (#{Regexp.last_match(1)} || '#{placeholder}') %}}"
  end
end

.join_variable_tags(match) ⇒ Object



18
19
20
21
22
23
# File 'lib/docx/builder/xml_processor.rb', line 18

def self.join_variable_tags(match)
  before = match.gsub(/<(.*)/m, '')
  inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
  after = match.gsub(/.*>([^>]+)$/m, '\\1')
  [before, inside, after].join
end