Jump to content

User:Torana/typography.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// typography fix
// dynamically turns the substitute characters " and ' into the typographically correct ones (primes or “curly” quotes and apostrophes) when reading.
// Author: Terry Ngai; Source: https://greasyfork.org/de/scripts/28804-smart-quotes

(function() {
 var textnodes, node, s;

textnodes = document.evaluate( "//body//text()[not(ancestor::pre) and not(ancestor::textarea) and not(ancestor::code) and not(ancestor::input) and not(ancestor::script)]",
document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < textnodes.snapshotLength; i++) {
   node = textnodes.snapshotItem(i);
   s = node.textContent;
      s = s.replace(/'''/g, "‴");
      s = s.replace(/(\W|^)"(\S)/g, "$1“$2");
      s = s.replace(/(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/g, "$1”$2");
      s = s.replace(/([^0-9])"/g, "$1”");
      s = s.replace(/''/g, "″");
      s = s.replace(/(\W|^)'(\S)/g, "$1‘$2");
      s = s.replace(/([a-z])'([a-z])/gi, "$1’$2");
      s = s.replace(/((\u2018[^']*)|[a-z])'([^0-9]|$)/gi, "$1’$3");
      s = s.replace(/(\u2018)([0-9]{2}[^\u2019]*)(\u2018([^0-9]|$)|$|\u2019[a-z])/gi, "’$2$3");
      s = s.replace(/(\B|^)\u2018(?=([^\u2019]*\u2019\b)*([^\u2019\u2018]*\W[\u2019\u2018]\b|[^\u2019\u2018]*$))/gi, "$1’");
      s = s.replace(/'/g, "′");
   node.data = s;
}

})();

// [[Category:Wikipedia scripts]]