User:Santaferra

This is my attempt at converting Tables in javascript from HTML to MediaWiki format.

var fallBackExtensions = "Calendar,Gnuplot,KwInclude,InputBox,kw_include_file";

function aboutMe{ //Local Variables for Current WYSIWYG var majorVersion = "0"; var minorVersion = "4"; return "Version: " + majorVersion + "." + minorVersion; } var is = new Is; //calendar,kwinclude,gnuplot function convertWiki(source, extensions){ //This one liner is to remove all carriage returns that MSIE uses and replace them with newlines source = source.replace(/\n\r/g, "\n"); source = source.replace(//mgi, "\n"); source = source.replace(/\n \n/g, "\n"); source = source.replace(/\&nbsp\;/mgi, "");

//Local Variables var firstPos = 0; var secondPos = 0; var currentPos = 0; var refPos = 0; var sbt = "";      //String Before Token var sat = "";      //String After Token var convertStr = source; //String to Convert var token = ""; var tempStr = "";  //Used for random Processing var linkStr = "";  //String for holding Links var linkTextStr = ""; //String for holding the text of links var i = 0; //For Loop Counter var tempChar = "";  //For Checking Previous Characters //var align = "", frameOrThumb = "", width = "", height = ""; var indentStack = "\n"; //List of Extensions to Support var extList = []; if(extensions != null){ extensions = extensions.toLowerCase; extList = extensions.split(','); }	else{ extensions = fallBackExtensions; extensions = extensions.toLowerCase; extList = extensions.split(','); }	var found = false;

//BUILD Conversion Variable Here var toWiki = {}; //Tokens to Search For toWiki.start = {}; toWiki.start[''] = "'''"; toWiki.start[''] = "'''"; toWiki.start[''] = "''"; toWiki.start[''] = "''"; toWiki.start[''] = "="; toWiki.start[''] = "=="; toWiki.start[''] = "==="; toWiki.start[''] = "===="; toWiki.start[''] = "====="; toWiki.start[''] = "======"; toWiki.start[''] = "\n"; toWiki.start['<lt;br />'] = "\n"; toWiki.start['<lt;hr />'] = ""; toWiki.start['<lt;pre>'] = "<lt;pre>"; toWiki.start['<lt;table'] = "<lt;table"; toWiki.start['<lt;a'] = "<lt;a"; toWiki.start['<lt;math>'] = "<lt;math>"; toWiki.start['<lt;nowiki>'] = "<lt;nowiki>"; toWiki.start['<lt;dl>'] = ""; toWiki.start['<lt;ol>'] = ""; toWiki.start['<lt;ul>'] = ""; toWiki.start['<lt;/dl>'] = ""; toWiki.start['<lt;/ol>'] = ""; toWiki.start['<lt;/ul>'] = ""; toWiki.start['<lt;dd>'] = ""; toWiki.start['<lt;dt>'] = ""; toWiki.start['<lt;li>'] = ""; toWiki.start['<lt;img'] = "<lt;img"; toWiki.start['<lt;div'] = "<lt;div"; toWiki.start['<lt;!--'] = "<lt;!--"; toWiki.start['<lt;tt>'] = "<lt;tt>"; //Token Matching Pattern toWiki.match = {}; toWiki.match['<lt;b>'] = "<lt;/b>"; toWiki.match['<lt;strong>'] = "<lt;/strong>"; toWiki.match['<lt;em>'] = "<lt;/em>"; toWiki.match['<lt;i>'] = "<lt;/i>"; toWiki.match['<lt;h1>'] = "<lt;/h1>"; toWiki.match['<lt;h2>'] = "<lt;/h2>"; toWiki.match['<lt;h3>'] = "<lt;/h3>"; toWiki.match['<lt;h4>'] = "<lt;/h4>"; toWiki.match['<lt;h5>'] = "<lt;/h5>"; toWiki.match['<lt;h6>'] = "<lt;/h6>"; toWiki.match['<lt;p>'] = "<lt;/p>"; toWiki.match['<lt;br />'] = ""; toWiki.match['<lt;hr />'] = ""; toWiki.match['<lt;pre>'] = "voodoo"; toWiki.match['<lt;table'] = "voodoo"; toWiki.match['<lt;a'] = "voodoo"; toWiki.match['<lt;math>'] = "voodoo"; toWiki.match['<lt;nowiki>'] = "voodoo"; toWiki.match['<lt;dl>'] = "voodoo"; toWiki.match['<lt;ol>'] = "voodoo"; toWiki.match['<lt;ul>'] = "voodoo"; toWiki.match['<lt;/dl>'] = "voodoo"; toWiki.match['<lt;/ol>'] = "voodoo"; toWiki.match['<lt;/ul>'] = "voodoo"; toWiki.match['<lt;dd>'] = "voodoo"; toWiki.match['<lt;dt>'] = "voodoo"; toWiki.match['<lt;li>'] = "voodoo"; toWiki.match['<lt;img'] = "voodoo"; toWiki.match['<lt;div'] = "voodoo"; toWiki.match['<lt;!--'] = "voodoo"; toWiki.match['<lt;tt>'] = "<lt;/tt>"; //Token Ending Pattern toWiki.end = {}; toWiki.end['<lt;/b>'] = "'''"; toWiki.end['<lt;/strong>'] = "'''"; toWiki.end['<lt;/em>'] = "''"; toWiki.end['<lt;/i>'] = "''"; toWiki.end['<lt;/h1>'] = "="; toWiki.end['<lt;/h2>'] = "=="; toWiki.end['<lt;/h3>'] = "==="; toWiki.end['<lt;/h4>'] = "===="; toWiki.end['<lt;/h5>'] = "====="; toWiki.end['<lt;/h6>'] = "======"; toWiki.end['<lt;/p>'] = ""; toWiki.end['<lt;pre>'] = "<lt;/pre>"; toWiki.end['<lt;table'] = "<lt;/table>"; toWiki.end['<lt;a'] = "<lt;/a>"; toWiki.end['<lt;math>'] = "<lt;/math>"; toWiki.end['<lt;nowiki>'] = "<lt;/nowiki>"; toWiki.end['<lt;dd>'] = "<lt;/dd>"; toWiki.end['<lt;dt>'] = "<lt;/dt>"; toWiki.end['<lt;li>'] = "<lt;/li>"; toWiki.end['<lt;!--'] = "-->"; toWiki.end['<lt;/tt>'] = "<lt;/tt>"; /*************Main Body of Algorithm*************/ //Preserve Formatting (This is a fallback for TinyMCE's settings) convertStr = convertStr.replace(/&lt;/g,"<lt;"); convertStr = convertStr.replace(/&gt;/g,">"); convertStr = convertStr.replace(/&quot;/g,"\"");	convertStr = convertStr.replace(/\<lt;br \/\>/g, "\n");	convertStr = convertStr.replace(/\<lt;br\>/g, "\n");

//WHILE Current Position of Parsing is Not at end of String while(currentPos <lt; convertStr.length && currentPos > -1){ //Grab First Token firstPos = convertStr.indexOf("<lt;", currentPos); secondPos = convertStr.indexOf(">", firstPos); token = convertStr.substring(firstPos, secondPos+1); //IF Token needs modification first, currently only "table" tags if(token.indexOf("<lt;table") > -1){ token = "<lt;table"; secondPos = firstPos + 5; }		else if(token.indexOf("<lt;a") > -1){ token = "<lt;a"; secondPos = firstPos + 1; }		else if(token.indexOf("<lt;img") > -1){ token = "<lt;img"; secondPos = firstPos + 3; }		else if(token.indexOf("<lt;div") > -1){ token = "<lt;div"; secondPos = firstPos + 3; }		else if(token.indexOf("<lt;!--") > -1){ token = "<lt;!--"; secondPos = firstPos + 3; }		else if(token != "<lt;pre>" && token.indexOf("<lt;p") > -1){ //Strip the Paragraph tagging here token = "<lt;p>"; //convertStr = convertStr.substring(0, firstPos) + "<lt;p>" + convertStr.substring(secondPos+1, convertStr.length+1); }		//IF Token is Known Conversion if(toWiki.start[token] != null && firstPos > -1)	{ //Advance Current Position to Trim Cycles currentPos = firstPos; //Replace Token in String convertStr = convertStr.substring(0,firstPos) + toWiki.start[token] + convertStr.substring(secondPos + 1, convertStr.length+1); //IF Special Processing Needed 'voodoo' if(toWiki.match[token] == "voodoo"){ //Identify Which Case we are Dealing with if(token == "<lt;math>"){

//<lt;pre>/<lt;math> tags is to not be parsed so find closing tag secondPos = convertStr.indexOf(toWiki.end[token], firstPos); //firstPos = convertStr.indexOf(toWiki.end[token],token); //Move Cursor to Closing Tag currentPos = secondPos; }				//Identify Which Case we are Dealing with if(token == "<lt;nowiki>"){

//<lt;pre>/<lt;math> tags is to not be parsed so find closing tag secondPos = convertStr.indexOf(toWiki.end[token], firstPos); //firstPos = convertStr.indexOf(toWiki.end[token],token); //Move Cursor to Closing Tag currentPos = secondPos; }								else if(token == "<lt;pre>"){ var tempAry = []; //Change the format to no pre tag secondPos = convertStr.indexOf(toWiki.end[token], firstPos); tempStr = convertStr.substring(firstPos, secondPos + 6);

//String Pre Tags tempStr = tempStr.replace(/\<lt;pre\>/,""); tempStr = tempStr.replace(/\<lt;\/pre\>/,""); tempStr = tempStr.replace(/\r/g, "\n"); tempStr = tempStr.replace(/ /g, " "); tempAry = tempStr.split('\n'); tempStr = ""; //FOR All Items in the list for(var eachLine = 0; eachLine <lt; tempAry.length; eachLine++){ if(tempAry[eachLine] != ""){ //Reinsert the items into the temporary string if(tempAry[eachLine].charAt(0) != " "){ tempStr = tempStr + " " + tempAry[eachLine] + "\n"; }							else{ tempStr = tempStr + tempAry[eachLine] + "\n"; }						}					}					//Reinsert the temp string back into the system convertStr = convertStr.substring(0,firstPos) + tempStr + convertStr.substring(secondPos + 7, convertStr.length+1); //Move Cursor to Closing Tag currentPos = firstPos + tempStr.length; }				else if(token == "<lt;table"){

//Identify Ending Tag secondPos = convertStr.indexOf(toWiki.end[token], firstPos); //WHILE There are Tables Within Tables refPos = convertStr.indexOf("<lt;table", firstPos + 5); while(refPos > firstPos && refPos <lt; secondPos){ //Take out second table and convert it						convertStr = convertStr.substring(0, refPos) + html2wikiTable(convertStr.substring(refPos, secondPos + 8), extensions) + convertStr.substring(secondPos+8,convertStr.length+1); //Advance Current Second Position Token secondPos = convertStr.indexOf(toWiki.end[token], firstPos); //Advance Reference Position refPos = convertStr.indexOf("<lt;table", firstPos + 5); }					//Convert Current Table + "\n" token = html2wikiTable(convertStr.substring(firstPos, secondPos + 8), extensions); convertStr = convertStr.substring(0, firstPos) + token + convertStr.substring(secondPos+8,convertStr.length+1);

}				else if(token == "<lt;a"){ //Process Links Here //Readjust Second Position secondPos = convertStr.indexOf(">", firstPos); //Fetch Entire Link Item refPos = convertStr.indexOf(toWiki.end[token], currentPos); //Get <lt;/a> linkTextStr = convertStr.substring(currentPos, refPos+4);

//Get the Text of the Link linkTextStr = convertStr.substring(secondPos + 1, refPos); //Stuff between >...<lt;/a>

//This is an appropiate link Converted Right firstPos = convertStr.indexOf("href=", currentPos); refPos = convertStr.indexOf('"', firstPos + 6);										linkStr = convertStr.substring(firstPos+6,refPos);					linkStr = linkStr.replace(/%20/g, " ");					//IF the Link str starts with http:...					if(linkStr.indexOf("http") == 0){						refPos = convertStr.indexOf(toWiki.end[token], currentPos);						linkStr = linkStr.replace(/ /g, "%20");						if(convertStr.substring(secondPos + 1, refPos) == linkStr){							//Process with single Brackets and space divider							convertStr = convertStr.substring(0, currentPos) 										 + "[" + linkStr + "]"							            + convertStr.substring(refPos+4,convertStr.length+1);														}						else{							//Process with single Brackets and space divider						convertStr = convertStr.substring(0, currentPos) 									 + "[" + linkStr + " " + convertStr.substring(secondPos + 1, refPos) + "]"						             + convertStr.substring(refPos+4,convertStr.length+1); }					}				else if(linkStr.toLowerCase.indexOf("mailto:") == 0){ //Must be a different link //Process with double brackets and | divider refPos = convertStr.indexOf(toWiki.end[token], currentPos); if(convertStr.substring(secondPos + 1, refPos) == linkStr){ //Process with single Brackets and space divider convertStr = convertStr.substring(0, currentPos) + "[" + linkStr + "]" + convertStr.substring(refPos+4,convertStr.length+1); }						else{ //Process with single Brackets and space divider convertStr = convertStr.substring(0, currentPos) + "[" + linkStr + " " + convertStr.substring(secondPos + 1, refPos) + "]" + convertStr.substring(refPos+4,convertStr.length+1); }					}					else { //Must be a different link //Process with double brackets and | divider refPos = convertStr.indexOf(toWiki.end[token], currentPos); if(convertStr.substring(secondPos + 1, refPos) == linkStr){ //Process with single Brackets and space divider convertStr = convertStr.substring(0, currentPos) + "" + linkStr + "" + convertStr.substring(refPos+4,convertStr.length+1); }						else{ //Process with single Brackets and space divider convertStr = convertStr.substring(0, currentPos) + "" + convertStr.substring(secondPos + 1, refPos) + "" + convertStr.substring(refPos+4,convertStr.length+1); }					}				}				else if(token == "<lt;dl>"){

indentStack = indentStack + ":"; }				else if(token == "<lt;ul>"){

indentStack = indentStack + "*"; }				else if(token == "<lt;ol>"){

indentStack = indentStack + "#"; }				else if(token == "<lt;/dl>" || token == "<lt;/ul>" || token == "<lt;/ol>"){ indentStack = indentStack.substring(0, indentStack.length-1); //Force a new line if we need it					if(convertStr.charAt(currentPos-1) != "\n" &&					  convertStr.charAt(currentPos-1) != "\n"){ convertStr = convertStr.substring(0,currentPos) + "\n" + convertStr.substring(currentPos, convertStr.length+1); }

}				else if(token == "<lt;dd>" || token == "<lt;li>"){

//Obtain new token location token = toWiki.end[token]; secondPos = convertStr.indexOf(token, firstPos); //while we have a nested Table refPos = convertStr.indexOf("<lt;table"); if(refPos >= firstPos && refPos <lt; secondPos){ while(convertStr.indexOf("<lt;/table>",firstPos) > secondPos						     && secondPos > -1){ secondPos = convertStr.indexOf(token, secondPos + 1); }					}					firstPos = secondPos; secondPos = firstPos + token.length; if(convertStr.charAt(currentPos - 1) == "\n"){ convertStr = convertStr.substring(0,currentPos) + indentStack.substring(1, indentStack.length) + convertStr.substring(currentPos,firstPos) + convertStr.substring(secondPos, convertStr.length+1); }					else if(convertStr.charAt(currentPos - 1) != "\n" &&					  convertStr.charAt(currentPos - 1) != ":" &&					   convertStr.charAt(currentPos - 1) != "*" &&					   convertStr.charAt(currentPos - 1) != "#" &&					   convertStr.charAt(currentPos - 1) != ";"){ //Put in the Colonsand the data after that upto the Firstposition convertStr = convertStr.substring(0,currentPos) + indentStack + convertStr.substring(currentPos,firstPos) + convertStr.substring(secondPos, convertStr.length+1);

}					else{

//Putin the Colons and the data after that upto the Firstposition //indentStack.substring(indentStack.length - 1, indentStack.length) convertStr = convertStr.substring(0,currentPos) + indentStack.substring(indentStack.length - 1, indentStack.length) + convertStr.substring(currentPos,firstPos) + convertStr.substring(secondPos, convertStr.length+1); }				}				else if(token == "<lt;dt>"){ //-1

indentStack = indentStack.substring(0, indentStack.length -1); indentStack = indentStack + ";"; //Obtain new token location token = toWiki.end[token]; firstPos = convertStr.indexOf(token, firstPos); secondPos = firstPos + token.length; if(convertStr.charAt(currentPos - 1) != "\n" &&					  convertStr.charAt(currentPos - 1) != ":" &&					   convertStr.charAt(currentPos - 1) != "*" &&					   convertStr.charAt(currentPos - 1) != "#" &&					   convertStr.charAt(currentPos - 1) != ";"){ //Put in the Colonsand the data after that upto the Firstposition convertStr = convertStr.substring(0,currentPos) + indentStack + convertStr.substring(currentPos,firstPos) + convertStr.substring(secondPos, convertStr.length+1); }					else{

//Put in the Colonsand the data after that upto the Firstposition //indentStack.substring(indentStack.length - 1, indentStack.length) convertStr = convertStr.substring(0,currentPos) + indentStack.substring(indentStack.length - 1, indentStack.length) + convertStr.substring(currentPos,firstPos) + convertStr.substring(secondPos, convertStr.length+1);

}									}				else if(token == "<lt;img"){ //Process the Image information here //Find Ending Section for the Image tag secondPos = convertStr.indexOf("/>", firstPos);

//IF its the magnify item // just remove the tag // set type as "thumb" //Get Alt if it exists //Get the Image Name //Build [[Image:Name.img					//if alt != ""					//tack on |alt					//IF Thumb Image					//IF Alignment Set					//Close out token ]] //Clear all Div Settings //Move Cursor currentPos = secondPos; }				else if(token == "<lt;div"){ //Process the Div information here secondPos = convertStr.indexOf("/>", firstPos); //IF the Div has a class //IF the Class has text "thumb t" none/left/right/center //IF the class has text "thumbinner" //IF the class is "magnify" //IF the class is "thumbcaption" //Move Cursor currentPos = secondPos; }				else if(token == "<lt;!--"){ //Current Pos is the beginning of <lt;!-- //Need to find the matching --> secondPos = convertStr.indexOf("-->", currentPos); //Pluck this string out tempStr = convertStr.substring(currentPos, secondPos + 4); tempStr = tempStr.toLowerCase; //Now we need to check if this string has key words in it					//FOR All Possible Keys for(var extPos = 0; extPos <lt; extList.length && !found; extPos++){ if(tempStr.indexOf("<lt;" + extList[extPos] + ">") > -1 && 						  tempStr.indexOf("<lt;/" + extList[extPos] + ">") > -1 ){ found = true; }					}					//IF Found Strip the Comment Tags if(found){ convertStr = convertStr.substring(0, currentPos) + convertStr.substring(currentPos + 4, secondPos) + convertStr.substring(secondPos + 3, convertStr.length); currentPos = currentPos + tempStr.length - 7; found = false; }					else{ //Move Currentposition to second pos currentPos = secondPos; }				}													else if(token == ""){ alert("Error: In a broken section search key\"voodoo 1\""); }			}			//ELSE IF Just a normal Parsing Token else if(toWiki.match[token] != ""){ //Obtain new token location token = toWiki.match[token]; firstPos = convertStr.indexOf(token, firstPos); secondPos = firstPos + token.length;

//Replace Match convertStr = convertStr.substring(0,firstPos) + toWiki.end[token] + convertStr.substring(secondPos, convertStr.length+1); }			//IF Neither IF is activated, then this tagging is meant to only occur once.

}		else if(currentPos > -1){ //Advance Position in the Conversion String currentPos = currentPos + 1; }

}//END WHILE More to Parse //Perform Cleanup on Random Items convertStr = convertStr.replace(/ \n/mgi, " \n"); convertStr = convertStr.replace(/\n /mgi, "\n"); convertStr = convertStr.replace(/ /mgi, " ");

//alert(source + "\n" + convertStr);

return convertStr; } /* * The Following Method is Designed to Convert Wikipedia Text to HTML * */

function convertHTML(source, extensions){

if(extensions == null){ extensions = fallBackExtensions;

}	var convertedText = source;  //Text to be converted var currentLine = "";        //Current Line of the Wiki text that needs to be changed var firstPos = 0;            //First Position for Substring use var secondPos = 0;           //Second Position for Substring use var refPos = 0;              //Reference Position if in Multilayers var found = false;           //Whether or not a Token has been found var tokenPos = 0;            //Counter for Which Token we are Checking for var currentPos = 0;          //Position in String Source var token = "";              //Token For current Working Item var counter = 0;			 //Counter of Random Items var colonCounter = 0;        //Coutner of Colons at Beginning of a Line var semiCounter = 0;         //Counter of Semi Colons var poundCounter = 0;        //Counter of Pound Signs var astCounter = 0;          //Coutner of Asteriks var inColon = false;		 //In a Colon Block var inSemi = false;          //In a Semi Colon Block var inPound = false;         //In a Pound Sign Block var inAst = false;           //In an Asterick Block var colonStack = [];		 //Track of Previous Line Items var useToken = false;        //Whether to Use a Token Addendum var poundStack = []; var astStack = []; var semiStack = []; var convStack = [];          //Stack of Which tokens used last var inTable = 0;         //Whetehr we are parsing a table... var inPre = false;       //Whether we are in a being built pre block var tempStr = ""; //Object for Identifying Key Characters var toHtml = {}; //Characters to Check For toHtml.key = {}; toHtml.key[0] = "\n";   //this is here as a fall back for refactoring toHtml.key[1] = "'"; toHtml.key[2] = "<lt;"; toHtml.key[3] = "{"; toHtml.key[4] = ":"; toHtml.key[5] = ";"; toHtml.key[6] = "#"; toHtml.key[7] = "*"; toHtml.key[8] = "["; toHtml.key[9] = "="; toHtml.key[10] = "-"; var keylength = 11;	 //This always need to be set to highest value in toHtml.key size plus ONE

//Starting for Character Change Overs universally toHtml.start = {}; toHtml.start["'"] = "<lt;b><lt;i>"; toHtml.start["'''"] = "<lt;b>"; toHtml.start["''"] = "<lt;i>"; toHtml.start["{|"] = "{|"; toHtml.start["="] = "<lt;h1>"; toHtml.start["=="] = "<lt;h2>"; toHtml.start["==="] = "<lt;h3>"; toHtml.start["===="] = "<lt;h4>"; toHtml.start["====="] = "<lt;h5>"; toHtml.start["======"] = "<lt;h6>"; toHtml.start[":"] = "<lt;dl><lt;dd>"; toHtml.start["*"] = "<lt;ul><lt;li>"; toHtml.start["#"] = "<lt;ol><lt;li>"; toHtml.start[";"] = "<lt;dl><lt;dt>"; //Matchign Section toHtml.match = {}; toHtml.match[""] = ""; toHtml.match[""] = ""; toHtml.match[""] = ""; toHtml.match["{|"] = "|}"; toHtml.match["="] = "="; toHtml.match["=="] = "=="; toHtml.match["==="] = "==="; toHtml.match["===="] = "===="; toHtml.match["====="] = "====="; toHtml.match["======"] = "======"; toHtml.match[":"] = "<lt;/dd><lt;dd>"; toHtml.match["*"] = "<lt;/li><lt;li>"; toHtml.match["#"] = "<lt;/li><lt;li>"; toHtml.match[";"] = "<lt;/dt><lt;dt>"; toHtml.match[":;"] = "<lt;/dd><lt;dt>"; toHtml.match[";:"] = "<lt;/dt><lt;dd>";

//End Mathign Token toHtml.end = {}; toHtml.end["'"] = "<lt;b><lt;i>"; toHtml.end["'''"] = "<lt;/b>"; toHtml.end["''"] = "<lt;/i>"; toHtml.end["|}"] = "|}"; toHtml.end["="] = "<lt;/h1>"; toHtml.end["=="] = "<lt;/h2>"; toHtml.end["==="] = "<lt;/h3>"; toHtml.end["===="] = "<lt;/h4>"; toHtml.end["====="] = "<lt;/h5>"; toHtml.end["======"] = "<lt;/h6>"; toHtml.end[":"] = "<lt;/dd><lt;/dl>"; toHtml.end["*"] = "<lt;/li><lt;/ul>"; toHtml.end["#"] = "<lt;/li><lt;/ol>"; toHtml.end[";"] = "<lt;/dt><lt;/dl>";

/*************Main Body*****************/ //Prepare any extensions to be lower cased for easy checking extensions = extensions.toLowerCase;

//WHILE Current Position is Previous while(currentPos <lt; convertedText.length) {

//FOR Every Token Key for(tokenPos = 0; tokenPos <lt; keylength && !found; tokenPos++){

//IF Current Character is a Token if(convertedText.charAt(currentPos) == toHtml.key[tokenPos]){ found = true; switch(tokenPos){ case 0: //Case \n //IF we have a blank space before the new line if(convertedText.charAt(currentPos + 1) == " " && !inPre){ inPre = true; //Creat the pre tag for this section convertedText = convertedText.substring(0,currentPos) + "<lt;pre>" + convertedText.substring(currentPos, convertedText.length + 1); currentPos = currentPos + 4; }					else if(convertedText.charAt(currentPos + 1) != " " && inPre){

inPre = false; //Creat the pre tag for this section convertedText = convertedText.substring(0,currentPos) + "<lt;/pre>" + convertedText.substring(currentPos, convertedText.length + 1); currentPos = currentPos + 5; }						//IF We have 2 New Lines in a Row if(convertedText.charAt(currentPos - 1) == "\n" && inTable <lt;= 0){ //Experiment if(currentPos + 2 <lt; convertedText.length){ convertedText = convertedText.substring(0, currentPos) + "<lt;p><lt;/p>" + convertedText.substring(currentPos + 1, convertedText.length+1); }						else { convertedText = convertedText.substring(0, currentPos) + "<lt;p><lt;/p>" + convertedText.substring(convertedText.length, convertedText.length+1); }						currentPos = currentPos + 6; }//END IF We have 2 New LInes in a Row

//IF We are in any of the Special Cases if((convStack.length > 0) ||					   (convertedText.charAt(currentPos + 1) == ":" || convertedText.charAt(currentPos + 1) == "*" || convertedText.charAt(currentPos + 1) == "#" || convertedText.charAt(currentPos + 1) == ";")){ if(inPre){ currentPos = currentPos - 5; }						//Grab all Special Characters for next few items var tempPos = currentPos + 1; var tempChar = ""; var tempStack = []; var insertStr = ""; var isDiff = false; tempChar = convertedText.charAt(tempPos); while((tempChar == ":" || tempChar == "*" || tempChar == "#" || tempChar == ";") && tempPos <lt; convertedText.length){ tempStack.push(tempChar); tempPos++; tempChar = convertedText.charAt(tempPos); }

//Check the Current Stack of Items and find divergence point var divergencePoint = 0; var divPos = 0; while(tempStack[divPos] == convStack[divPos] && divPos <lt; tempStack.length && divPos <lt; convStack.length){ divPos++; }						divergencePoint = divPos; //IF There isn't a Divergence if(divergencePoint == tempStack.length && divergencePoint == convStack.length){ //Build a Quick Insert Str insertStr = toHtml.match[convStack[convStack.length-1]]

} 						else{ //Close out Old Stack Items and remove them from the stack //FOR All items that need to be removed in reverse order if(convStack.length > 0){ for(divPos = convStack.length - 1; (divPos > divergencePoint - 1 && divPos > -1); divPos = divPos - 1){ //Add the Closeout tag to the Insert Str insertStr = insertStr + toHtml.end[convStack[divPos]]; convStack.pop; }//END FOR

//IF there is a need to remove one last item if(convStack.length == 1 && tempStack.length == 0){ convStack.pop; }								//Insert the Change Over item if(convStack.length > 0 && convStack.length <lt;= tempStack.length 								  && divergencePoint >= convStack.length){ tempStr = convStack[divergencePoint] + tempStack[divergencePoint]; if(toHtml.match[tempStr] == null){ insertStr = insertStr + toHtml.match[convStack[convStack.length-1]]; }									else{ insertStr = insertStr + toHtml.match[convStack[tempStr]]; }								}							}							//Build up the stack with the new items and insert the new items text if(tempStack.length > 0 && tempStack.length != convStack.length){ for(divPos = convStack.length; divPos <lt; tempStack.length; divPos ++){ //Add the Starting Tags insertStr = insertStr + toHtml.start[tempStack[divPos]]; convStack.push(tempStack[divPos]); }//END FOR } 						}//END IF No Divergence

//Insert the String convertedText = convertedText.substring(0, currentPos+1) + insertStr + convertedText.substring(tempPos,convertedText.length+1); //currentPos = currentPos + insertStr.length; if(inPre){ currentPos = convertedText.indexOf("\n", currentPos); }					}//END IF Special Cases break; case 1: //Case '

//Identify if this is one of the key cases //Grab Token of first 5 characters token = convertedText.substring(currentPos, currentPos + 5); //IF Token is 5 Apostraphes if(token.indexOf("'") > -1){ //Leave Token Alone! }					//ELSE IF Token is 3 apostraphes else if(token.indexOf("'''") > -1){ //Convert Token to 3 Apostraphes only token = "'''"; }					//ELSE IF Token is 2 Apostraphes else if(token.indexOf("''") > -1){ //Convert Token to 2 Apostraphes Only token = "''"; }

//IF token exists properly if(toHtml.start[token] != null){ //Get Position of Ending Part secondPos = convertedText.indexOf(toHtml.match[token],currentPos + 1);

//IF Matching 2nd Position Exists if(secondPos > -1){ //Replace First Part convertedText = convertedText.substring(0,currentPos) + toHtml.start[token] + convertedText.substring((currentPos + token.length),convertedText.length+1); //Get Position of Ending Part secondPos = convertedText.indexOf(toHtml.match[token],currentPos + 1); //Replace Second Part (ending token) token = toHtml.match[token]; convertedText = convertedText.substring(0,secondPos) + toHtml.end[token] + convertedText.substring((secondPos + token.length),convertedText.length+1); }//END IF 2nd Token Exists }//END IF Token Exists Properly break; case 2: //Case <lt; for possible <lt;pre>,<lt;math>, or <lt;nowiki> blocks //Identify Possible > after current loc firstPos = convertedText.indexOf(">",currentPos); //IF > exists in String if(firstPos > -1){ //Grab the Token of <lt;?> token = convertedText.substring(currentPos,firstPos+1); token = token.toLowerCase;

//IF Token is <lt;pre> tag if(token == "<lt;pre>"){ //Find Matching <lt;/pre> tag secondPos = convertedText.indexOf("<lt;/pre>",currentPos); //IF Matching Tag Exists if(secondPos > -1){ //Grab a Temporary String, Move it to the tempStr = convertedText.substring(currentPos + 5, secondPos); tempStr = tempStr.replace(/<lt;/g,"&lt;"); tempStr = tempStr.replace(/>/g,"&gt;"); convertedText = convertedText.substring(0, currentPos) + "<lt;pre>" + tempStr + convertedText.substring(secondPos, convertedText.length + 1); secondPos = convertedText.indexOf("<lt;/pre>",currentPos);

//Advance Current Position to Second Position in string currentPos = convertedText.indexOf("<lt;/pre>",currentPos); }						}						//ELSE IF Token is <lt;math> tag else if(token == "<lt;math>"){ //Find Matching <lt;/math> tag secondPos = convertedText.indexOf("<lt;/math>",currentPos); //IF Matching Tag Exists if(secondPos > -1){

//Advance Current Position to Second Position in string currentPos = secondPos; }						}						//ELSE IF Token is <lt;nowiki> tag else if(token == "<lt;nowiki>"){ //Find Matching <lt;/nowiki> tag secondPos = convertedText.indexOf("<lt;/nowiki>",currentPos); //IF Matching Tag Exists if(secondPos > -1){ tempStr = convertedText.substring(currentPos + 8, secondPos); tempStr = tempStr.replace(/<lt;/g,"&lt;"); tempStr = tempStr.replace(/>/g,"&gt;"); convertedText = convertedText.substring(0, currentPos) + "<lt;nowiki>" + tempStr + convertedText.substring(secondPos, convertedText.length + 1); secondPos = convertedText.indexOf("<lt;/nowiki>",currentPos); //Advance Current Position to Second Position in string currentPos = secondPos; }													}						//ELSE IF This Token Starts with <lt;table else if(token.indexOf("<lt;table") == 0){ if(inTable <lt; 0){ inTable = 0; }							inTable = inTable + 1; currentLine = convertedText.substring(currentPos, firstPos+1); if(currentLine.indexOf("\n") == -1){ currentPos = convertedText.indexOf(">", currentPos); }													}						else if(token == "<lt;/table>"){ inTable = inTable - 1; }						else if(extensions.indexOf(token.substring(1,token.length-1)) > -1 &&						       (extensions.indexOf(token.substring(1,token.length-1))) == 0 ||						        (extensions.charAt(extensions.indexOf(token.substring(1,token.length-1))) == "," && (extensions.charAt(extensions.indexOf(token.substring(1,token.length-1)) + token.length - 2) == ","						        || extensions.indexOf(token.substring(1,token.length-1)) + token.length - 1 == extensions.length))						        ){ //Find Matching Tag var endTag = 0; var endString = "<lt;/"; endString = endString + token.substring(1,token.length); endTag = convertedText.toLowerCase.indexOf(endString, currentPos); //IF There is a matching tag if(endTag > -1){

//Surround Entire Bit by Comments convertedText = convertedText.substring(0,currentPos) + "<lt;!--" + convertedText.substring(currentPos,(endTag + endString.length)).replace(/\n/g,"<lt;br />") + "-->"								               + convertedText.substring((endTag + endString.length),convertedText.length + 1); //Advance Cursor to be after converted Section currentPos = endTag + token.length + 8; }							else{ // Must not be a Matching Tag //Just Advance Cursor to Ending Item currentLine = convertedText.substring(currentPos, firstPos+1); if(currentLine.indexOf("\n") == -1){ currentPos = convertedText.indexOf(">", currentPos); }															}//END IF There is a Matching Tag }						else{ //Just advance cursor to Ending Item currentLine = convertedText.substring(currentPos, firstPos+1); if(currentLine.indexOf("\n") == -1){ currentPos = convertedText.indexOf(">", currentPos); }						}					}//END IF > exists in String break; case 3: //Case Current Char is { Possible Table Block //IF This is a Table Starter if(convertedText.indexOf("{|",currentPos) == currentPos){ //Convert The Token key to appropiate tag token = "{|"; token = toHtml.match[token]; //Identify Ending Tag secondPos = convertedText.indexOf(toHtml.end[token], currentPos); //WHILE There are Tables Within Tables refPos = convertedText.indexOf("{|", currentPos + 2); while(refPos > currentPos && refPos <lt; secondPos && refPos != -1){

token = ""; token = convertedText.substring(refPos, secondPos + 2); token = wiki2htmlTable(token, extensions);

//Take out second table and convert it							convertedText = convertedText.substring(0, refPos) + token + convertedText.substring(secondPos+2,convertedText.length+1);

//Advance Current Second Position Token token = ""; token = "|}"; secondPos = convertedText.indexOf(toHtml.end[token], currentPos); //Advance Reference Position refPos = convertedText.indexOf("{|", currentPos + 2);

}					   //IF There is no Closing to the Current table if(secondPos == -1){ //Magically create an end to this table secondPos = convertedText.length - 2; }						//Convert Current Table token = ""; token = convertedText.substring(currentPos, secondPos + 2 ); token = wiki2htmlTable(token, extensions); convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+2,convertedText.length+1); //Move Cursor to after Table currentPos = currentPos + token.length-1; }					break; case 4: //Case : indent block

