Revision [16819]

This is an old revision of TableAction made by Tw9Za3 on 2007-05-31 10:50:46.

 

Table Action


See also:
This is the development page for the Table action.
 

History

The original Table action available in Wikka was from IanAndolina's nontroppo wiki. Note: Searching on the web reveals that Ian is not the original author. Credit goes to Michael Abendroth for being the original author.

The original Table action provides a nice way to create a simple HTML table.

This Table action has a number of drawbacks, however:
  1. It is awkward to use for anything but the most simple tables - mainly because it's an action, which is different from the common approach to table markup in many Wikis: Most WikiEngines that support tables provide a table markup instead of a "plugin"-like solution like this.
  1. Cell contents can't have Wikka markup (if they do, the markup is shown as raw text instead of formatted).
  1. The action does not actually produce data table markup (which it should).

Preliminary solution

There is an ongoing discussion about providing true table markup in Wikka:

Clearly, we need a better solution than the current action, preferably (some kind of) true table markup syntax.
However, it would be unwise to release a "preliminary" markup, as it could easily lead to confusion among our user base (as well as needless conversions down the line), and extra work for the developers.
Instead, we should first determine what we want to be able to do in terms of table markup, and then look if we can find a simpler preliminary solution (as a subset of that markup) that still enables us to get there. And any preliminary solution should at least address the problem of ease-of-use and generating data table markup.

So, until we have come to a clear conclusion about "Wikka table markup", we won't have a preliminary Wikka table markup either. Still, there is a clear (and sometimes pressing) need for an easier way to produce HTML tables from Wikka code. Therefore I've investigated whether it was possible to extend the current Table action to provide more flexibility and address the major drawbacks at least partially. I was glad to find it wasn't actually all that hard.

I'm presenting here a rewrite of the Table action and a minor patch to the main wikka.php file (which could benefit other actions as well). It's far from a perfect solution for producing tables - but it's a lot more flexible and easier to use than the current table action. So I'm hoping this can tide us over until we embark on true table markup.

Patch for ./wikka.php
Two very small changes in ./wikka.php are going to provide us with the framework that enables the new Table action to be much easier to use. Essentially, they make it possible to:
The second option, while very important for easy maintainability of Table action content (particularly, the cell contents), should be approached with caution however: currently (most? all?) actions are not written to take advantage of this. While the new Table action is written specifically to make use of this, other actions may actually fail if multi-line parameters are passed to them. Current action code won't fail, but all action code will need to be investigated (and maybe patched) if the new capability causes problems.

In ./wikka.php, find the Action function. Then replace this line:
            preg_match("/^([A-Za-z0-9]*)\s (.*)$/", $action, $matches);
by this:
            preg_match("/^([A-Za-z0-9]*)\s (.*)$/s", $action, $matches);
and replace this line:
                preg_match_all("/([A-Za-z0-9]*)=\"(.*)\"/U", $vars_temp, $matches);
by this:
                preg_match_all("/([A-Za-z0-9]*)=\"(.*)\"/sU", $vars_temp, $matches);

In both cases, all we do is add an 's' after the final '/' of the regular expression, which will make it match across multiple lines.

The first change allows us to "recognize" an action that spans multiple lines (so you can put each parameter on a new line, if desired); the second change allows parameter content to span multiple lines (a parameter value must still be enclosed in double quotes to be recognized, though). Careful with the latter one: not all actions may be able to handle such multi-line parameter values!!

New Table action
Starting with the current Table action, refactoring, refactoring, refactoring .... many of you won't be surprised that I ended up with a complete rewrite of the table action. It provides a lot more flexibility but is completely backwards-compatible with the current table action. Still, if you want to test only, I'd advise you to store the code under the name of table2.php rather than table.php (as indicated below): that way you can test without influencing the operation of any current table actions on your Wikka site.

Replace ./actions/table.php with the following code (or save it as table2.php for testing):
%%(php)<?php
Constants
define('EMPTY_CELL', '#');
define('METACHARS', '- *,|[]{}/\\'); # meta characters used in REs must be escaped if used as delimiter

define('MISSING_PARAM', 'Error: required parameter %s is missing'); # i18n

The following defaults can be overridden with a parameter
$lDelim = ';'; # default delimiter
$lCaption = ;
$lCols = 1;
$lBorder = 1;
$lPadding = 3;
$lSpacing = 1;
$lStyle =
; # styling for table tag
$lClass = ;
$lSummary =
;

if (is_array($vars))
{
check presence of required parameters
if (!isset($vars['cells']))
{
$out = '<em class="error">';
$out .= '
';
$out .= sprintf(' '.MISSING_PARAM, '<tt>cells</tt>');
$out .= '</em>';
}
all OK, let's go ahead
else
{
get delimiter first (escape chars that are special chars in REs)
if (isset($vars['delimiter']))
{
}
elseif (isset($vars['delim']))
{
if (1 strlen($vars['delim'])) $lDelim = addcslashes($vars['delim'],METACHARS);
}
process other parameters
foreach ($vars as $param => $value)
{
sanitize input
$value = trim($value);
while ($value != strip_tags($value)) $value = strip_tags($value);
parse input
switch ($param)
{
case 'caption':
$lCaption = $value;
break;
case 'columns':
case 'cols':
if (preg_match('/[0-9] /',$value)) $lCols = $value;
break;
case 'border':
if (preg_match('/[0-9] /',$value)) $lBorder = $value;
break;
case 'cellpadding':
if (preg_match('/[0-9] /',$value)) $lPadding = $value;
break;
case 'cellspacing':
if (preg_match('/[0-9] /',$value)) $lSpacing = $value;
break;
case 'style':
$lStyle = $value;
break;
case 'class':
$lClass = $value;
break;
case 'summary':
$lSummary = $value;
break;
case 'cells':
get rid of any surrounding delimiters
if (preg_match('/(.*?)['.$lDelim.'] $/s',$value,$matches)) $value = $matches[1];
if (preg_match('/^['.$lDelim.'] (.*)/s',$value,$matches)) $value = $matches[1];
split contents into array
$cells = split($lDelim, $value);
break;
}
}

start table
$out = '<table cellpadding="'.$lPadding.'" cellspacing="'.$lSpacing.'" border="'.$lBorder.'"';
if ( != $lStyle) $out .= ' style="'.$lStyle.'"';
if ( != $lClass) $out .= ' class="'.$lClass.'"';
if ( != $lSummary) $out .= ' summary="'.$lSummary.'"';
$out .= ">\n";
optional caption
if ( != $lCaption) $out .= '<caption>'.$lCaption.'</caption>'."\n";

$iCell = 0;
foreach ($cells as $content)
{
discard surrounding whitespace
$content = trim($content);
start row
if (($iCell % $lCols)
0) $out .= "<tr>\n";
process cells
There are 8 comments on this page. [Show comments]