Skip to content

Instantly share code, notes, and snippets.

@karbassi
Created August 12, 2013 23:34
Show Gist options
  • Save karbassi/6216412 to your computer and use it in GitHub Desktop.
Save karbassi/6216412 to your computer and use it in GitHub Desktop.
Really small jQuery plugin to convert text with straight quotes to curly/smart quotes.
<!DOCTYPE html>
<head>
<title>Example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<section class="what">
<div class="awesome">
<div class="classnames">
<article class="i-have">
<h1>It's a good day.</h1>
<h2>What's up my friend?</h2>
<p>He said, "She's my best friend."</p>
<a href="#">Go "Here", I say.</a>
</article>
</div>
</div>
</section>
<script src="jquery.curlies.js"></script>
<script>
$(document).ready(function () {
$('body').curlies();
});
</script>
</body>
</html>
(function($){
$.fn.curlies = function( itemQuery ) {
function smarten(text) {
return text
/* opening singles */
.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018")
/* closing singles & apostrophes */
.replace(/'/g, "\u2019")
/* opening doubles */
.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c")
/* closing doubles */
.replace(/"/g, "\u201d")
/* em-dashes */
.replace(/--/g, "\u2014");
};
return this.each(function(){
var $this = $(this);
var $children = $this.children();
if ($children.length) {
$children.each(function(){
$(this).curlies();
});
} else {
$this.text( smarten( $this.text() ) );
}
});
};
})(jQuery);
@jenlampton
Copy link

Thanks for this! I turned it into a really tiny Drupal module if anyone needs it:
https://www.drupal.org/sandbox/jenlampton/2453655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment