User:Chocolateboy/smart quotes.user.js

From Wikipedia, the free encyclopedia

Contents

Description

This Greasemonkey user script converts typewriter quotation marks to "smart" quotes, hyphens to en dashes, double hyphens to em dashes, and triple periods to ellipses. Text inside <pre>...</pre> and <code>...</code> is not affected.

Usage

  1. Install Greasemonkey >= 0.3
  2. Copy 'n' paste the script below and save it as smart_quotes.user.js
  3. Open it in Firefox and select Tools -> Install This User Script (or just click the Install button in Firefox >= 1.5)

Test

  • "double quotes" should appear as smart quotes: "double quotes"
  • single-hyphen should appear as an en dash: single-hyphen
  • double -- hyphen should appear as an em dash: double -- hyphen
  • ellipsis... should appear as a single character: ellipsis...

Script

// SmartQuotes
// version 0.14
// 2005-12-06 - 2005-12-07
// Copyright (c) 2005, chocolateboy
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script. To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "SmartQuotes", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          SmartQuotes
// @namespace     http://en.wikipedia.org/wiki/User:Chocolateboy
// @description   convert typewriter quotation marks into "smart" quotes
// @include       http://en.wikipedia.org/*
// @exclude       *Quotation_mark*
// @exclude       *diff=*
// ==/UserScript==
//
// --------------------------------------------------------------------
//
// This script is loosely based on DumbQuotes by Mark Pilgrim:
// http://diveintogreasemonkey.org/casestudy/dumbquotes.html
//

var currentQM = "\u201d";

var toggleQM = {
    "\u201c" : "\u201d",
    "\u201d" : "\u201c"
};

var replacements = [
    [ /\.\.\./g, "\u2026" ],  // Horizontal ellipsis 
    [ /--/g,     "\u2014" ],  // Em dash
    [ /-/g,      "\u2013" ]   // En dash
];

var textnodes = document.evaluate(
    "//body//text()[not(ancestor::pre or ancestor::code)]",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < textnodes.snapshotLength; ++i) {
    var node = textnodes.snapshotItem(i);
    var s = node.data;

    for (var j = 0; j < replacements.length; ++j) {
        var key = replacements[j][0];
        var val = replacements[j][1];
        s = s.replace(key, val);
    }

    while (s.indexOf('"') != -1) {
        currentQM = toggleQM[currentQM];
        s = s.replace('"', currentQM);
    }

    node.data = s;
}

Changelog

  • 0.01 Original version
  • 0.02 Only modify text nodes that are descendants of <p> nodes
  • 0.03 Fix ellipsis regex
  • 0.04 Further constrain ellipsis regex + correct JavaScript replacement pattern syntax
  • 0.05 Better handling of double quotes
  • 0.06 Access text node children of <body> rather than <p> so that <li> nodes are included
  • 0.07 Exclude preformatted text
  • 0.08 Properly fix ellipsis regex
  • 0.09 Order the replacements so that double hyphens are processed before single hyphens
  • 0.10 Remove redundant loop
  • 0.11 Var cleanup
  • 0.12 Exclude articles with unbalanced quotation marks
  • 0.13 Exclude diffs
  • 0.14 Refine exclusions

See also

External links