User:BPositive/prepare.js

mw.loader.using( ['jquery.ui', 'mediawiki.api'], function {	//Function to show the content in a dialog box	function renderContentDialog ( content ) {		var $dialog = $( '  ' )			.html ( content )			.dialog ({ autoOpen: true, title: 'Text after preparing the page ' + wgPageName + " for translation", width: '70%', modal: true });	}	function addLanguageBar(pageContent){		//&lt; and &gt; are just for display purpose. Will be substituded with    	pageContent = "&lt;languages/&gt;\n" + pageContent.replace(/+/gi, "");    	console.log(pageContent);    	//regexp = +    	return pageContent;    }	function fixInternalLinks ( pageContent ) {		var normalizeRegex = new RegExp( /\[\[([^|]*?)\]\]/gi );		// First convert all links into two-party form. If a link is not having a pipe,		// add a pipe and duplicate the link text		// Regex : http://regex101.com/r/pO9nN2		pageContent = pageContent.replace( normalizeRegex, "$1");		var linkPrefixRegex = new RegExp( /\[\[(?!Category:|m:|w:)(.*?)\]\]/gi );		// Add the Special:MyLanguage/ prefix for all internal links except those starting		// with Category, w:, m:. Other namespaces can be added eventually.		// Regex : http://regex101.com/r/dG5zA3 		pageContent = pageContent.replace( linkPrefixRegex, "Special:MyLanguage/$1"); return pageContent; }	function fixFiles( pageContent ) { var captionFilesRegex = new RegExp( /\[\[([Ff]ile.*\|)(.*?)\]\]/gm ); // Add translate tags for files with captions // Regex: http://regex101.com/r/zM0cI7 pageContent = pageContent.replace( linkPrefixRegex, "&lt;/translate&gt;$1&lt;translate&gt;$2&lt;/translate&gt;&lt;translate&gt;"); var fileRegex = new RegExp( /\[\[([Ff]ile[^|]*?)\]\]/gm ); // Add translate tags for files without captions // Regex : http://regex101.com/r/cB3xJ9 pageContent = pageContent.replace( linkPrefixRegex, "&lt;/translate&gt;$1&lt;translate&gt;"); return pageContent; }	///w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&titles=User%3ABPositive //Function to prepare the page for translation function prepareForTranslation{ var api = new mw.Api; //This api call returns the raw source text of the page api.get ({	       action:'query',	        prop: 'revisions',	        format: 'json',	        rvprop: 'content',	        rvlimit: '1',	        titles: wgPageName	    }).done ( function( data ) {	    	var pageContent = data['query']['pages'][wgArticleId]['revisions'][0]['*'];	    	//pageContent.replace('\\n', ' ');	    	pageContent = addLanguageBar( pageContent );	    	pageContent = fixInternalLinks( pageContent );	    	pageContent = fixFiles( pageContent );	    	renderContentDialog(pageContent);	    }); }   $(document).ready( function {		// Add a link to the toolbox		var link = mw.util.addPortletLink( 'p-tb', '#',			'Prepare', 't-preparetranslation', 'Prepare the page for translation', null, '#t-whatlinkshere' );		$(link).click( function( e ) { // Avoid the browser going to '#' e.preventDefault; prepareForTranslation; });	}); });