Extension:SimpleTable

I created this very simple extension to allow tabular data to be easily cut-and-pasted into a Wiki; for example, this allows a CSV export from Excel to be pasted in without having to manually edit it into Wiki table syntax. It's really crude, and doesn't allow any kind of clever formatting; for example, there is no way to set row and cell parameters, including row and cell spanning. OTOH, it's really simple, and it would be pretty easy to expand the parser (the convertTable function) to handle more complex input.

Suggestions for enhancements are welcome (on the talk page). However, please bear in mind that if we do anything fancy in the markup, it will end up being not much less complex than the Wiki table markup; the whole concept of this extension is to give the user something which is absolutely minimal, and requires no markup learning. If the user has to learn any markup at all, they may as well learn the real MW table markup.

There are now two versions available; a non-class-based version for PHP 4, and an enhanced, class-based version that works with PHP 5 and on MediaWiki 1.9.3. Both versions include Smcnaught's improvement to allow Wiki text within the table — thanks, Smcnaught! — JohanTheGhost 15:25, 12 April 2007 (UTC)

Update: the PHP 5 version now includes a new "sep" option, "bar", and the "head" parameter (see below). JohanTheGhost 17:18, 19 April 2007 (UTC)

Version for PHP 5 (MediaWiki 1.9)
This version requires PHP version 5 or later, and is known to run on MediaWiki 1.9.3. To install it:
 * Get the code
 * Save it as  in your   folder
 * Add this line to your :
 * require_once( "$IP/extensions/TabbedData.php" );

