User:Nat/Table Converter
Jump to navigation
Jump to search
My own table converter. I used to use Voidious's one, but that one create long table style, not compact style. But this one I created can convert to both long and short (inline) style. Try it out. Comment are welcome.
Available at http://wiki.robothai.net/table_converter
- Unimplemented List
(Help me if you can)
- Alignment & Table Style (the hardest one)
- MediaWiki table parser
- UseMod table creator
- How it work
It just simple PHP page. Using jQuery to do some AJAX. It have several functions:
- process() - this one will read $_POST and generate the output.
- usemodTableParser - read the UseModWiki-style table and return an array represent table.
- mediaTableCreater - take an array that represent table and generate MediaWiki-style table.
Table represent in array like:
Array ( [0] => Array( [0] => row1cell1, [1] => row1cell2, ... ), [1] => Array( [0] => row2cell1, [1] => row2cell2, ... ), ... )
Source Code
License under Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Thailand License
<?php error_reporting(E_ALL); define ('LINE', "<br />\n"); $ajax = isset($_GET['ajax']) && $_GET['ajax']; $submit = isset($_GET['submit']) && $_GET['submit']; $val = isset($_POST['table']) ? stripslashes($_POST['table']) : ''; if ($ajax) { process(); die; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <head> <title>MediaWiki Table Converter</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#ct').submit(function(event){ event.preventDefault(); $.post('?ajax=1', { table: $('#table').val(), from: $('#from').val(), to: $('to').val(), inline: $('#inline').is(':checked'), whole: $('#whole').is(':checked') }, function(data, status) { if (status != 'success') { alert('Error: ' + status); } else { $('#result').html(data); } } , 'text'); return false; }); }); </script> <style type="text/css"> #usemod-style {border:1px solid #f00; color:#f00; padding:10px;} #table {width:100%} #result {font-family:courier, monospace;font-size:x-small;} </style> </head> <body> <form action="?submit=1" method="post" id="ct"> <h2>Convert</h2> <div id="usemod-style"> WARNING: Converting table cause lost of <strong>all</strong> table styles. </div> <p> <label for="from">From:</label> <select name="from" id="from" disabled="disabled"> <option value="usemod" selected="selected">UseModWiki Syntax</option> <option value="media">MediaWiki Syntax</option> </select> <label for="to">To:</label> <select name="to" id="to" disabled="disabled"> <option value="usemod">UseModWiki Syntax</option> <option value="media" selected="selected">MediaWiki Syntax</option> </select> <br/> <input type="checkbox" name="inline" id="inline" value="true" checked="checked" /> <label for="large">Inline table</label> <input type="checkbox" name="whole" id="whole" value="true" disabled="disabled" /> <label for="whole">Whole Page</label> </p> <p> <label id="table-label" for="table">Table</label>: <br /> <textarea id="table" name="table" rows="10" cols="80"><?php echo $val ?></textarea> </p> <p> <input type="submit" value="Convert" id="convert"/> </p> </form> <hr/> <div id="result"> <?php if ($submit) process(); ?> </div> </body> </html> <?php function process() { $inline = isset($_POST['inline']) ? $_POST['inline'] == 'true' : false; echo mediaTableCreator(usemodTableParser(stripslashes($_POST['table'])), $inline); } function usemodTableParser($table) { $return = array(); $table = explode("\n", $table); foreach ($table as $line) { $line = trim($line); $line = explode('|', $line); array_shift($line); array_pop($line); $row = array(); foreach ($line as $cell) { $row[] = $cell; } $return[] = $row; } return $return; } function mediaTableCreator($table, $inline = true, $style = 'border="1" style="border-collapse:collapse;color:black"') { $out = '{| ' . $style . LINE; $sep = ''; foreach ($table as $row) { $out .= $sep; $sep = '|-' . LINE; $sep2 = '| '; foreach ($row as $cell) { $out .= $sep2; if ($inline) $sep2 = ' || '; else $sep2 = LINE . '| '; $out .= $cell; } $out .= LINE; } return $out . '|}'; } function usemodTableCreator($table) { /* TODO Creating usemod table creator */ } function mediaTableParser($table) { /* TODO Creating mediawiki table parser */ } ?>