Jump to content

Wikipedia:AutoEd/curlyfixer.js

From Wikipedia, the free encyclopedia
function autoEdCurlyFixer (str) {

	// First check to see if there is anything to fix
	if(str.search(/[“”‘’]/g) >= 0) {

		// Maybe something to fix, now check to see if we can safely mark curlies we shouldn't change
		if(str.search(/[\uE000\uE001\uE002\uE003]/g) < 0) { // see [[Private Use Area]]

			// Okay, we can mark, so now mark places where we shouldn't change curlies
			old = str + ' ';
			while (old != str) {
				old = str;
				// Mark places where we shouldn't change curlies
				str = str.replace(/(\[\[[:\t ]*(?:File|Image)[\t ]*:[^\|\[\]]*)[“]/gi, "$1\uE000");
				str = str.replace(/(\[\[[:\t ]*(?:File|Image)[\t ]*:[^\|\[\]]*)[”]/gi, "$1\uE001");
				str = str.replace(/(\[\[[:\t ]*(?:File|Image)[\t ]*:[^\|\[\]]*)[‘]/gi, "$1\uE002");
				str = str.replace(/(\[\[[:\t ]*(?:File|Image)[\t ]*:[^\|\[\]]*)[’]/gi, "$1\uE003");
			}

			// Replace the curlies
			str = str.replace(/[“”]/g, '"').replace(/[‘’]/g, "'");

			// Unmark
			str = str.replace(/\uE000/g, "“");
			str = str.replace(/\uE001/g, "”");
			str = str.replace(/\uE002/g, "‘");
			str = str.replace(/\uE003/g, "’");
		}
	}
	return str;
}