Class: MODL::Parser::Substitutions

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/substitutions.rb

Overview

Escape-sequence replacements for MODL files.

Constant Summary collapse

@@subs =
[
%w(~% %),
%w(\\% %),
%w(~\\ \\),
%w(\\\\ \\),
%w(~~ ~),
%w(\\~ ~),
%w{~( (},
%w{\\( (},
%w{~) )},
%w{\\) )},
%w(~[ [),
%w(\\[ [),
%w(~] ]),
%w(\\] ]),
%w(~{ {),
%w(\\{ {),
%w(~} }),
%w(\\} }),
%w(~; ;),
%w(\\; ;),
%w(~: :),
%w(\\: :),
%w(~` `),
%w(\\` `),
%w(~" "),
%w(\\" "),
%w(~= =),
%w(\\= =),
%w(~/ /),
%w(\\/ /),
%w(< <),
%w(\\< <),
%w(~> >),
%w(\\> >),
%w(~& &),
%w(\\& &),
%w(! !),
%w(\\! !),
%w(~| |),
%w(\\| |),
['\\t', "\t"],
['\\n', "\n"],
['\\b', "\b"],
['\\f', "\f"],
['\\r', "\r"]

Class Method Summary collapse

Class Method Details

.convert_unicode(s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/modl/parser/substitutions.rb', line 96

def self.convert_unicode(s)
  uni_str_idx = s.index('\u')
  uni_str_idx = s.index('~u') if uni_str_idx.nil?
  return s if uni_str_idx.nil?

  value = s.slice(uni_str_idx + 2, 4).to_i(16)
  uni_str = s.slice(uni_str_idx, 6)
  uni_val = value.chr(Encoding::UTF_8)
  result = s.sub(uni_str, uni_val)
  return convert_unicode result
end

.process(str) ⇒ Object

Replace all escape sequences in the supplied string and return the new value.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/modl/parser/substitutions.rb', line 79

def self.process(str)
  return str unless str.is_a? String

  # Remove unescaped graves and double quotes
  new_str = Sutil.unquote(str)

  # Handle escape sequences
  @@subs.each do |s|
    loop do
      prev = new_str
      new_str = new_str.sub(s[0], s[1])
      break unless new_str && new_str != prev
    end
  end
  convert_unicode new_str
end