Skip to content

Instantly share code, notes, and snippets.

@stephencelis
Created February 3, 2010 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephencelis/293412 to your computer and use it in GitHub Desktop.
Save stephencelis/293412 to your computer and use it in GitHub Desktop.
Chinos
# encoding: utf-8
#
# Smarter than SmartyPants? For plain text.
module Chinos
extend self
def educate(text)
educate_quotes educate_ellipses(educate_dashes(text))
end
def stupefy(text)
stupefy_quotes stupefy_ellipses(stupefy_dashes(text))
end
private
def educate_dashes(text)
text.gsub(/(\d)-(\d)/, '\1–\2').gsub(/ - /, ' – ').
gsub(/(\b| |^)---(\b| |$)|-{2}/, '\1—\2')
end
def stupefy_dashes(text)
text.gsub('–', '-').gsub('—', '--')
end
def educate_ellipses(text)
text.gsub(/\. ?\. ?\. ?/, "…")
end
def stupefy_ellipses(text)
text.gsub('…', '...')
end
def educate_quotes(text)
text.gsub(/"([^"]*)"?/, '“\1”').
gsub(/( |^)'(.+?)'( |$)/, '\1‘\2’\3').
gsub(/([[:punct:]]| |^)'((?:[^']|[^' ]+'[^' ]+)+)'([[:punct:]]| |$)/,
'\1‘\2’\3').
gsub(/'/, '’')
end
def stupefy_quotes(text)
text.gsub(/[“”]/, '"').gsub(/[‘’]/, "'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment