Skip to content

Instantly share code, notes, and snippets.

@ponychicken
Last active August 29, 2015 14:13
Show Gist options
  • Save ponychicken/55d0be162033f10b299e to your computer and use it in GitHub Desktop.
Save ponychicken/55d0be162033f10b299e to your computer and use it in GitHub Desktop.
Fix Wikipedia quotes to typographical quotes
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;
};
var textarea = $("textarea#wpTextbox1");
var wikitext = textarea.val();
// We don't want to touch references
var selfclosedRefs = /<ref .*?\/>/g
var enclosedRefs = /<ref .*<\/ref>/g
var allRefs = /(<ref .*<\/ref>|<ref .*?\/>)/g
// Get all references
var matches = [];
var positions = [];
while ((match = allRefs.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);
console.log(curText)
curText = replaceQuotes(curText);
console.log(curText)
newWikiText += curText;
newWikiText += matches[i] || "";
curPos = pos.start + pos.length;
});
// Save
textarea.val(newWikiText)
// Helpers
$("#wpSummary").val("(t) use typographically correct quotes");
$("#wpMinoredit").click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment