<?php if (!defined('PmWiki')) exit();

$RecipeInfo['AcmeTables']['Version'] = '2007-04-25';

## DESCRIPTION:  This recipe adds a simpler and somewhat more flexible tables directive system into PmWiki.
## Use [t] [r] [c] for table, row, and cells. No closing cell or row tags required but a closing [t] required
## All standard attributes are permitted, along with one-word shortcuts like center, left, top, middle, bottom
## Also shortcuts like color, padding, and spacing. Regular html tags can also optionally be allowed.
## For nesting a table in a cell, put 1 space before each tag in the nested table, or 2 for a third level
## Tags can be each be put on a separate line, or run together (some or all) on one line.  
## Just be very careful about the number of spaces (0, or 1-2 spaces) before every tag or you'll have disaster.
## Author: Dan Vis aka Caveman <editor àt fast döt st>, Copyright 2007.  

## LICENSE:  You can redistribute this software 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.


MarkUp('table', 'table', '/\n?([ ]{0,2})\[([trc]{1})(.*?)\]/e', 'ZAPtables("$2", "$3", strlen("$1"))');

function ZAPtables($type, $attr, $level=0) {
	global $tablememory;
	if ($attr != '') $attr = ZAPtableattrs($attr, $type);
	switch($type) {
		case "t" :
			if ($tablememory["$level-t"] != 'on') {
				$tablememory["$level-t"] = 'on';
				return "<table $attr>";
				}
			else {
				$tablememory["$level-t"] = 'off';
				$tablememory["$level-r"] = 'off';
				$tablememory["$level-c"] = 'off';
				return "</td></tr></table>";
				}
		case "r" :
			if ($tablememory["$level-r"] != 'on') {
				$tablememory["$level-r"] = 'on';
				return "<tr $attr>";
				}
			else {
				$tablememory["$level-c"] = 'off';
				return "</td></tr><tr $attr>";
				}
		case "c" :
			if ($tablememory["$level-c"] != 'on') {
				$tablememory["$level-c"] == 'on';
				return "<td $attr>";
				}
			else return "</td><td $attr>";
		default:
			return '';
		}
	}

function ZAPtableattrs($attr, $type) {
	global $debug;
	$out = '';
	$r1 = array(' center', ' left', ' right', ' top', ' middle', ' bottom', ' color', ' padding', ' spacing', ' rows', ' cols', ' image');
	$r2 = array(' align=center', ' align=left', ' align=right', ' valign=top', ' valign=middle', ' valign=bottom', ' bgcolor', ' cellpadding', 'cellspacing', ' rowspan', ' colspan', ' background');
	$attr = str_replace($r1, $r2, $attr);
	$attrs['t'] = Array('align', 'background', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'width');
	$attrs['r'] = Array('align', 'background', 'bgcolor', 'valign', 'border');
	$attrs['c'] = Array('align', 'background', 'bgcolor', 'valign', 'border', 'colspan', 'rowspan', 'nowrap', 'valign', 'width');
	$args = ParseArgs($attr);
	foreach($args as $f => $v) {
		if (is_array($v)) continue;
		if (! in_array($f, $attrs[$type])) continue;
		if (ereg('/[\'#0-9a-zA-Z]+/', $v)) continue;
		$out .= " $f='$v' ";
		}
	return $out;
	}