|
From: Stefan <son...@ba...> - 2010-06-27 17:05:10
|
<?php // -*-php-*-
rcs_id('$Id: RichTable.php,v 2.2 2010/06/24 17:44:24 Stefan Schorn Exp $');
/**
Richtable - Tabellen plugin
*/
class WikiPlugin_RichTable
extends WikiPlugin
{
function getName() {
return _("RichTable");
}
function getDescription() {
return _("Layout tables using a very rich markup style.");
}
function getDefaultArguments() {
return array();
}
function getVersion() {
return preg_replace("/[Revision: $]/", '',
"\$Revision: 1.7 $");
}
function run($dbi, $argstr, &$request, $basepage) {
global $WikiTheme;
include_once("lib/BlockParser.php");
// RichTablePlugin markup is new.
$markup = 2.0;
$lines = preg_split('/\n/', $argstr);
$table = HTML::table();
$atlas = 0;
$atlasdl = 0;
if ($lines[0][0] == '*') {
$line = substr(array_shift($lines),1);
$attrs = $this->_parse_attr($line);
foreach ($attrs as $key => $value) {
if (in_array ($key, array("id", "class", "title", "style",
"bgcolor", "frame", "rules", "border",
"cellspacing", "cellpadding",
"summary", "align", "width"))) {
if ($key == 'class') $class = $value;
$table->setAttr($key, $value);
}
}
}
$r = 0;
$c = 0;
$ownclass = 0;
$ownclasstr = 0;
$Zelle = array();
if(is_array($lines)) $lastLine = $lines[count($lines)-1];
if (!(trim($lastLine) == '-')) { $lines[] = '-';}
foreach ($lines as $line){
if (substr($line,0,1) == "-") {
$makeit = 0;
if (isset($row)) {
if (isset($cell)) {
if ($r == 1) $cmax = $c;
if (isset($class) && $class && !$ownclass) {
if ($c == $cmax) {
if ($r == 1) {
$cell->setAttr('class', $class.'-bn');
} else {
$cell->setAttr('class', $class.'-bt');
}
} else {
if ($r == 1) {
$cell->setAttr('class', $class.'-br');
} else {
$cell->setAttr('class', $class.'-brt');
}
}
}
if (isset($content)) {
// debug $cell->pushContent(TransformText('<<'.$r.'#'.$c.'>>', $markup, $basepage));
$cell->pushContent(TransformText($content, $markup, $basepage));
unset($content);
}
$row->pushContent($cell);
unset($cell);
$makeit = 1;
}
if ($makeit == 1) {
$table->pushContent($row);
unset($row);
}
}
$r++;
$row = HTML::tr();
$attrs = $this->_parse_attr(substr($line,1));
$c = 0;
$ownclasstr = 0;
foreach ($attrs as $key => $value) {
if (in_array ($key, array("id", "class", "title", "style",
"bgcolor", "align", "valign"))) {
$row->setAttr($key, $value);
if ($key == 'class' && isset($value) && $value) {
$ownclasstr = 1;
}
}
}
if (!$ownclasstr && isset($class) && $class)
$row->setAttr('class', $class);
continue;
}
if (substr($line,0,1) == "|" and isset($row)) {
if (isset($cell)) {
if ($class && !$ownclass) {
if ($r == 1) {
$cell->setAttr('class', $class.'-br');
} else {
$cell->setAttr('class', $class.'-brt');
}
}
if (isset ($content)) {
//debug $cell->pushContent(TransformText('<<'.$r.'#'.$c.'>>', $markup, $basepage));
$cell->pushContent(TransformText($content, $markup, $basepage));
unset($content);
}
$row->pushContent($cell);
}
$keyZold = $c;
$go = 1;
if (isset($Zelle[$r])) {
foreach ($Zelle[$r] as $keyZ => $valueZ) {
$diff = $keyZ - $keyZold;
$keyZold = $keyZ;
if ($diff == 1 && $go) {
$c = $c + $valueZ;
unset($Zelle[$r][$keyZ]);
} else $go = 0;
}
}
$c++;
$cell = HTML::td();
$line = substr($line, 1);
if ($line[0] == "*" ) {
$attrs = $this->_parse_attr(substr($line,1));
$colsp = 0;
$rowsp = 0;
$ownclass = 0;
foreach ($attrs as $key => $value) {
if (in_array ($key, array("id", "class", "title", "style",
"colspan", "rowspan", "width", "height",
"bgcolor", "align", "valign"))) {
if (!($atlas && $key == 'class'))
$cell->setAttr($key, $value);
if ($key == 'colspan' && is_numeric($value)) {
$colsp = $value;
}
if ($key == 'rowspan' && is_numeric($value)) {
$rowsp = $value;
}
if ($key == 'class' && isset($value) && $value) {
$ownclass = 1;
}
}
}
if (is_numeric($colsp)) {
for ($x = 1; $x < $colsp; $x++) {
$c = $c + 1;
}
$colsp = 0;
}
if (is_numeric($rowsp)) {
$endrow = $rowsp + $r;
for ($x = ($r+1); $x < $endrow; $x++) {
$Zelle[$x][$c] = 1;
}
$rowsp = 0;
}
continue;
}
}
if (isset($row) and isset($cell)) {
$line = str_replace("?\>", "?>", $line);
$line = str_replace("\~", "~", $line);
if (empty($content)) $content = '';
$content .= $line . "\n";
}
}
// $makit - no more empty rows
$makeit = 0;
if (isset($row)) {
if (isset($cell)) {
if (isset($content))
$cell->pushContent(TransformText($content));
$makeit = 1;
$row->pushContent($cell);
}
if ($makeit == 1) {
$table->pushContent($row);
unset($row);
}
}
return $table;
}
function _parse_attr($line) {
$attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
$options = array();
foreach ($attr_chunks as $attr_pair) {
if (empty($attr_pair)) continue;
$key_val = preg_split("/\s*=\s*/", $attr_pair);
if (!empty($key_val[1]))
$options[trim($key_val[0])] = trim($key_val[1]);
}
return $options;
}
}
// End:
?> |