//Check if this is flush with the Left margin if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n" ){ //Reset the Token token = ""; //IF Colon Counter hasn't been set yet if(!inColon){ //Set Colon Counter to 1 (So far) colonCounter = 1; //Build Start of Token token = "<lt;dl><lt;dd>"; //Setup the First Item to being created and true colonStack[1] = true; //Push Being in indent block convStack.push(":"); }						//Set Reference Position to next in String refPos = currentPos + 1; //WHILE Next Character is a ":" counter = 1; while(convertedText.charAt(refPos) == ":"){ //Increment Counter and Move Cursor counter = counter + 1; refPos = refPos + 1; convStack.push(":"); }						//IF in a colon block and We are previously used if(inColon && colonStack[counter]){ useToken = true; }						//WHILE Current Count of Every ":" while(colonCounter <lt; counter){ //add <lt;dl><lt;dd> token = "<lt;dl><lt;dd>" + token; //Set Current Flag of Used to True colonStack[colonCounter] = true; //Increment Amount of Colons Counted colonCounter++; }						while(counter <lt; colonCounter){ //Add <lt;dl><lt;dd> token = token + "<lt;/dd><lt;/dl>"; //Set Current Flag of Used to False colonStack[colonCounter] = false; //Decrement Colons counted colonCounter--;

}

