Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created August 31, 2012 14:43
Show Gist options
  • Save kardeiz/3553858 to your computer and use it in GitHub Desktop.
Save kardeiz/3553858 to your computer and use it in GitHub Desktop.
Replace curly quotes and other ugly MS Word conversions
# thanks to http://www.andornot.com/blog/post/Replace-MS-Word-special-characters-in-javascript-and-C.aspx
class UglyCharGsubber
# To replace characters in a string and return the substitution string
def self.replace(text)
text.
gsub(/[\u2018|\u2019|\u201A]/, "\'").
gsub(/[\u201C|\u201D|\u201E]/, "\"").
gsub(/\u2026/, "...").
gsub(/[\u2013|\u2014]/, "-").
gsub(/\u02C6/, "^").
gsub(/\u2039/, "<").
gsub(/\u203A/, ">").
gsub(/[\u02DC|\u00A0]/, " ")
end
# To modify a file in place using method above
def self.purify(file)
replace(File.read(file)).tap do |o|
File.open(file, 'w') { |f| f.write o }
end
"Purified your file: #{file}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment