Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/openxml_docx_templater/escape_xml.rb

Constant Summary collapse

CODE_LINE_PATTERN =
/{(.*?)%(.+?)%(.*?)}/
TAG_PATTERN =
/<(.+?)>[\n]?/m

Instance Method Summary collapse

Instance Method Details

#convert_newlinesObject



10
11
12
13
# File 'lib/openxml_docx_templater/escape_xml.rb', line 10

def convert_newlines
  gsub!("\n", '<w:br/>')
  self
end

#escape_xmlObject



6
7
8
# File 'lib/openxml_docx_templater/escape_xml.rb', line 6

def escape_xml
  mgsub!([[/&/, '&amp;'], [/</, '&lt;'], [/>/, '&gt;']])
end

#mgsub!(key_value_pairs = [].freeze) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/openxml_docx_templater/escape_xml.rb', line 19

def mgsub!(key_value_pairs=[].freeze)
  regexp_fragments = key_value_pairs.collect { |k, v| k }
  gsub!(Regexp.union(*regexp_fragments)) do |match|
    key_value_pairs.detect { |k, v| k =~ match }[1]
  end
  self
end

#refactObject



15
16
17
# File 'lib/openxml_docx_templater/escape_xml.rb', line 15

def refact
  gsub(CODE_LINE_PATTERN) { |result| result.gsub(TAG_PATTERN, "") }
end