Class: XmlProcessor

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

Constant Summary collapse

BROKEN_VARIABLE_PATTERN =
"<w:t>([^\#^<^>]*?)#([^\#^<^>]*?)<\/w:t>"

Class Method Summary collapse

Class Method Details

.clean_spaces_inside_variables(match) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/docx/xml_processor.rb', line 21

def self.clean_spaces_inside_variables(match)
    ret = match.dup
    cleaner_methods = Cleaners.methods.grep /_cleaner/
    cleaner_methods.each do |clean_method|
        ret = Cleaners.send(clean_method, ret)
    end
    puts "Returning cleaned variable: #{ret}"
    ret
end

.clean_variables(document) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/docx/xml_processor.rb', line 6

def self.clean_variables(document)
    document.gsub(/\{\{.*?\}\}/m) do |match|
        ret = match.include?('>') ? join_variable_tags(match) : match
        ret = clean_spaces_inside_variables(ret)
        ret
    end
end

.join_variable_tags(match) ⇒ Object



14
15
16
17
18
19
# File 'lib/docx/xml_processor.rb', line 14

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