Class: String

Inherits:
Object show all
Defined in:
lib/ceedling/encodinator.rb

Overview

Patch the string class so that we have a nice shortcut for cleaning string encodings

Instance Method Summary collapse

Instance Method Details

#clean_encoding(safe_char = '') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ceedling/encodinator.rb', line 10

def clean_encoding(safe_char = '')
  begin
    # Clean up any oddball characters in an otherwise ASCII document
    encoding_options = {
      :invalid           => :replace,  # Replace invalid byte sequences
      :undef             => :replace,  # Replace anything not defined in ASCII
      :replace           => safe_char, # Use a safe character for those replacements
      :universal_newline => true       # Always break lines with \n
    }
  
    return self.encode("ASCII", **encoding_options).encode('UTF-8', **encoding_options)
  rescue 
    raise "String contains characters that can't be represented in standard ASCII / UTF-8."
  end 
  self 
end