To use it, all you need to do is prepare your data in rows, with fields separated by tab characters. (Excel's "Save as" → "Text (Tab delimited)" function saves data in this format.) Place the data inside  ...  tags, and set any table parameters inside the opening   tag: Field 1→Field 2→Field 3 Field 4→Field 5→Field 6 Field 7→Field 8→Field 9 (where → represents a tab character) produces the corresponding table.

You can use a different separator, by specifying it as the  parameter. can be one of:
 * tab: a tab
 * space: a space
 * spaces: one or more spaces and/or tabs
 * comma: a comma
 * bar: a vertical bar (|)

You can also specify that either the top row or the left column should be formatted as a heading, using the  parameter, which takes one of two values:
 * top: make the top row a heading
 * left: make the left column a heading

For example:  Head 1,Head 2,Head 3 Field 4,Field 5,Field 6 Field 7,Field 8,Field 9 produces a table with a heading row and two data rows, all having three columns.

Version for PHP 4 (MediaWiki 1.6)
This version works on PHP version 4, and is known to run on MediaWiki 1.6. To install it:
 * Get the code for PHP 4
 * Save it as  in your   folder
 * Add this line to your :
 * require_once( "$IP/extensions/TabbedData.php" );

Usage is the same as above, except that the  and   parameters are not supported.

Code: for PHP 5
<?php

/* * Tabbed Data extension. * * This extension implements a simple method to display tabbed data, with a * far simpler markup than the standard Wiki table markup. The goal is to * allow tabular data to be very easily pasted in from an external source, * such as a spreadsheet. The disadvantage is that it doesn't allow for any * fancy formatting; for example, there is no way (as yet) to set row and cell * parameters, including row and cell spanning. However, it makes a very * simple way to include tabular data in a Wiki. * * All you need to do is prepare your data in rows, with fields separated * by tab characters. Excel's "Save as" -> "Text (Tab delimited)" function * saves data in this format. Place the data inside ... tags, * and set any table parameters inside the opening tag; eg.: * *   *   field1\tfield2\t... *   field1\tfield2\t... *   * * Additional parameters allowed in the tag are: *  sep        Specify a different separator; see the $separators array. *  head       Specify a heading; "head=top" makes the first row a heading, *             "head=left" makes the first column a heading. * * Thanks for contributions to: *	Smcnaught *	Frederik Dohr */

$wgExtensionFunctions[] = 'wfTabbedData'; $wgExtensionCredits['parserhook'][] = array( 'name'=>'TabbedData',  'author'=>'Johan the Ghost',  'url'=>'http://meta.wikimedia.org/wiki/TabbedData_extension',  'description'=>'convert tabbed data into a Wiki table', );

/* * Setup TabbedData extension. * Sets a parser hook for. */ function wfTabbedData { new TabbedData; }

class TabbedData {

/*    * The permitted separators. An array of separator style name * and preg pattern to match it. */   private $separators = array(        'space' => '/ /',        'spaces' => '/\s+/',        'tab' => '/\t/',        'comma' => '/,/',        'bar' => '/\|/',    );

/*    * Construct the extension and install it as a parser hook. */   public function __construct { global $wgParser; $wgParser->setHook('tab', array(&$this, 'hookTab')); }

/*    * The hook function. Handles. * Receives the table content and parameters. */   public function hookTab($tableText, $argv, &$parser) { // The default field separator. $sep = 'tab';

// Default value for using table headings. $head = null;

// Build the table parameters string from the tag parameters. // The 'sep' and 'head' parameters are special, and are handled // here, not passed to the table. $params = ''; foreach (array_keys($argv) as $key) { if ($key == 'sep') $sep = $argv[$key]; else if ($key == 'head') $head = $argv[$key]; else $params .= ' '. $key. '="' . $argv[$key] . '"'; }

if (!array_key_exists($sep, $this->separators)) return "Invalid separator: $sep";

// Parse and convert the table body. $wikiText = $this->convertTable($tableText, $head, $this->separators[$sep]);

// Wrap the body in table tags, with the table parameters. $wikiTable = "{|". $params. "\n". $wikiText. "|}";

// Done. Parse the result, so that the table can contain Wiki // text. Thanks to Smcnaught. $ret = $parser->parse($wikiTable,                             $parser->mTitle,                              $parser->mOptions,                              false,                              false); return $ret->getText; }

/*    * Convert tabbed data into a Wiki-markup table body. */   private function convertTable($tabbed, $head, $pattern) { $wikitab = '';

// Remove initial and final newlines. $tabbed = trim($tabbed);

// Split the input into lines, and convert each line to table format. $lines = preg_split('/\n/', $tabbed); $row = 0; foreach ($lines as $line) { $wikitab .= "|-\n"; $bar = $head == 'top' && $row == 0 ? '!' : '|';

$fields = preg_split($pattern, $line); $col = 0; foreach ($fields as $field) { $cbar = $head == 'left' && $col == 0 ? '!' : $bar; $wikitab .= $cbar. " " . $field. "\n"; ++$col; }       	++$row; }       return $wikitab; }

}

?>

Code: for PHP 4
<?php

/* * Tabbed Data extension. * * This extension implements a simple method to display tabbed data, with a * far simpler markup than the standard Wiki table markup. The goal is to * allow tabular data to be very easily pasted in from an external source, * such as a spreadsheet. The disadvantage is that it doesn't allow for any * fancy formatting; for example, there is no way (as yet) to set row and cell * parameters, including row and cell spanning. However, it makes a very * simple way to include tabular data in a Wiki. * * All you need to do is prepare your data in rows, with fields separated * by tab characters. Excel's "Save as" -> "Text (Tab delimited)" function * saves data in this format. Place the data inside ... tags, * and set any table parameters inside the opening tag; eg.: * *   *   field1\tfield2\t... *   field1\tfield2\t... *   */

$wgExtensionFunctions[] = 'wfTabbedData'; $wgExtensionCredits['parserhook'][] = array(                                           'name'=>'TabbedData',                                            'author'=>'Johan the Ghost',                                            'url'=>'http://meta.wikimedia.org/wiki/TabbedData_extension',                                            'description'=>'convert tabbed data into a Wiki table',                                            );

/* * Setup TabbedData extension. * Sets a parser hook for. */ function wfTabbedData { global $wgParser; $wgParser->setHook('tab', 'hookTabbedData'); }

/* * The hook function. Handles. * Receives the table content and parameters. */

// * Added $parser=null to hook:

function hookTabbedData($tableText, $argv, $parser=null) { global $wgOut; // Build the table parameters string from the tag parameters. $params = ''; foreach (array_keys($argv) as $key) { $params .= ' '. $key. '="' . $argv[$key] . '"'; }

if ($params == "") { $params = " border=\"1\" cellpadding=\"2\" cellspacing=\"0\" class=\"wikitable\""; }

// Parse and convert the table body. $wikiText = convertTable($tableText);

// Wrap the body in table tags, with the table parameters. $wikiTable = " \n";

// Done.

// * Added parse call that reserves the Parse reference.

// Reserve Parse Reference $ret = $parser->parse(                       $wikiTable,                        $parser->mTitle,                        $parser->mOptions,                        false,                        false                        ); return $ret->getText;

}

/* * Convert tabbed data into an HTML table body. */ function convertTable($tabbed) { $wikitab = "";

// Remove initial and final newlines. $tabbed = trim($tabbed);

// Split the input into lines, and convert each line to table format. $lines = preg_split('/\n/', $tabbed); foreach ($lines as $line) { $wikitab .= " \n"; $wikitab .= " ". preg_replace('/\t/', ' ', $line). " \n"; $wikitab .= " \n"; } return $wikitab; }

?>