Extension:SimpleTable/1.0

From mediawiki.org
Jump to navigation Jump to search
<?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 <tab>...</tab> tags,
 * and set any table parameters inside the opening <tab> tag; eg.:
 *
 *   <tab class=wikitable>
 *   field1\tfield2\t...
 *   field1\tfield2\t...
 *   </tab>
 */

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

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

/*
 * The hook function. Handles <tab></tab>.
 * Receives the table content and <tab> 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 = "<table" . $params . ">\n" . $wikiText . "</table>\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 .= "<tr>\n";
    $wikitab .= "<td>" . preg_replace('/\t/', '</td><td align=center>', $line) . "</td>\n";
    $wikitab .= "</tr>\n";
  }
  return $wikitab;
}