$(document).ready(function() {  
  $("*", "body").not("script,pre,code,kbd").contents().filter(function(){
    return this.nodeType == 3;    
    }).each(function(){
        this.nodeValue = transform(this.nodeValue);
    });
  
  function transform(str) {
    if (/^\s+$/.test(str)) {
      return str;
    }
    var doubleOpen = "\u201E", doubleClosed = "\u201C", singleOpen = "\u201A", singleClose = "\u2018";
    str = str.replace(/(^|\s|[\("\-])'([^']*)'($|[\.,;:!?\/"\)\-]|\s)/g, "$1" + singleOpen + "$2" + singleClose + "$3");
    str = str.replace(/(^|\s|\(|\-)'$/g, "$1" + singleOpen);
    str = str.replace(/^'([\.,;:!?\/\)\-]|\s)/g, singleClose + "$1");
    str = str.replace(/(^|\s|\(|\-)"([^"]*)"($|[\.,;:!?\/\)\-]|\s)/g, "$1" + doubleOpen + "$2" + doubleClosed+ "$3");
    str = str.replace(/(^|\s|\(|\-)"(\s*)$/g, "$1" + doubleOpen + "$2");
    str = str.replace(/^(\s*)"([\.,;:!?\/\)\-]|\s)/g, "$1" + doubleClosed + "$2");
    return str;
  }
});

