Tabtable-raf

Summary: tabtable with RAF patch of 2007/01/23 applied
Version: 1
Prerequisites: paste over tabtable, also required excel.gif that is included in alternate tabtable.zip download
Status: tested
Maintainer: RAF (patch written by RAF, applied by jtankers -> mailto:jim_tank [snail] hotmail [period] com;jtankers [snail] stratagemnet [period] com)
Categories: Tables

Questions answered by this recipe

Updated version of tabtable.php that includes the following patches:

  • 2007/01/23; RAF; Add header row

This section is optional; use it to indicate the types of questions (if any) this recipe is intended to answer.

Description

Enter your recipe's description here. Please download alternate tabtable.zip (which includes excel.gif). Then to apply the first line header patch written by RAF on 2007/01/23, replace the code in tabtable.php with the code in the Notes section below:

Notes


<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005 Patrick R. Michaud (pmichaud@pobox.com)
    This file is tabtable.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  

    This script makes it possible to convert spreadsheets copy-and-pasted
    from other applications (such as Microsoft Excel) into PmWiki
    simple tables.  Numeric cells are automatically right-aligned,
    while other cells are left-aligned.  Internally, the script
    simply converts lines containing tab characters into PmWiki
    table cells.

    Currently the script enables itself via the GUIButton bar.
    (Contact the author if you want this feature without GUIButtons.)
    Place this script in the cookbook/ directory, the excel.gif
    file in pub/guiedit/ , and add the following line to config.php:

        include_once('cookbook/tabtable.php');

    Updates:
    2007/08/06 jtankers Updated with RAF patch to create header row

*/

SDV($EnableGUIButtons, 1);
SDV($GUIButtons['tabtable'], array(1100, '', '', '',
  "<input type='image' name='tabtable' src='\$GUIButtonDirUrlFmt/excel.gif' title='\$[Convert tabs to table]' />"));

SDV($HTMLStylesFmt['tabtable'],' 
  table.tabtable { border-collapse: collapse; }
  table.tabtable td { border:1px solid #cccccc; }
');

if (@$_POST['tabtable_x']) array_unshift($EditFunctions, 'TabTable');
else return;

function TabTable($pagename, &$page, &$new) {
  if (!@$_POST['tabtable_x']) return;
  $lines = explode("\n", $new['text']);
  $row = array(); $wid = array();
  while ($lines || $row) {
    $x = array_shift($lines);
    if ($x==='' && $row) { $row[] = array(); continue; }
    if (strpos($x, "\t") !== false) {
      $col = explode("\t", $x);
      $row[] = $col;
      for($i=0; $i<count($col); $i++) 
        $wid[$i] = max(@$wid[$i], strlen($col[$i]));
      continue;
    }
    while ($row && count(end($row))==0) { array_pop($row); $x = "\n".$x; }
    if ($row) {
      $y[] = "||class='tabtable'";
      $_first = '!'; /* first pass use heading formats (RAF-2007/08/06) */ 
      while ($row) {
        $col = array_shift($row);
        $r = '||';
        for($i=0; $i<count($wid); $i++) {
          $m = preg_match('/^\\s*\\(?-?[\\d,]+\\.?\\d*\\)?\\s*$/', @$col[$i]) 
          /* Removed: ? " %{$wid[$i]}s||" : "%-{$wid[$i]}s ||";  (RAF-2007/08/06) */
          ? " $_first%{$wid[$i]}s||" : "$_first%-{$wid[$i]}s ||";   /* (RAF-2007/08/06) */ 
          $r .= sprintf($m,@$col[$i]);
        }
        $y[] = $r;
        $_first = '';   /* (RAF-2007/08/06) */ 
      }
      $row = array(); $wid = array();
    }
    if (!is_null($x)) $y[] = $x;
  }
  $new['text'] = implode("\n", $y);
  $_POST['preview'] = 1;
}

?>

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

See Also

Contributors

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.