User:JSutherland (WMF)/autoTranslationMarkupClean.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
// temp script to cleanup translation markup done incorrectly
// ### DOES NOT CURRENTLY WORK ###
mw.loader.using('mediawiki.util', function () {
$(document).ready(function () {
//create a button the user can click to indicate they want to run this script
//(ganked from https://www.mediawiki.org/wiki/ResourceLoader/Modules#addPortletLink)
var transCleanLink = mw.util.addPortletLink('p-personal', '#', 'EasyTranslateClean', 'pt-EasyTranslateClean');
//what happens when they click it? this!
//(as long as you're in edit mode. If you run it while just viewing a page, nothing happens)
$(transCleanLink).click(function(){
var allText = $("#wpTextbox1").text();
allText = allText.replace(/^(<)(translate>==)/gm, "<tran" + "slate>\n=="); // fix headers pt1
allText = allText.replace(/^(==?\s?)(<!--T:\d\d?-->)(.*?)(<\/translate>)$/gm, "$1$2\n$3"); // headers on new line (pt2)
allText = allText.replace(/^(<)(translate>)(<!--T:\d\d?-->(\s?)+)([^=]*?)(<\/translate>)/gm, "$3\n$5"); // strip translate tags
allText = allText.replace(/^([^<\n\s=\*#])/gm, "<translate>\n$1"); // Add translate tag back
allText = allText + "</translate>";
$( '#wpTextbox1' ).val( allText );
}); //end click TransLink
}); //end doc.ready
}); //end mw.loader