Jump to content

Wikipedia:AutoEd/tablestowikitext.js

From Wikipedia, the free encyclopedia
//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 </tr>, </td>, </th>, </table>
  str=str.replace(/(<\/table|<\/tr|<\/td|<\/th)[^<>]+(>)/gim, '$1$2');
  // Remove space whitespace after </tr>, </td>, </th>, <table>
  str=str.replace(/(<\/tr>|<\/td>|<\/th>|<table[^<>]*>)[\s]+/gim, '$1');
  // Remove space before <tr>, <td>, <th>, </table>
  str=str.replace(/[\s]+(<\/table>|<tr[^<>]*>|<td[^<>]*>|<th[^<>]*>)/gim, '$1');
  // Replace '<table>' with '{|'
  str=str.replace(/<table( [^<>]*|)>[\s]*/gim, '{|$1\n');
  // Replace '</table>' with '|}'
  str=str.replace(/[\s]*<\/table>/gi, '\n|}');
  // Replace '</td><td>' with '||'
  str=str.replace(/<\/td[\s]*>[\s]*<td[\s]*>/gim, '||');
  str=str.replace(/<\/td[\s]*>[\s]*<td ([^<>]+)>/gim, '|| $1 |');
  // Replace '</th><th>' with '!!'
  str=str.replace(/<\/th[\s]*>[\s]*<th[\s]*>/gim, '!!');
  str=str.replace(/<\/th[\s]*>[\s]*<th ([^<>]+)>/gim, '!! $1 |');
  // Replace '</td></tr>' and '</th></tr>' with EOL
  str=str.replace(/<\/(?:td|th)>[\s]*<\/tr>[\s]/gim, '\n');
  // Replace '</td>', '</th>', '</tr>' with EOL
  str=str.replace(/<\/(?:td|th|tr)>[\s]*/gim, '\n');
  // Replace '<tr>' with '|-'
  str=str.replace(/[\s]*<tr>[\s]*/gim, '\n|-\n');
  str=str.replace(/[\s]*<tr ([^<>]*)>[\s]*/gim, '\n|- $1\n');
  // Replace '<td>' with '|'
  str=str.replace(/[\s]*<td>([^\s])/gim, '\n| $1');
  str=str.replace(/[\s]*<td>([\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 '<th>' with '!'
  str=str.replace(/[\s]*<th>([^\s])/gim, '\n! $1');
  str=str.replace(/[\s]*<th>([\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;
}