//IF We need to Alter the Token if(useToken){ //Change the Token Appropiately token = token + "<lt;/dd><lt;dd>"; //Flip this Flag Back to False useToken = false; }

//Replace Colons on this Line with Token Generated convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(refPos,convertedText.length+1); //Move Cursor to After Changed Token currentPos = currentPos + token.length - 1; //Set to being In a Colon Set inColon = true; }					break; case 5: //Case ; Situation

//Check if this is flush with the Left margin if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n"){ //Reset the Token token = ""; //IF Pound Hasn't been Started if(!inSemi){ //Set Semi Counter to 1 (So far) semiCounter = 1; //Build Start of Token token = "<lt;dl><lt;dt>"; //Setup the First Item to being created and true semiStack[1] = true;

//Push Being in pound block convStack.push(";"); }						//Set Reference Position to next in String refPos = currentPos + 1; //WHILE Next Character is a ";" counter = 1; while(convertedText.charAt(refPos) == ";"){ //Increment Counter and Move Cursor counter = counter + 1; refPos = refPos + 1; convStack.push(";"); }						//IF in a pound block and We are previously used if(inSemi && semiStack[counter]){ useToken = true; }						//WHILE Current Count of Every "#" while(semiCounter <lt; counter){ //add <lt;dl><lt;dt> token = "<lt;dl><lt;dt>" + token; //Set Current Flag of Used to True semiStack[semiCounter] = true; //Increment Amount of Pounds Counted semiCounter++; }						while(counter <lt; semiCounter){ //Add token = token + "<lt;/dt><lt;/dl>"; //Set Current Flag of Used to False semiStack[semiCounter] = false; //Decrement Pounds counted semiCounter--;

}

//IF We need to Alter the Token if(useToken){ //Change the Token Appropiately token = token + "<lt;/dt><lt;dt>"; //Flip this Flag Back to False useToken = false; }

//Replace Colons on this Line with Token Generated convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(refPos,convertedText.length+1); //Move Cursor to After Changed Token currentPos = currentPos + token.length - 1; //Set to being In a Pound Set inPound = true; }										break; case 6: //Case # Numbered Indents

//Check if this is flush with the Left margin if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n"){ //Reset the Token token = ""; //IF Pound Hasn't been Started if(!inPound){ //Set Pound Counter to 1 (So far) poundCounter = 1; //Build Start of Token token = "<lt;ol><lt;li>"; //Setup the First Item to being created and true poundStack[1] = true;

//Push Being in pound block convStack.push("#"); }						//Set Reference Position to next in String refPos = currentPos + 1; //WHILE Next Character is a "#" counter = 1; while(convertedText.charAt(refPos) == "#"){ //Increment Counter and Move Cursor counter = counter + 1; refPos = refPos + 1; convStack.push("#"); }						//IF in a pound block and We are previously used if(inPound && poundStack[counter]){ useToken = true; }						//WHILE Current Count of Every "#" while(poundCounter <lt; counter){ //add <lt;ul><lt;li> token = "<lt;ol><lt;li>" + token; //Set Current Flag of Used to True poundStack[poundCounter] = true; //Increment Amount of Pounds Counted poundCounter++; }						while(counter <lt; poundCounter){ //Add token = token + "<lt;/li><lt;/ol>"; //Set Current Flag of Used to False poundStack[poundCounter] = false; //Decrement Pounds counted poundCounter--;

}

//IF We need to Alter the Token if(useToken){ //Change the Token Appropiately token = token + "<lt;/li><lt;li>"; //Flip this Flag Back to False useToken = false; }

//Replace Colons on this Line with Token Generated convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(refPos,convertedText.length+1); //Move Cursor to After Changed Token currentPos = currentPos + token.length - 1; //Set to being In a Pound Set inPound = true; }					break; case 7: //Case * Bulletted Indents

//Check if this is flush with the Left margin if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n"){ //Reset the Token token = ""; //IF ast Hasn't been Started if(!inAst){ //Set ast Counter to 1 (So far) astCounter = 1; //Build Start of Token token = "<lt;ul><lt;li>"; //Setup the First Item to being created and true astStack[1] = true;

//Push Being in indent block convStack.push("*"); }						//Set Reference Position to next in String refPos = currentPos + 1; //WHILE Next Character is a "*" counter = 1; while(convertedText.charAt(refPos) == "*"){ //Increment Counter and Move Cursor counter = counter + 1; refPos = refPos + 1; convStack.push("*"); }						//IF in a ast block and We are previously used if(inAst && astStack[counter]){ useToken = true; }						//WHILE Current Count of Every "*" while(astCounter <lt; counter){ //add <lt;ul><lt;li> token = "<lt;ul><lt;li>" + token; //Set Current Flag of Used to True astStack[astCounter] = true; //Increment Amount of asts Counted astCounter++; }						while(counter <lt; astCounter){ //Add token = token + "<lt;/li><lt;/ul>"; //Set Current Flag of Used to False astStack[astCounter] = false; //Decrement asts counted astCounter--;

}

//IF We need to Alter the Token if(useToken){ //Change the Token Appropiately token = token + "<lt;/li><lt;li>"; //Flip this Flag Back to False useToken = false; }

//Replace Colons on this Line with Token Generated convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(refPos,convertedText.length+1); //Move Cursor to After Changed Token currentPos = currentPos + token.length - 1; //Set to being In a ast Set inAst = true; }					break; case 8: //Case [ References

//Several Cases to Consider TODO //IF This isn't last character in string if(currentPos != convertedText.length-1){ //IF This has a [ to the right if(convertedText.charAt(currentPos+1) == "["){ //IF This has a matching ]] Tag secondPos = convertedText.indexOf("]]",currentPos); if(secondPos > -1){ //See if the Link has a link within it (crazy people out there) refPos = convertedText.indexOf("", currentPos + 2);								while(refPos <lt; secondPos && refPos > -1){									secondPos = convertedText.indexOf("",secondPos + 2); refPos = convertedText.indexOf("[[", refPos + 2);								}								//Grab the Token between these two Items								token = convertedText.substring(currentPos, secondPos + 2);

//IF Token doesn't have a new line if(token.indexOf("\n") == -1){

//SWITCH On 3rd Character in String switch(token.toUpperCase.charAt(2)){ case "I": //Check if this is Image Link if(token.toUpperCase.indexOf("IMAGE:") == 2){ /*												//Trim Image token = token.substring(2, token.length-2); //Split current token on "|" var tokenArray = token.split("|");

//Variables needed for Image Processing var alt = ""; var src = ""; var longdesc = ""; var imgClass = ""; var align = "none"; var width = ""; var height = ""; var imgCap = ""; var tempToken = ""; var divCounter = 1; var useEnlarge = false; var useFrame = false;

//Build up Basic Elements of Image link longdesc = tokenArray[0]; src = tokenArray[0].split(":")[1]; //WHILE There are more Tokens in the Array counter = 1; while(tokenArray[counter] != null){ //Identify Token Type var currToken = tokenArray[counter].toUpperCase; if(currToken == "THUMB" || currToken == "THUMBNAIL"){ imgClass = "thumbimage"; useEnlarge = true; useFrame = true; }													else if(currToken == "FRAME"){ imgClass = "thumbimage"; useEnlarge = false; useFrame = true; }													else if(currToken == "LEFT" || currToken == "RIGHT" || currToken == "CENTER" || currToken == "NONE"){ align = tokenArray[counter].toLowerCase; }													else if(currToken.indexOf("PX") == currToken.length - 2){ //Parse for Number of pixels var pxPattern = /\d+X\d+PX/; var singPxPattern = /\d+PX/; if(pxPattern.test(currToken)){ firstPos = currToken.indexOf("X"); width = currToken.substring(0,firstPos); height = currToken.substring(firstPos + 1, currToken.length - 2); }														else if(singPxPattern.test(currToken)){ width = tokenArray[counter].substring(tokenArray[counter].length - 2); }														else{ //Must not be a Pixel Pattern Setup //alt = convertHTML(tokenArray[counter]); //alt = alt.replace(/"/g, "'");															alt = tokenArray[counter];														}													}													else { //must be a caption element check for links in it, extend if needed														//alt = convertHTML(tokenArray[counter]);														//alt = alt.replace(/\"/g, "'"); alt = tokenArray[counter]; }//END Identify Type //Increment Counter counter++; }												//Build the Token for Use //First Put Alignment Div into play token = ""; token = "<lt;div class=\"thumb t"+ align + "\">"; //Then if there is a frame place that if(useFrame){ token = token + "<lt;div class=\"thumbinner\">"; divCounter++; }												//Insert Image Proper token = token + "<lt;img alt=\"" + alt + "\" " + "longdesc=\"" + longdesc + "\" "; if(useFrame){ token = token + "class=\"thumbimage\" "; }												token = token + "src=\"" + src + "\" "; if(width != ""){ token = token + "width=\"" + width + "\" "; }												if (height != ""){ token = token + "height=\"" + height + "\" "; }												token = token + "/>"; if(useFrame){ token = token + "<lt;div class=\"thumbcaption\">"; divCounter++; }												//IF Have to have Enlarge option if(useEnlarge){ token = token + "<lt;div class=\"magnify\" style=\"float:right\"><lt;img src=\"/mediawiki/skins/common/images/magnify-clip.png\" width=\"15\" height=\"11\" alt=\"\" /><lt;/div>"; }												//Close out any remaning Divs while(divCounter > 0){ token = token + "<lt;/div>"; divCounter--; }												//Replace Token back into string convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+2,convertedText.length+1);

//Move Cursor up currentPos = currentPos + token.length - 1;

//Skip the Image Processing for now: currentPos = currentPos + 2; }

