User:DieBuche/vector.js

$.ajax({ dataType: 'script',  cache: true,  url: '//meta.wikimedia.org/w/index.php?title=User:Krinkle/Scripts/InsertWikiEditorButton.js&action=raw&ctype=text/javascript' }).done(function  {

// Monkey krInsertWikiEditorButton({   id: 'mw-customeditbutton-anotherspecialbutton',    icon: '//upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Gnome-face-monkey.svg/22px-Gnome-face-monkey.svg.png',    label: 'Insert monkey',    callback: function (e) {      fixQuotes;    }  });

});

var currentQM = "\u201d";

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

var skip = 0;

function replaceQuotes(text) { var newText = ""; for (var i = 0; i < text.length; i++) {

if (text[i] == '"') {     // Seems to be some table property or similar      if (text[i - 1] == "=") {        skip = 1;        newText += text[i];        continue;      }

if (skip) { skip--; newText += text[i]; continue; }     currentQM = toggleQM[currentQM]; newText += currentQM; } else { newText += text[i]; } }

return newText; }

function fixSingles(text) { text = text.replace(/(\w)'(\w)/g, '$1’$2'); text = text.replace(/(\s)'(\w)/g, '$1‘$2'); text = text.replace(/(\w)'(\s)/g, '$1’$2'); text = text.replace(/(\w)'([\,\.\:\;”])/g, '$1’$2'); return text; }

function fixQuotes { var textarea = $("textarea#wpTextbox1"); var wikitext = textarea.val;

// We don't want to touch references or links var exclusions = /(| |\[+.*?\]+|\{\{.*?\}\})/g;

// Get all references var matches = []; var positions = [];

while ((match = exclusions.exec(wikitext)) !== null) { positions.push({     start: match.index,      length: match[0].length    }); matches.push(match[0]); }

// Add a last empty pos positions.push({   start: wikitext.length,    length: 0  });

// Get text until next match and replace quotes var curPos = 0; var newWikiText = "";

positions.forEach(function (pos, i) {

var curText = wikitext.substring(curPos, pos.start); curText = replaceQuotes(curText); curText = fixSingles(curText); newWikiText += curText; newWikiText += matches[i] || "";

curPos = pos.start + pos.length; });

// Save textarea.val(newWikiText);

// Helpers $("#wpSummary").val($("#wpSummary").val + "(t) use typographically correct quotes"); $("#wpMinoredit").click; }