User:ToxiBoi/WikidotCompatibility.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
var RegObj = [
	{ //KEEP THIS AT TOP
		"regex": /{{(.*)}}/g,
		"hotkey": "{"+"{code|$1}}", 
	},
	{
		"regex": /\[\[include (:|)(.*)\]\]/g,
		"hotkey": function(){
			alert("WARNING: Your text has one Wikidot template. This may cause malformed formatting. Make sure to use the Show Preview button.");
			return "{{$2}}<!-- This is a Wikidot template call(!)-->";
		},
	},
	{
		"regex": /\*\*(.*)\*\*/g,
		"hotkey": "'''$1'''",
	},
	{
		"regex": /\/\/(.*)\/\//g,
		"hotkey": "''$1''",
	},
	{
		"regex": /(\++) (.*)/g,
		"hotkey": function(str, reg){
			var str2 = (str.match(/\+/g) || []).length; // count number of +'s
			var end = "";
			var i;
			for (i = 0; i < str2; i++) {
			  end += "=";
			}
			end += " Heading ";
			for (i = 0; i < str2; i++) {
			  end += "=";
			}
			return end;
		},
	},
	{
		"regex": /> (.*)/g,
		"hotkey": "{"+"{blockquote|$1}}", 
	},
	{
		"regex": /(?!<!)--(.*)--(?!>)/g,
		"hotkey": "<s>$1</s>",
	}
];
if (mw.util.getParamValue( 'action' ) == 'edit' || mw.util.getParamValue( 'action' ) == 'submit') {
	mw.util.$content.append('<h2 id="toxiboi">ToxiBoi\'s Wikidot Importer</h2><button id="Wikidot" onclick="SetupWikidot()" class="mw-ui-button mw-ui-destructive">Open Wikidot UI</button>');
}
function SetupWikidot(){
	var elem = document.getElementById('Wikidot');
    if (elem !== null) {
    	elem.parentNode.removeChild(elem);
    }
    mw.util.$content.append('<textarea id="sourcetext" class="mw-editfont-monospace" placeholder="Wikidot wikitext goes here..." style="margin: 0px; height: 196px; width: 637px; max-width: 100%; min-width: 100%;"></textarea><br><button id="translate" onclick="Translate()" class="mw-ui-button mw-ui-progressive">Import Wikidot Data</button>');
}
function Translate(){
	var elem = document.getElementById('translate');
	var source2 = document.getElementById('sourcetext');
	var source = source2.value;
	var end = source;
	var att = document.createAttribute("disabled");
	document.getElementById('sourcetext').setAttributeNode(att);
	elem.innerHTML = "Converting...";
	RegObj.forEach(function(value,index,array){
		end = end.replace(value.regex,value.hotkey);
		console.log(value);
		console.log(end);
	});
	elem.innerHTML = "Moving to Edit Box...";
	document.getElementsByTagName("textarea")[0].value = end;
	elem.innerHTML = "Done! Removing...";
	source2.parentNode.removeChild(source2);
	elem.parentNode.removeChild(elem);
	/*var elemTitle = document.getElementById('toxiboi');
	elemTitle.parentNode.removeChild(elemTitle);*/
	SetupWikidot();//restart the loop!
}
// </nowiki>