KidzSearch Encyclopedia:AutoEd/tablestowikitext.js
//<source lang=javascript> //From Plastikspork's script
function autoEdTablestoWikitext(str) { //MAIN FUNCTION describes list of fixes
// Remove newlines from inside table specific tags var loopcount = 0; while( str.search(/(?:<\/?table|<\/?tr|<\/?td|<\/?th)[^<>]*[\r\n]/gi) >= 0 && loopcount <= 10 ) { str.replace(/((?:<\/?table|<\/?tr|<\/?td|<\/?th)[^<>]*)[\r\n]/gi, '$1 ') loopcount++; } // Remove extra whitespace from inside table specific tags str=str.replace(/(<table|<tr|<td|<th)([^<>]*?)[\s]+(>)/gim, '$1$2$3'); str=str.replace(/(<table|<tr|<td|<th)([^<>]*?)[\s][\s]+/gim, '$1$2 ');
// Remove any extra junk , , ,
str=str.replace(/(<\/table|<\/tr|<\/td|<\/th)[^<>]+(>)/gim, '$1$2');
// Remove space whitespace after , , ,
str=str.replace(/(<\/tr>|<\/td>|<\/th>|<table[^<>]*>)[\s]+/gim, '$1'); // Remove space before ,, | , |
---|
str=str.replace(/[\s]+(<\/table>|<tr[^<>]*>|<td[^<>]*>|<th[^<>]*>)/gim, '$1');
// Replace '
' with '{| stuff' str=str.replace(/<table( [^<>]*|)>[\s]*/gim, '{|$1\n'); // Replace '' with '|}'
str=str.replace(/[\s]*<\/table>/gi, '\n|}');
// Replace '' with '||'
str=str.replace(/<\/td[\s]*>[\s]*<td[\s]*>/gim, '||'); str=str.replace(/<\/td[\s]*>[\s]*<td ([^<>]+)>/gim, '|| $1 |');
// Replace '' with '!!'
str=str.replace(/<\/th[\s]*>[\s]*<th[\s]*>/gim, '!!'); str=str.replace(/<\/th[\s]*>[\s]*<th ([^<>]+)>/gim, '!! $1 |');
// Replace '' and '' with EOL str=str.replace(/<\/(?:td|th)>[\s]*<\/tr>[\s]/gim, '\n'); // Replace '', '', '' with EOL str=str.replace(/<\/(?:td|th|tr)>[\s]*/gim, '\n'); // Replace '' with '|-' str=str.replace(/[\s]*[\s]*/gim, '\n|-\n'); str=str.replace(/[\s]*<tr ([^<>]*)>[\s]*/gim, '\n|- $1\n'); // Replace '' with '|' str=str.replace(/[\s]*([^\s])/gim, '\n| $1'); str=str.replace(/[\s]*([\s])/gim, '\n|$1');
str=str.replace(/[\s]*<td[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n| $1 | $2'); str=str.replace(/[\s]*<td[\s]*([^<>]*?)[\s]*>([\s])/gim, '\n| $1 |$2');
// Replace '' with '!' str=str.replace(/[\s]*([^\s])/gim, '\n! $1'); str=str.replace(/[\s]*([\s])/gim, '\n!$1');
str=str.replace(/[\s]*<th[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n! $1 | $2'); str=str.replace(/[\s]*<th[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n! $1 |$2'); return str;
}
//</source>