else { //Must be a regular link //Process basic Link firstPos = token.indexOf("|"); if(firstPos > -1){ token = token.replace(/\[\[(.*?)\|(.*?)\]\]/,"<lt;a href=\"$1\">$2<lt;/a>"); }												else { token = token.replace(/\[\[(.*?)\]\]/,"<lt;a href=\"$1\">$1<lt;/a>"); }												//Replace Token back into string convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+2,convertedText.length+1); //Advance Current Position currentPos = currentPos + token.indexOf(">"); }

break;

case ":": //Link Processing here, don't show the :, but keep it											firstPos = token.indexOf("|"); if(firstPos > -1){ token = token.replace(/\[\[\:(.*?)\|(.*?)\]\]/,"<lt;a href=\":$1\">$2<lt;/a>"); }											else { token = token.replace(/\[\[\:(.*?)\]\]/,"<lt;a href=\":$1\">$1<lt;/a>"); }											//Replace Token back into string convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+2,convertedText.length+1); //Advance Current Position currentPos = currentPos + token.indexOf(">"); break;

default: //Process basic Link firstPos = token.indexOf("|"); if(firstPos > -1){ token = token.replace(/\[\[(.*?)\|(.*?)\]\]/,"<lt;a href=\"$1\">$2<lt;/a>"); }											else { token = token.replace(/\[\[(.*?)\]\]/,"<lt;a href=\"$1\">$1<lt;/a>"); }											//Replace Token back into string convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+2,convertedText.length+1); //Advance Current Position currentPos = currentPos + token.indexOf(">"); break;

}//END SWITCH

}//END IF This Doesn't have a \n character in it							}//END IF This has a matching ]] tag }						else{ //Must not have a [ to the right of current position //IF This has a matching ] secondPos = convertedText.indexOf("]",currentPos); if(secondPos > -1){ token = convertedText.substring(currentPos, secondPos + 1); //Check for a New Line in token if(token.indexOf("\n") == -1){ //Check for blank space in token if(token.indexOf(" ") > -1){ //Case 1: Wording token = token.replace(/\[(.*?)\s(.*?)\]/, "<lt;a href=\"$1\">$2<lt;/a>"); }									else {//Case 2: token = token.replace(/\[(.*?)\]/, "<lt;a href=\"$1\">$1<lt;/a>"); }									//Insert Token back into string convertedText = convertedText.substring(0, currentPos) + token + convertedText.substring(secondPos+1,convertedText.length+1); currentPos = currentPos + token.indexOf(">"); }							}

}//END IF There is only one [

}//END IF This is Last Character in String

break; case 9: //Case = headers if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n" || convertedText.charAt(currentPos - 1) == ">" ){ //Set our Reference Position to Next Position in string refPos = currentPos + 1; //Set count of = settings to 1 counter = 1; //WHILE Current Character is = while(convertedText.charAt(refPos) == "="){ //Increment Counter and Move Cursor counter = counter + 1; refPos = refPos + 1; }											//Set Token to number of = that we have token = ""; for(tokenPos = 0; tokenPos <lt; counter; tokenPos++){ token = token + "="; }						//See if we can find an ending token secondPos = convertedText.indexOf(toHtml.match[token], currentPos + token.length); //IF there is a matching pair of this item if(secondPos > -1){ //Replace First Part convertedText = convertedText.substring(0,currentPos) + toHtml.start[token] + convertedText.substring((currentPos + token.length),convertedText.length+1); //Get Position of Ending Part secondPos = convertedText.indexOf(toHtml.match[token],currentPos + 1); //Replace Second Part (ending token) token = toHtml.match[token]; convertedText = convertedText.substring(0,secondPos) + toHtml.end[token] + convertedText.substring((secondPos + token.length),convertedText.length+1); }					}					break; case 10: //Case - Hyphen posible Horizontal Line //Quick Test is to check if last position was a newline if(currentPos == 0 || convertedText.charAt(currentPos - 1) == "\n"){ //Identify This Current Line of text secondPos = convertedText.indexOf("\n", currentPos); currentLine = convertedText.substring(currentPos, secondPos); //IF this is meant to be a horizontal line if(currentLine == ""){ //Change Hyphens to Proper Tag in Converted Text convertedText = convertedText.substring(0,currentPos) + "<lt;hr />" + convertedText.substring((secondPos),convertedText.length+1); }					}					break; default: alert("Unhandled Token Case"); }			}//END SWITCH On Token }//END FOR Every Token Key //Set flag to not Found found = false; //Increment Position in String currentPos = currentPos + 1; }//WHILE There is More to Convert

//While We have to close out a stack item while(convStack.length > 0){ convertedText = convertedText + toHtml.end[convStack[convStack.length -1]]; convStack.pop; }

//Do a force Convert of New Lines Manually convertedText = convertedText.replace(/<lt;!--\n-->/mgi, "\n"); convertedText = convertedText.replace(/<lt;!--<lt;p><lt;\/p>-->/mgi, "<lt;p><lt;/p>"); convertedText = convertedText.replace(/\n/mgi, "<lt;!--\n-->"); return convertedText; }

