ব্যবহারকারী:RiazACU/AutoEd/tablestowikitext.js
লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।
- ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
- গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
- ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
- অপেরা: Ctrl-F5 টিপুন।
//<syntaxhighlight 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 </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;
}
//</syntaxhighlight>