Created
February 3, 2010 06:57
-
-
Save stephencelis/293412 to your computer and use it in GitHub Desktop.
Chinos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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