function unitTestSuite{ var testString = ""; var answerString = "";

/*** Build Table Starter ***/ document.write("<lt;table border=\"1\"><lt;tr><lt;th>Test Name<lt;/th><lt;th>Success Status<lt;/th><lt;th>Extra Information<lt;/th><lt;/tr>");

/***********Testing HTML 2 Wiki Conversion**********/ /*		//Testing Bold testString = "<lt;b>bold<lt;/b>"; answerString = "bold"; testString = convertWiki(testString); tableWrite("bold: ", (testString == answerString), " "); //Testing Italic testString = "<lt;i>test<lt;/i>"; answerString = "test"; testString = convertWiki(testString); tableWrite("italic: ",(testString == answerString), " "); //Testing Strong testString = "<lt;strong>test<lt;/strong>"; answerString = "test"; testString = convertWiki(testString); tableWrite("strong: ",(testString == answerString)," "); //Testing Emphasized testString = "<lt;em>test<lt;/em>"; answerString = "test"; testString = convertWiki(testString); tableWrite("em: ",(testString == answerString)," "); //Testing Headers Section testString = "<lt;h1>test<lt;/h1>"; answerString = "=test="; testString = convertWiki(testString); tableWrite("header 1: ",(testString == answerString)," ");

//Testing Headers Section testString = "<lt;h2>test<lt;/h2>"; answerString = "==test=="; testString = convertWiki(testString); tableWrite("header 2: ",(testString == answerString)," "); //Testing Headers Section testString = "<lt;h3>test<lt;/h3>"; answerString = "===test==="; testString = convertWiki(testString); tableWrite("header 3: ", (testString == answerString)," ");

//Testing Headers Section testString = "<lt;h4>test<lt;/h4>"; answerString = "====test===="; testString = convertWiki(testString); tableWrite("header 4: ", (testString == answerString)," ");

//Testing Headers Section testString = "<lt;h5>test<lt;/h5>"; answerString = "=====test====="; testString = convertWiki(testString); tableWrite("header 5: ", (testString == answerString)," ");

//Testing Headers Section testString = "<lt;h6>test<lt;/h6>"; answerString = "======test======"; testString = convertWiki(testString); tableWrite("header 6: ", (testString == answerString)," "); //Testing Paragraphs testString = "<lt;p>test<lt;/p>"; answerString = "\ntest\n"; testString = convertWiki(testString); tableWrite("paragraph: ", (testString == answerString)," "); //Testing Breaks testString = "<lt;br />test<lt;br />"; answerString = "\ntest\n"; testString = convertWiki(testString); tableWrite("breaks: ", (testString == answerString)," "); //Test Pre Tags testString = "<lt;pre><lt;b>test<lt;/b><lt;/pre><lt;b>test 2<lt;/b>"; answerString = "<lt;pre><lt;b>test<lt;/b><lt;/pre>test 2"; testString = convertWiki(testString); tableWrite("Pre Tags: ", (testString == answerString)," "); //Testing Simple Table testString = "<lt;table><lt;tr><lt;td>a<lt;/td><lt;td>b<lt;/td><lt;/tr><lt;/table>"; answerString = "{|\n|-\n|a\n|b\n|}\n"; testString = convertWiki(testString); tableWrite("Simple Table: ", (testString == answerString), (testString + "\n" + answerString));

//Testing Table with Caption testString = "<lt;table><lt;caption>fucksticks<lt;/caption><lt;tr><lt;td>a<lt;/td><lt;td>b<lt;/td><lt;/tr><lt;/table>"; answerString = "{|\n|+fucksticks\n|-\n|a\n|b\n|}\n"; testString = convertWiki(testString); tableWrite("Table with Caption: ", (testString == answerString), testString);

/***********Testing Wiki 2 HTML Conversion**********/ /*	//Testing Hyphens testString = "\na\n\n"; answerString = "<lt;hr />\na\n<lt;hr />\n"; testString = convertHTML(testString); tableWrite("hyphens: ", (testString == answerString)," ");

//Testing Bold testString = "bold"; answerString = "<lt;b>bold<lt;/b>"; testString = convertHTML(testString); tableWrite("bold: ", (testString == answerString)," "); //Testing Italic testString = "test"; answerString = "<lt;i>test<lt;/i>"; testString = convertHTML(testString); tableWrite("italic: ", (testString == answerString)," "); //Testing Headers Section testString = "=test="; answerString = "<lt;h1>test<lt;/h1>"; testString = convertHTML(testString); tableWrite("header 1: ", (testString == answerString)," ");

//Testing Headers Section testString = "==test=="; answerString = "<lt;h2>test<lt;/h2>"; testString = convertHTML(testString); tableWrite("header 2: ", (testString == answerString)," "); //Testing Headers Section testString = "===test==="; answerString = "<lt;h3>test<lt;/h3>"; testString = convertHTML(testString); tableWrite("header 3: ", (testString == answerString)," ");

//Testing Headers Section testString = "====test===="; answerString = "<lt;h4>test<lt;/h4>"; testString = convertHTML(testString); tableWrite("header 4: ", (testString == answerString)," ");

//Testing Headers Section testString = "=====test====="; answerString = "<lt;h5>test<lt;/h5>"; testString = convertHTML(testString); tableWrite("header 5: ", (testString == answerString)," ");

//Testing Headers Section testString = "======test======"; answerString = "<lt;h6>test<lt;/h6>"; testString = convertHTML(testString); tableWrite("header 6: ", (testString == answerString)," "); //Testing Paragraphs testString = "\ntest\n\n"; answerString = "<lt;p>test<lt;/p>"; testString = convertHTML(testString); tableWrite("paragraph: ", (testString == answerString)," "); //Testing Breaks testString = "\ntest\n"; answerString = "<lt;br />test<lt;br />"; testString = convertHTML(testString); tableWrite("breaks: ", (testString == answerString)," "); //Test Pre Tags testString = "<lt;pre>test<lt;/pre>test 2"; answerString = "<lt;pre>test<lt;/pre><lt;b>test 2<lt;/b>"; testString = convertHTML(testString); tableWrite("Pre Tags: ", (testString == answerString)," ");

//Testing Simple Table testString = "{| border=\"1\"\n|-\n|a\n|b\n|}\n"; answerString = "<lt;table border=\"1\"><lt;tr><lt;td>a<lt;/td><lt;td>b<lt;/td><lt;/tr><lt;/table>"; testString = convertHTML(testString); tableWrite("Simple Table: ", (testString == answerString), (testString + "\n" + answerString));

testString = "{| border=\"1\"\n|style=\"background: #00AA00\"|a\n\n|a\n|b\n|}\n"; answerString = "<lt;table border=\"1\"><lt;tr><lt;td style=\"background: #00AA00\">a\n\n<lt;/td><lt;td>a\n<lt;/td><lt;td>b\n<lt;/td><lt;/tr><lt;/table>"; testString = convertHTML(testString); tableWrite("Table Without Row Identifier: ", (testString == answerString), (testString + "\n" + answerString)); //Testing Indents testString = ":test\n"; answerString = "<lt;dl><lt;dd>test\n<lt;/dd><lt;/dl>\n"; testString = convertHTML(testString); tableWrite("indents: ", (testString == answerString), testString); testString = ":1\n::2\n:::3\n"; answerString = "<lt;dl><lt;dd>1\n<lt;dl><lt;dd>2\n<lt;dl><lt;dd>3\n<lt;/dd><lt;/dl>\n<lt;/dd><lt;/dl>\n<lt;/dd><lt;/dl>\n"; testString = convertHTML(testString); tableWrite("indents more: ", (testString == answerString), testString); testString = ":1\n::2\n:3\n"; answerString = "<lt;dl><lt;dd>1\n<lt;dl><lt;dd>2\n<lt;/dd><lt;/dl>\n<lt;/dd><lt;dd>3\n<lt;/dd><lt;/dl>\n"; testString = convertHTML(testString); tableWrite("indents stacking: ", (testString == answerString), testString); //Testing Numbered Lines/Lists testString = "#test\n"; answerString = "<lt;ol><lt;li>test\n<lt;/li><lt;/ol>\n"; testString = convertHTML(testString); tableWrite("numbering: ", (testString == answerString), testString); testString = "#1\n##2\n###3\n"; answerString = "<lt;ol><lt;li>1\n<lt;ol><lt;li>2\n<lt;ol><lt;li>3\n<lt;/li><lt;/ol>\n<lt;/li><lt;/ol>\n<lt;/li><lt;/ol>\n"; testString = convertHTML(testString); tableWrite("more numbering: ", (testString == answerString), testString); testString = "#1\n##2\n#3\n"; answerString = "<lt;ol><lt;li>1\n<lt;ol><lt;li>2\n<lt;/li><lt;/ol>\n<lt;/li><lt;li>3\n<lt;/li><lt;/ol>\n"; testString = convertHTML(testString); tableWrite("staggered numbering: ", (testString == answerString), testString);

//Testing Bulletted Lines/Lists testString = "*test\n"; answerString = "<lt;ul><lt;li>test\n<lt;/li><lt;/ul>\n"; testString = convertHTML(testString); tableWrite("bullets: ", (testString == answerString), testString); testString = "*1\n**2\n***3\n"; answerString = "<lt;ul><lt;li>1\n<lt;ul><lt;li>2\n<lt;ul><lt;li>3\n<lt;/li><lt;/ul>\n<lt;/li><lt;/ul>\n<lt;/li><lt;/ul>\n"; testString = convertHTML(testString); tableWrite("more bullets: ", (testString == answerString), testString); testString = "*1\n**2\n*3\n"; answerString = "<lt;ul><lt;li>1\n<lt;ul><lt;li>2\n<lt;/li><lt;/ul>\n<lt;/li><lt;li>3\n<lt;/li><lt;/ul>\n"; testString = convertHTML(testString); tableWrite("staggered numbering: ", (testString == answerString), testString);

//Testing tony's Testing abilities testString = "*:test\n"; answerString = "<lt;ul><lt;li><lt;dl><lt;dd>test\n<lt;/dd><lt;/dl>\n<lt;/li><lt;/ul>\n"; testString = convertHTML(testString); tableWrite("mixed scenario: ", (testString == answerString), testString); testString = "*:test\n*testline2\n"; answerString = "<lt;ul><lt;li><lt;dl><lt;dd>test\n<lt;/dd><lt;/dl>\n<lt;/li><lt;li>testline2\n<lt;/li><lt;/ul>\n"; testString = convertHTML(testString); tableWrite("mixed scenario: ", (testString == answerString), testString); testString = ":{| border=\"1\"\n|-\n|a\n|b\n|}\n"; answerString = "<lt;dl><lt;dd><lt;table border=\"1\"><lt;tr ><lt;td><lt;b>a<lt;/b><lt;/td><lt;td >b<lt;/td><lt;/tr><lt;/table><lt;/dd><lt;/dl>\n"; testString = convertHTML(testString); tableWrite("Indented Table: ", (testString == answerString),(testString + "\n" + answerString));

testString = "link"; answerString = "<lt;a href=\"link\">link<lt;/a>"; testString = convertHTML(testString); tableWrite("basic link:", (testString == answerString), testString); testString = ""; answerString = "<lt;div class=\"center\"><lt;div class=\"thumbinner\"><lt;img alt=\"caption\" longdesc=\"/mediawiki/index.php/Image:Star.gif\" class=\"thumbimage\" src=\"/mediawiki/images/0/08/Star.gif\" width=\"150\" height=\"150\" /><lt;div class=\"thumbcaption\"><lt;div class=\"magnify\" style=\"float:right\"><lt;a href=\"/mediawiki/index.php/Image:Star.gif\" class=\"internal\" title=\"Enlarge\"><lt;img src=\"/mediawiki/skins/common/images/magnify-clip.png\" width=\"15\" height=\"11\" alt=\"\" /><lt;/a><lt;/div><lt;b>caption<lt;/b><lt;/div><lt;/div><lt;/div><lt;/div>"; testString = convertHTML(testString); tableWrite("image:", (testString == answerString), testString);

//Table in Table Testing testString = "{| border=\"1\" cellpadding=\"5\" cellspacing=\"0\" align=\"center\"\n|+An example table\n|-\n! style=\"background:#efefef;\" | First header\n! colspan=\"2\" style=\"background:#ffdead;\" | Second header\n|-\n| upper left\n| \n| rowspan=2 style=\"border-bottom:3px solid grey;\" valign=\"top\" |\nright side\n|-\n| style=\"border-bottom:3px solid grey;\" | lower left\n| style=\"border-bottom:3px solid grey;\" | lower middle\n|-\n| colspan=\"3\" align=\"center\" | \n{| border=\"0\"\n|+Adv Table\n|-\n| align=\"center\" width=\"150px\" | \n| align=\"center\" width=\"150px\" | \n|-\n| align=\"center\" colspan=\"2\" style=\"border-top:1px solid red; border-right:1px solid red; border-bottom:2px solid red; border-left:1px solid red;\" |\nTwo Wikimedia logos\n|}\n|}\n"; answerString = "FAIL"; testString = convertHTML(testString); tableWrite("Table in Table:", (testString == answerString), testString);

testString = "{| border=\"0\"\n|+Adv Table\n|-\n| align=\"center\" width=\"150px\" | \n| align=\"center\" width=\"150px\" | \n|-\n| align=\"center\" colspan=\"2\" style=\"border-top:1px solid red; border-right:1px solid red; border-bottom:2px solid red; border-left:1px solid red;\" |\nTwo Wikimedia logos\n|}\n"; answerString = "FAIL"; testString = convertHTML(testString); tableWrite("Advanced Table 2:", (testString == answerString), testString); testString = "* John\n"; answerString = "<lt;ul><lt;li> John<lt;/li><lt;/ul>"; testExt = "whatever,seomthing,calendar"; testString = convertWiki(convertHTML(testString, testExt), testExt);

tableWrite("quick test:", (testString == answerString), testString); alert(convertWiki(convertHTML('* one bullet\n::* two indent bullet\n:* one indent bullet\n* one bullet\n',testExt),testExt)); /*****Closing out Table information******/ document.write("<lt;/table>"); } function tableWrite(testName, successLevel, extraInfo){ if(successLevel){ document.write("<lt;tr><lt;td>" + testName + "<lt;/td>"); }	else{ document.write("<lt;tr style=\"background: #FF0000\"><lt;td>" + testName + "<lt;/td>"); }	document.write("<lt;td>" + successLevel + "<lt;/td>"); document.write("<lt;td>" + extraInfo + "<lt;/td><lt;/tr>"); } function html2wikiTable(source, extensions){ //Local Variables var ssbt = "";        //SubString Before Table var ssat = "";        //SubString After Table var table_string = ""; //The Table Being Converted var tempStr = "";     //Temporary String, will get used and abused var rowStr = "";      //Row String, used for Multi Parsing var firstPos = 0;     //First Position before the Table var secondPos = 0;    //Second Position after the table var rowBegin = 0;     //Beginning of Row of Data var rowEnd = 0;       //End of Row of Data var dataFirst = 0; var dataSecond = 0; var dataEnd = 0; var newLine = ""; /***********Main Body************/

//Identify Sections of the String firstPos = source.indexOf("<lt;table"); secondPos = source.indexOf("<lt;/table>") + 8;

//Section off the Source String ssbt = source.substring(0,firstPos); table_string = source.substring(firstPos,secondPos); ssat = source.substring(secondPos); //Split out Table Properties firstPos = table_string.indexOf("<lt;table"); secondPos = table_string.indexOf(">",firstPos); tempStr = table_string.substring(firstPos, secondPos); tempStr = tempStr.replace(/<lt;table/,""); if(tempStr.charAt(0) == " "){ tempStr = tempStr.substring(1, tempStr.length + 1); }

//Convert Table Properties table_string = table_string.replace(/<lt;table(.*?)>/,"{|" + tempStr);

//Convert Caption if it Exists table_string = table_string.replace(/<lt;caption>(.*?)<lt;\/caption>/,"|+$1\n");

//WHILE There are Table Rows to Setup while(table_string.indexOf("<lt;tr")>-1){ tempStr = ""; rowStr = ""; //Break Up Current Row into Row Settings and Row Data //<lt;tr(row settings)>(row data)<lt;/tr> firstPos = table_string.indexOf("<lt;tr") + 3; secondPos = table_string.indexOf(">", firstPos); tempStr = table_string.substring(firstPos,secondPos); rowBegin = secondPos + 1; rowEnd = table_string.indexOf("<lt;/tr>"); rowStr = table_string.substring(rowBegin,rowEnd);

//WHILE There are Table Headers while(rowStr.indexOf("<lt;th") > -1){ dataFirst = rowStr.indexOf("<lt;th"); dataSecond = rowStr.indexOf(">", dataFirst); dataEnd = rowStr.indexOf("<lt;/th>", dataFirst); if(rowStr.charAt(dataFirst+3) == " "){ rowStr = rowStr.substring(0, dataFirst+3) + rowStr.substring(dataFirst+4, rowStr.length + 1); dataSecond = rowStr.indexOf(">", dataFirst); dataEnd = rowStr.indexOf("<lt;/th>", dataFirst);

}							if(rowStr.charAt(dataEnd-1) != "\n"){ newLine = "\n" }			else{ newLine = ""; }

//IF There are Cell Properites if(rowStr.substring(dataFirst+3,dataSecond) != ""){ //Grab Settings and put it in the format // ! Settings | Data \n rowStr = rowStr.substring(0, dataFirst) + "!" +				        rowStr.substring(dataFirst+3, dataSecond) + "|" + convertWiki(rowStr.substring(dataSecond + 1, dataEnd), extensions) + newLine+ rowStr.substring(dataEnd + 5, rowStr.length + 1); //rowStr = rowStr.replace(/<lt;th(.*?)>(.*?)<lt;\/th>/,"!$1|$2\n");

}			else{ rowStr = rowStr.substring(0, dataFirst) + "!" +				        convertWiki(rowStr.substring(dataSecond + 1, dataEnd), extensions) + newLine + rowStr.substring(dataEnd + 5, rowStr.length + 1); //rowStr = rowStr.replace(/<lt;th>(.*?)<lt;\/th>/,"!$1\n"); }		}//END While There are Table Headers

//WHILE There are Table Data Cells while(rowStr.indexOf("<lt;td") > -1){ dataFirst = rowStr.indexOf("<lt;td"); dataSecond = rowStr.indexOf(">", dataFirst); dataEnd = rowStr.indexOf("<lt;/td>", dataFirst); if(rowStr.charAt(dataFirst+3) == " "){ rowStr = rowStr.substring(0, dataFirst+3) + rowStr.substring(dataFirst+4, rowStr.length + 1); dataSecond = rowStr.indexOf(">", dataFirst); dataEnd = rowStr.indexOf("<lt;/td>", dataFirst);

}

if(rowStr.charAt(dataEnd-1) != "\n"){ newLine = "\n" }			else{ newLine = ""; }

//IF There are Cell Properites if(rowStr.substring(dataFirst+3,dataSecond) != ""){ rowStr = rowStr.substring(0, dataFirst) + "|" + rowStr.substring(dataFirst+3, dataSecond) + "|" + convertWiki(rowStr.substring(dataSecond + 1, dataEnd), extensions) + newLine + rowStr.substring(dataEnd + 5, rowStr.length + 1); //rowStr = rowStr.replace(/<lt;td(.*?)>(.*?)<lt;\/td>/, "|$1|$2\n"); }			else{ rowStr = rowStr.substring(0, dataFirst) + "|" + convertWiki(rowStr.substring(dataSecond + 1, dataEnd), extensions) + newLine + rowStr.substring(dataEnd + 5, rowStr.length + 1); //rowStr = rowStr.replace(/<lt;td>(.*?)<lt;\/td>/,"|$1\n"); }

}//END While There are Table Data Cells

//Reinsert Blank Data Cells rowStr = rowStr.replace(/\|\|\|\|/g,"|| ||"); //Rebuild the Table String with Modified Data //Rebuild the table row without the Table Row Tags table_string = table_string.substring(0, firstPos - 3) + "|-" + tempStr + "\n" + rowStr + table_string.substring(rowEnd+5, table_string.length + 1); //table_string = table_string.replace(/<lt;tr(.*?)>(.*?)<lt;\/tr>/, "|-" + tempStr + "\n" + rowStr);

}//END While There are Table

