MediaWiki:AutoTranslationMarkup.js: Difference between revisions
Content deleted Content added
GVarnum-WMF (talk | contribs) m GVarnum-WMF moved page Wikimedia:AutoTranslationMarkup.js to MediaWiki:AutoTranslationMarkup.js |
GVarnum-WMF (talk | contribs) mNo edit summary |
||
Line 12: | Line 12: | ||
//(as long as you're in edit mode. If you run it while just viewing a page, nothing happens) |
//(as long as you're in edit mode. If you run it while just viewing a page, nothing happens) |
||
$(transLink).click(function(){ |
$(transLink).click(function(){ |
||
var allText = $("# |
var allText = $("#mw-content-text").text(), |
||
texts = allText.split("\n"), |
texts = allText.split("\n"), |
||
n = 0, |
n = 0, |
||
Line 55: | Line 55: | ||
wikitext = wikitext.replace(/\* <translate>\*/g, "**"); //laziness involving bullets |
wikitext = wikitext.replace(/\* <translate>\*/g, "**"); //laziness involving bullets |
||
wikitext = wikitext.replace(/\<\/translate\>\<\/translate\>/g, "</translate>"); //laziness involving bullets |
wikitext = wikitext.replace(/\<\/translate\>\<\/translate\>/g, "</translate>"); //laziness involving bullets |
||
$( '# |
$( '#mw-content-text' ).val( wikitext ); |
||
// alert("Remember to check for <tvar ="NAME">s! There might be lots. Cmd+F them all!"); |
// alert("Remember to check for <tvar ="NAME">s! There might be lots. Cmd+F them all!"); |
||
}); //end click TransLink |
}); //end click TransLink |
Revision as of 19:03, 9 April 2024
//v1.1
//For documentation about this script, see https://meta.wikimedia.org/wiki/User:JSutherland_(WMF)/autoTranslationMarkup
// Please note that this script outputs markup that does not completely follow the guidelines for translation markup - it tags each line, which is messy. Please feel free to improve it.
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 transLink = mw.util.addPortletLink('p-cactions', '#', 'EasyTranslate', 'pt-EasyTranslate');
//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)
$(transLink).click(function(){
var allText = $("#mw-content-text").text(),
texts = allText.split("\n"),
n = 0,
wikitext;
for (i = 0; i < texts.length; i++) {
var temp = texts[i];
if(!temp.match(/(^[0-9,\.]+$)|(^\|\s?[0-9]+$)|(^\([0-9,\.]+\)$)|(^[\{\}].*?)|(^\[\[File:.*?)|(\[\[Category\:.*?)|(^<\/?\w+?>$)/)) {
// This will ignore nodes that are just numbers and punctuation (e.g. table entries), lines beginning with curly brackets (i.e. templates), Files, and Categories.
// and if node is not one of these...
if($.trim(texts[i]).length>0){ // ...check it is not empty
if (temp.match(/^(\s?\|\s+?\w+\s+?=\s?)(.*)/)) { //For templates
texts[i] = texts[i].replace(/^(\s?\|\s+?\w+\s+?=\s?)(.*)/, "$1<translate>$2</translate>");
} else if (temp.match(/^[\*\#;\|\!<]/)) { // catch things which need tagged individually
texts[i] = texts[i].replace(/^(\*+:?|#+:?|;)(.*)/, "$1 <translate>$2</translate>");
texts[i] = texts[i].replace(/^(\||\!)([^-\{\}+])(.*)/, "$1 <translate>$2$3</translate>");
texts[i] = texts[i].replace(/(<div.*?>|<small.*?>|<big.*?>)(.+?)(<.?div>|<.?small>|<.?big>)/g, "$1<translate>$2</translate>$3");
} else {
// Not sure if this is a good idea or not \/
// texts[i] = texts[i].replace(/([^<translate\>])(<\/?div.*?>|<\/?span.*?>|<\/?small.*?>|<\/?big.*?>)(.*?)(<\/?div.*?>|<\/?span.*?>|<\/?small.*?>|<\/?big.*?>)([^<translate\>])/g, "$1</tran" + "slate>$2<translate>$3</translate>$4<tran" + "slate>$5");
texts[i] = "<translate>" + texts[i] + "</translate>"; // ...then wrap the thing in translate tags
}
wikitext = wikitext + "\n" + texts[i]; // add the result to the wikitext
} else {
wikitext = wikitext + "\n"; // if the line was empty, just add a new line
}
} else {
wikitext = wikitext + "\n" + texts[i]; // if no tags were added but the line wasn't empty, add it back unchanged
}
}
// Adding "tvar"s - probably ought to have more foolproofing
wikitext = wikitext.replace(/\[\[([^\|]+)\]\]/g, "[[<tvar name=TEMPTEMPTVARNAME>$1</tvar>|$1]]")
.replace(/(\[\[[^<].*?)(\|)/g, "$1</>$2")
.replace(/\[\[([^<])/g, "[[<tvar name=TEMPTVARNAME>$1")
.replace(/(\[)([a-zA-Z])/g, "$1<tvar name=TEMPTVARNAME>$2")
.replace(/(http.*?)\s/g, "$1</tvar> ");
// This will number the tvar names so they're unique and don't need to be manually added
wikitext = wikitext.replace(/TEMPTVARNAME/g, function(){return ('"".++n') });
//now to pop the edited wikitext back into the edit window
wikitext = wikitext.replace(/^undefined\n/, "<noinclude><languages /></noinclude>\n"); //hack for lazy JS above
wikitext = wikitext.replace(/\* <translate>\*/g, "**"); //laziness involving bullets
wikitext = wikitext.replace(/\<\/translate\>\<\/translate\>/g, "</translate>"); //laziness involving bullets
$( '#mw-content-text' ).val( wikitext );
// alert("Remember to check for <tvar ="NAME">s! There might be lots. Cmd+F them all!");
}); //end click TransLink
}); //end doc.ready
}); //end mw.loader