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



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

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

#escape_xmlObject



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

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

#mgsub(key_value_pairs = {}) ⇒ Object



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

def mgsub(key_value_pairs = {})
  regexp_fragments = key_value_pairs.keys
  gsub(Regexp.union(*regexp_fragments)) do |match|
    key_value_pairs.detect { |k, _v| k =~ match }[1]
  end
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