//Close Table Table Out table_string = table_string.replace(/<lt;\/table>/,"|}"); table_string = table_string.replace(/<lt;tbody>/g, ""); table_string = table_string.replace(/<lt;\/tbody>/g, ""); //Remove all ">" symbols //table_string = table_string.replace(/>/g,"");

//Return the Converted String return ssbt + table_string + ssat; } function wiki2htmlTable(source, extensions){

//Local Variables var ssbt = "";        //SubString Before Table var ssat = "";        //SubString After Table var table_string = ""; //The Table Being Converted var tempStr = "";     //Temporary String, will get used and abused var firstPos = 0;     //First Position before the Table var secondPos = 0;    //Second Position after the table var thirdPos = 0;     //Special variable Used for Reformatting Rows var tempPos = 0;      //Temporary Position var testPos = 0;      //Position for Testing Locations var rowStr = "";      //Variable to Checking for Multiple Tables var inRow = false;    //Whether we are currently in a row var endCase = false;  //End Case when the system

/***********Main Body************/

//Set up Table String firstPos = source.indexOf("{|"); secondPos = source.indexOf("|}") + 2; //Shifted 2 for end of table chars ssbt = source.substring(0,firstPos); table_string = source.substring(firstPos,secondPos); ssat = source.substring(secondPos,source.length); table_string = table_string.replace(/\n(\s*)\|/mgi, "\n|"); //Convert Table Properties table_string = table_string.replace(/{\|(.*?)\n/,"<lt;table $1>\n"); //Close Out the Table table_string = table_string.replace(/\|}/,"<lt;/table>"); //Check for and Process Caption Tag if Exists in the right place firstPos = table_string.indexOf("\n"); if(table_string.indexOf("|+") == (firstPos + 1)){

secondPos = table_string.indexOf("\n", firstPos+1); tempStr = table_string.substring(firstPos + 3, secondPos); tempStr = convertHTML(tempStr, extensions); table_string = table_string.substring(0,firstPos+1) + "<lt;caption>" + tempStr + "<lt;/caption>" + table_string.substring(secondPos, table_string.length+1); firstPos = table_string.indexOf("<lt;/caption>") + 10;

}

//WHILE There are Newlines before End of Table thirdPos = table_string.indexOf("<lt;/table>", table_string.length - 10); while(thirdPos != (firstPos + 1) && !endCase){

//Parse and send every line into Data Formatter Method secondPos = table_string.indexOf("\n", firstPos + 1); tempStr = table_string.substring(firstPos + 1, secondPos+1);

//IF the line doesn't start with a | or a ! if(tempStr.charAt(0) != '|' && tempStr.charAt(0) != '!'){

//Get position of next \n| starting at 2nd pos tempPos = table_string.indexOf("\n|", secondPos); if(tempPos == -1){ tempPos = 0; }			//Get Position of next \n! starting at 2nd pos testPos = table_string.indexOf("\n!", secondPos) if(testPos <lt; tempPos && testPos > -1){ tempPos = testPos; }			//IF we haven't found a suitable end to the string if(tempPos == 0){ tempPos = thirdPos; endCase = true; }			//Adjust length of tempString to include the new data secondPos = tempPos; firstPos++; tempStr = table_string.substring(firstPos, secondPos);

//Insert data into Appropiate Place tempPos = table_string.indexOf("<lt;/td>", firstPos - 10); if(tempPos == -1){ tempPos = table_string.indexOf("<lt;/th>", firstPos - 10); }			//Insert Temporary String into Location tempStr = convertHTML(tempStr); table_string = table_string.substring(0,tempPos) + tempStr + table_string.substring(tempPos, firstPos) + table_string.substring(secondPos, table_string.length+1); }		else{ //Must Begin with Proper Line Character Setup

//IF Temp Str isn't a Table Row if(tempStr.indexOf("|-") != 0){ //Convert the Currentline into proper format tempStr = wiki2htmlRowLine(tempStr, extensions); //IF We aren't in a Table Row if(!inRow){ //Add on to the Temp String at beginning to start a row tempStr = "<lt;tr>" + tempStr; //Say we are in a Row inRow = true; }

}			else{ // Current Line is a Table Row //Close Out Previous Table Row //IF Currently in a row if(inRow){ //Convert Rest of Line into Proper format tempPos = tempStr.indexOf("|-"); testPos = tempStr.indexOf("\n"); tempStr = "<lt;/tr><lt;tr " + tempStr.substring(0,tempPos) + tempStr.substring(tempPos+2, testPos) + ">";

}				else{ //Convert Rest of Line into Proper format tempPos = tempStr.indexOf("|-"); testPos = tempStr.indexOf("\n"); tempStr = "<lt;tr " + tempStr.substring(0,tempPos) + tempStr.substring(tempPos+2, testPos) + ">";

//Set In row to true inRow = true; }			}//END IF Current Line isn't a Table Row

//Insert Temporary String into Location tempStr = convertHTML(tempStr, extensions); table_string = table_string.substring(0,firstPos+1) + tempStr + table_string.substring(secondPos + 1, table_string.length+1); }//END IF this begins with a proper character

//Move Cursor Forwards firstPos = firstPos + tempStr.length; thirdPos = table_string.indexOf("<lt;/table>", table_string.length - 10);

}//END WHILE More LInes to Convert //IF We are Still in a Table Row if(inRow){ //Close out Table Row table_string = table_string.substring(0,thirdPos) + "<lt;/tr>" + table_string.substring(thirdPos, table_string.length+1); }//END IF We Are still in a table row //Clean up of Any Extraneous Spaces table_string = table_string.replace(/<lt;tr >/g, "<lt;tr>"); table_string = table_string.replace(/<lt;td >/g, "<lt;td>"); table_string = table_string.replace(/<lt;th >/g, "<lt;th>");

//Return the Converted String return ssbt + table_string + ssat; } function wiki2htmlRowLine(source, extensions){ //Local Constants var rowToken = {}; rowToken.start = {}; rowToken.start[1] = '|'; rowToken.start[2] = '!'; rowToken.data = {}; rowToken.data[1] = '|'; rowToken.data[2] = '|'; rowToken.end = {}; rowToken.end[1] = "||"; rowToken.end[2] = "!!"; rowToken.end[3] = "\n";

//Local Variables var currentPos = 0;     //Position in the Line we are on	var convText = "";       //Text that has been converted var strSettings = ""; var strData = ""; var startToken = 0; var endToken = 0; var endPos = 0;         //End Position var dataPos = 0;        //Position for Data Element var tempPos = 0; var refPos = 0; /**********Main Body***************/

//WHILE End Token is not newline character while(endToken != 3){ //Reset End Token endToken = 0; //Figure out which starter we have if(source.charAt(currentPos) == rowToken.start[1]){ startToken = 1; }		else if(source.charAt(currentPos) == rowToken.start[2]){ startToken = 2; }		//Figure out which end token we have (that's closest) endPos = source.indexOf(rowToken.end[1], currentPos + 1); if(endPos > -1){ endToken = 1; }		tempPos = source.indexOf(rowToken.end[2], currentPos + 1); if(tempPos > -1 && (tempPos <lt; endPos || endPos == -1)){ endPos = tempPos; endToken = 2; }				tempPos = source.indexOf(rowToken.end[3], currentPos + 1); if(tempPos > -1 && tempPos <lt; endPos){ endPos = tempPos; endToken = 3; }		//IF No End Token Found if(endToken == 0){ endPos = source.length; endToken = 3; }				//Figure out if we have a Settings/Data Split dataPos = source.indexOf(rowToken.data[startToken], currentPos + 1); //WHILE the matching | is in a link tempPos = source.indexOf("[[", currentPos);

