// 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;
}