while(tempPos > -1 && dataPos > -1 && tempPos <lt; dataPos){ refPos = dataPos; //Move the Cursor further along in the list dataPos = source.indexOf(rowToken.data[startToken], dataPos + 1); tempPos = source.indexOf("[[", refPos);		}		if(dataPos > -1 && source.charAt(dataPos + 1) != "|"){			strData = source.substring(dataPos + 1, endPos);			strData = convertHTML(strData, extensions);			strSettings = source.substring(currentPos+1,dataPos);		}		else {			strData = source.substring(currentPos + 1, endPos);			strData = convertHTML(strData, extensions);		}		//IF We have Table data Section		if(startToken == 1){			convText = convText + "<lt;td " + strSettings + ">"			          + strData + "<lt;/td>";		}		//ELSE IF We have Table Header Section		else if(startToken == 2){			convText = convText + "<lt;th " + strSettings + ">"			           + strData + "<lt;/th>";					}		//Move Cursor to End Position + 1		currentPos = endPos + 1;	}//END WHILE End Token is not a newline character	//RETURN Converted Text	return convText; } // Ultimate client-side JavaScript client sniff. // (C) Netscape Communications 1998.  Permission granted to reuse and distribute. // Revised 20 April 98 to add is.nav4up and is.ie4up (see below).

// Everything you always wanted to know about your JavaScript client // but were afraid to ask ... "Is" is the constructor function for "is" object, // which has properties indicating: // (1) browser vendor: //    is.nav, is.ie, is.opera // (2) browser version number: //    is.major (integer indicating major version number: 2, 3, 4 ...) //    is.minor (float   indicating full  version number: 2.02, 3.01, 4.04 ...) // (3) browser vendor AND major version number //    is.nav2, is.nav3, is.nav4, is.nav4up, is.ie3, is.ie4, is.ie4up // (4) JavaScript version number: //    is.js (float indicating full JavaScript version number: 1, 1.1, 1.2 ...) // (5) OS platform and version: //    is.win, is.win16, is.win32, is.win31, is.win95, is.winnt, is.win98 //    is.os2 //    is.mac, is.mac68k, is.macppc //    is.unix //       is.sun, is.sun4, is.sun5, is.suni86 //       is.irix, is.irix5, is.irix6 //       is.hpux, is.hpux9, is.hpux10 //       is.aix, is.aix1, is.aix2, is.aix3, is.aix4 //       is.linux, is.sco, is.unixware, is.mpras, is.reliant //       is.dec, is.sinix, is.freebsd, is.bsd //    is.vms // // See http://www.it97.de/JavaScript/JS_tutorial/obj_hierarchy/navobjFr.html // for a detailed list of userAgent strings. // // Note: you don't want your Nav4 or IE4 code to "turn off" or // stop working when Nav5 and IE5 (or later) are released, so // in conditional code forks, use is.nav4up ("Nav4 or greater") // and is.ie4up ("IE4 or greater") instead of is.nav4 or is.ie4 // to check version in code which you want to work on future // versions.

function Is {  // convert all characters to lowercase to simplify testing var agt=navigator.userAgent.toLowerCase

// *** BROWSER VERSION *** this.major = parseInt(navigator.appVersion) this.minor = parseFloat(navigator.appVersion)

this.nav = ((agt.indexOf('mozilla')!=-1) && ((agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1))) this.nav2 = (this.nav && (this.major == 2)) this.nav3 = (this.nav && (this.major == 3)) this.nav4 = (this.nav && (this.major == 4)) this.nav4up = this.nav && (this.major >= 4) this.navonly     = (this.nav && (agt.indexOf(";nav") != -1))

this.ie  = (agt.indexOf("msie") != -1) this.ie3 = (this.ie && (this.major == 2)) this.ie4 = (this.ie && (this.major == 4)) this.ie4up = this.ie  && (this.major >= 4)

this.opera = (agt.indexOf("opera") != -1) // *** JAVASCRIPT VERSION CHECK *** // Useful to workaround Nav3 bug in which Nav3 // loads <lt;SCRIPT LANGUAGE="JavaScript1.2">. if (this.nav2 || this.ie3) this.js = 1.0 else if (this.nav3 || this.opera) this.js = 1.1 else if (this.nav4 || this.ie4) this.js = 1.2 // NOTE: In the future, update this code when newer versions of JS    // are released. For now, we try to provide some upward compatibility // so that future versions of Nav and IE will show they are at    // *least* JS 1.2 capable. Always check for JS version compatibility // with > or >=. else if ((this.nav && (this.minor > 4.05)) || (this.ie && (this.major > 4))) this.js = 1.2 else this.js = 0.0 // HACK: always check for JS version with > or >=

// *** PLATFORM *** this.win  = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) ) // NOTE: On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all //       Win32, so you can't distinguish between Win95 and WinNT. this.win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1))

// is this a 16 bit compiled version? this.win16 = ((agt.indexOf("win16")!=-1)              || (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1)               || (agt.indexOf("windows 16-bit")!=-1) )

this.win31 = (agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) || (agt.indexOf("windows 16-bit")!=-1)

// NOTE: Reliable detection of Win98 may not be possible. It appears that: //      - On Nav 4.x and before you'll get plain "Windows" in userAgent. //      - On Mercury client, the 32-bit version will return "Win98", but //        the 16-bit version running on Win98 will still return "Win95". this.win98 = ((agt.indexOf("win98")!=-1)||(agt.indexOf("windows 98")!=-1)) this.winnt = ((agt.indexOf("winnt")!=-1)||(agt.indexOf("windows nt")!=-1)) this.win32 = this.win95 || this.winnt || this.win98 || ((this.major >= 4) && (navigator.platform == "Win32")) || (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1)

this.os2  = (agt.indexOf("os/2")!=-1) || (navigator.appVersion.indexOf("OS/2")!=-1) || (agt.indexOf("ibm-webexplorer")!=-1)

this.mac   = (agt.indexOf("mac")!=-1) this.mac68k = this.mac && ((agt.indexOf("68k")!=-1) ||                               (agt.indexOf("68000")!=-1)) this.macppc = this.mac && ((agt.indexOf("ppc")!=-1) ||                               (agt.indexOf("powerpc")!=-1))

this.sun  = (agt.indexOf("sunos")!=-1) this.sun4 = (agt.indexOf("sunos 4")!=-1) this.sun5 = (agt.indexOf("sunos 5")!=-1) this.suni86= this.sun && (agt.indexOf("i86")!=-1) this.irix = (agt.indexOf("irix") !=-1)    // SGI this.irix5 = (agt.indexOf("irix 5") !=-1) this.irix6 = ((agt.indexOf("irix 6") !=-1) || (agt.indexOf("irix6") !=-1)) this.hpux = (agt.indexOf("hp-ux")!=-1) this.hpux9 = this.hpux && (agt.indexOf("09.")!=-1) this.hpux10= this.hpux && (agt.indexOf("10.")!=-1) this.aix  = (agt.indexOf("aix")  !=-1)      // IBM this.aix1 = (agt.indexOf("aix 1")  !=-1) this.aix2 = (agt.indexOf("aix 2")  !=-1) this.aix3 = (agt.indexOf("aix 3")  !=-1) this.aix4 = (agt.indexOf("aix 4")  !=-1) this.linux = (agt.indexOf("inux")!=-1) this.sco  = (agt.indexOf("sco")!=-1) || (agt.indexOf("unix_sv")!=-1) this.unixware = (agt.indexOf("unix_system_v")!=-1) this.mpras   = (agt.indexOf("ncr")!=-1) this.reliant = (agt.indexOf("reliantunix")!=-1) this.dec  = (agt.indexOf("dec")!=-1) || (agt.indexOf("osf1")!=-1) || (agt.indexOf("dec_alpha")!=-1) || (agt.indexOf("alphaserver")!=-1) || (agt.indexOf("ultrix")!=-1) || (agt.indexOf("alphastation")!=-1) this.sinix = (agt.indexOf("sinix")!=-1) this.freebsd = (agt.indexOf("freebsd")!=-1) this.bsd = (agt.indexOf("bsd")!=-1) this.unix = (agt.indexOf("x11")!=-1) || this.sun || this.irix || this.hpux || this.sco ||this.unixware || this.mpras || this.reliant || this.dec || this.sinix || this.aix || this.linux || this.freebsd

this.vms  = (agt.indexOf("vax")!=-1) || (agt.indexOf("openvms")!=-1) } // Tony, need to replace assertwiki as well.

function assertwiki {   // This object is used for unit tests. See example below for usage. var ishtml=1; //((print+"").indexOf("native code")>20); var ut={}; var ut_ac={}; var msgall="";

this.sethtml = function { ishtml=1; }; this.add = function(key, ac, ac2) { if(key in ut) printit("["+key+"] is redefined.","red"); ut[key]=convertWiki(convertHTML(ac+'\n','something,kw_include_file,calendar,yay'), 'something,kw_include_file,calendar,yay'); if(ac2==undefined) { ut_ac[key]=ac; } else { ut_ac[key]=ac2; } };  this.run = function(wht, mode) {   // wht="all"/key,  mode=0-pass/fail, 1-pass/fail display fail, 2-summary, 3-verbose var ii, tot=0, pass=0; if(wht=='all') { for(ii in ut) { runit(ii); }   } else { runit(wht); }   printit(msgall+"Tests="+tot+" Pass="+pass+" FAIL="+(tot-pass)+(tot==pass?" ... clean Run":" >>>>>>> NOT CLEAN"),"green");

// Done

function runit(key) { var tpass=0; var utac_res, res,msg1,msg2,msg3; if(!(key in ut)) { printit("["+key+"] is not defined","red"); return; } tot++;

if(typeof(ut[key])=='function') res=ut[key]; else  res=ut[key]; res=res.replace(/^\n/, ""); res=res.replace(/\n$/, ""); //res=res.replace(/ /g, "+"); res=res.replace(/ border/, " border");   // bandaid for the tables if(typeof(ut_ac[key])=='function') utac_res=ut_ac[key]; else  utac_res=ut_ac[key]; msg1=key+" ..... Expected: ["+utac_res+"]";

// Execute algorithm if(typeof(res)=='string' || typeof(res)=='number' || typeof(res)=='boolean') { if(res==utac_res) tpass=1; msg2=key+" ....... Actual: ["+res+"]"; } else { if(res instanceof Array) { var res2=res.join; if(res2==utac_res) tpass=1; msg2=key+" ....... Actual: ["+res2+"]"; } else { msg2="["+key+"] returned an unknown type. Only String, Number, boolean or Array is allowed."; }     }

// Just display stuff msg3=key+" is a "+(tpass?"Pass":"FAIL!!!!!!!!"); if(mode==3 || ( mode==1 && (!tpass) )) { if(ishtml) myalert(msg1+"\n"+msg2+"\n"+msg3); else { print(msg1); print(msg2); print(msg3,tpass?"blue":"red"); }     }      if(mode==2) { msgall=msgall+msg3+"\n"; }     if(tpass)   pass++;

} // end runit }; // end .run

function myalert(txt) { if(typeof(_halttrue)!='undefined') _haltalready; if(txt!=undefined) { if(!confirm(txt+"\n\nContinue?")) { _halttrue=1; _haltalready; } }   }

function printit(txt, clr) { if(ishtml) myalert("Assert:\n"+txt); else print(txt, clr); }

} // end assert object

var aa=new assertwiki;

// START OF TEST CASES Ver0.3b ==================================================

aa.add('bullet0', '=== h3 ===\n* 1b\n* 1b\n:: 2i\n: 1i\n=== ah ===\n* 1b\n::* 2ib\n:* 1ib\n* 1b\n== ah ==\n::* 2i\nplain\n::: 3i\n* 1b\n::* 2ib'); aa.add('bullet1', '=== heading 3 ===\n* one bullet\n* one bullet\n:: two indent\n: one indent'); aa.add('bullet2', '* one bullet\n::* two indent bullet\n:* one indent bullet\n* one bullet'); aa.add('bullet3', '* b\n:* ib\n::* iib\n:::* iiib\nnone\n: i\n:: ii\n::: iii\n::: iii\nnone\n::: iii\n:: ii\n* b\n::* iib'); aa.add('number', '# one\n# two two\n#three\n# four\n\n#one\n: indent');

aa.add('boldpre', '<lt;b>bold<lt;/b>\n<lt;pre>text1\ntext2\n<lt;/pre>', "bold\n text1\n text2"); aa.add('newline', 'line1\nline2\n\nline3\n\n\nline3'); aa.add('linkwiki','text Text t"ex"t page text text again\n* bullet'); aa.add('linkhtml',"text Text t'ex't text\n"); aa.add('mailto' ,'[mailto:john.intel@intel?subject=xplanner_1sttimeuser_request_access mailhere] text [mailto:john.intel@intel] text');

aa.add('basic',  "=head=\n* bullet\nline text\n: indent\nbold text italic text 'another text'"); aa.add('tableconv', '<lt;table><lt;tr><lt;td>data<lt;/td><lt;td>data1<lt;/td><lt;/tr><lt;tr><lt;td>data2<lt;/td><lt;td>data3<lt;/td><lt;/tr><lt;/table>', '{||-\n|data\n|data1\n|-\n|data2\n|data3\n|}'); aa.add('heading', '=head1 =\n*hello\n=Head1a=hello again\n==Head3 2 ==\nhello line 1\n===Head3 ===\n====Head4 ====\nhello\n====Head4a ====\nhello'); aa.add('media', '[[Media:Namespace_filename.ppt| hyperlink_text]]: outtext');

aa.add('tablebasic', '{| border="0"\n|-\n! Acronym \n! Definition\n|-\n| ATPG\n| Automated Test Pattern Generation\n|-\n| SAF\n| Stuck-at-fault\n|}'); aa.add('indentbullettable', '*line1\n::{| border="0"\n|-\n! Acronym \n! Definition\n|-\n | ATPG\n  | Automated Test Pattern Generation\n|-\n| SAF\n| Stuck-at-fault\n|}', '*line1\n::{| border="0"\n|-\n! Acronym \n! Definition\n|-\n| ATPG\n| Automated Test Pattern Generation\n|-\n| SAF\n| Stuck-at-fault\n|}'); aa.add('pre', '* Bullet\n pre\n pre\n: indent\n* bullet'); aa.add('pre2', '* a\n This is a pre\nnon-pre\n<lt;pre>\ninside pre box\n<lt;b>yo<lt;/b>\n<lt;/pre>','* a\n This is a pre\nnon-pre\n inside pre box\n <lt;b>yo<lt;/b>'); aa.add('comment', '*line\n<lt;'+'!-- comment\nend'+'>\n*line2'); aa.add('3rdparty', '*line\n<lt;kw_include_file>yay&y=a<lt;/kw_include_file>\n*line');

aa.add('fontsize', '*line\n<lt;font size="+1"> Methodology & Planning<lt;/font>\n;hey'); aa.add('colonsemi', 'line\n; ATPG: try\n;ATPG\nyikes'); aa.add('indenttable1', '*line1\n:{| border="0"\n|-\n! Acronym \n! Definition\n|-\n|\n;ATPG\n: indent\n* bullet\n\n|}');

aa.add('nowiki', "* sentence <lt;nowiki>'yay'<lt;/nowiki>\n<lt;nowiki><lt;b>yay<lt;/b><lt;/nowiki>"); aa.add('twolink-a', '*Nhm Carve the Pig / (Sharepoint) : High level work assignments for Nhm'); aa.add('twolink-b', ';FMEA Training, xls: text after'); aa.add('one-linka', ':2006 Team Structure & Release plan'); aa.add('image', '; text\n*  text\n::: '); aa.add('italic_underline', '<lt;i>italic<lt;/i><lt;u>underline<lt;/u>\n<lt;blockquote>line1\nline2\n<lt;/blockquote><lt;'+'!-- comment --'+'>',"italic<lt;u>underline<lt;/u>\n<lt;blockquote>line1\nline2\n<lt;/blockquote><lt;"+"!-- comment --"+">"); aa.add('coloncolon', ": sub proj: Put details here"); aa.add('tablespan', '{| border="1"\n|-\n! Head1\n! colspan="2"| Head2\n|-\n| ATPG\n|}'); aa.add('true type', "<lt;tt>true type<lt;/tt>\n");

aa.run("all",1);       // wht="all"/key,  mode=0-pass/fail, 1-pass/fail display fail, 2-summary, 3-verbose

//========================== END