^ Name ^ Deposited ^ Balance ^
| John | 25 | 500 |
| Mary | 40 | 680 |
| Lex | 10 | 140 |
| TOTAL| =AVG | =SUM |
*
* @license GPL-2.0 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
* @author Sherri W. (http://syntaxseed.com)
*/
if (!defined('DOKU_INC')) {
define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
}
if (!defined('DOKU_PLUGIN')) {
define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
}
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_avmathtable extends DokuWiki_Syntax_Plugin
{
private array $infoTable = [];
/**
* What kind of syntax are we?
*/
public function getType()
{
return 'substition';
}
/**
* Where to sort in?
*/
public function getSort()
{
return 999;
}
/**
* Connect pattern to lexer
*/
public function connectTo($mode)
{
$this->Lexer->addEntryPattern('\
');
// var_dump($state, $tables);
// echo('');
[$match, $info] = $tables;
$this->infoTable = $info;
switch ($state) {
case DOKU_LEXER_ENTER:
//$renderer->doc .= "');
// var_dump($calls);
// echo('');
// 2) Convert to array
$table = [];
$row = [];
$cell = null;
$infoTable = []; // Keep track of things like if it's a header cell or a regular cell, alignment, etc.
$infoRow = [];
$infoCell = ['type' => 'plain', 'alignment' => 'left'];
foreach ($calls as $call) {
[$cmd, $data] = $call;
switch ($cmd) {
case 'tableheader_open':
$cell = '';
$infoCell = ['type' => 'header', 'alignment' => (is_null($data[1]) ? 'left' : $data[1])];
break;
case 'tablecell_open':
$cell = '';
$infoCell = ['type' => 'plain', 'alignment' => (is_null($data[1]) ? 'left' : $data[1])];
break;
case 'cdata':
if ($cell !== null) {
$cell .= $data[0];
}
break;
case 'tableheader_close':
case 'tablecell_close':
$row[] = trim($cell);
$infoRow[] = $infoCell;
$cell = null; // Reset
$infoCell = ['type' => 'plain', 'alignment' => 'left']; // Reset
break;
case 'tablerow_close':
$table[] = $row;
$infoTable[] = $infoRow;
$row = [];
$infoRow = [];
break;
}
}
return [$table, $infoTable];
}
private function renderArrayIntoTable(array $table): string
{
$output = '';
$columnData = [];
$rowNum = 1;
// Create each row:
foreach ($table as $i => $row) {
// Create each cell:
foreach ($row as $j => $cell) {
// Initialize info about this column.
if ($rowNum == 1) {
$columnData[$j] = ['sum' => 0, 'count' => 0, 'precision' => 0];
}
// Open up the cell wiki syntax.
if ($this->infoTable[$i][$j]['type'] == 'header') {
$output .= "^ ";
} else {
$output .= "| ";
}
if ($this->infoTable[$i][$j]['alignment'] == 'right' || $this->infoTable[$i][$j]['alignment'] == 'center') {
$output .= " ";
}
// Gather info about the numbers in this cell.
if (is_numeric($cell)) {
if ((int)$cell == $cell) {
$columnData[$j]['sum'] += intval($cell);
$columnData[$j]['count'] += 1;
} elseif ((float)$cell == $cell) {
$columnData[$j]['sum'] += floatval($cell);
$columnData[$j]['count'] += 1;
$columnData[$j]['precision'] = max($columnData[$j]['precision'], $this->countDecimalPlaces((float)$cell));
}
}
// Insert the cell value. TODO : Handle special math features.
$output .= $this->insertCellData($cell, $columnData, $rowNum, $j);
// Close up the cell wiki syntax.
if ($this->infoTable[$i][$j]['alignment'] == 'left' || $this->infoTable[$i][$j]['alignment'] == 'center') {
$output .= " ";
}
if ($this->infoTable[$i][$j]['type'] == 'header') {
$output .= " ^";
} else {
$output .= " |";
}
}
$output .= "\n"; // End of a row.
$rowNum++;
}
//$dump = var_export($table, true);
return $output;
}
/**
* Put the value back in the cell. Substitute math where applicable.
*/
private function insertCellData(mixed $cell, array $columnData, int $rowNum, int $colNum)
{
// echo('');
// var_dump($columnData);
// echo('');
switch (trim($cell)) {
case '=SUM':
return '' . $columnData[$colNum]['sum'] . '';
break;
case '=CNT':
return '' . $columnData[$colNum]['count'] . '';
break;
case '=AVG':
return '' . round(($columnData[$colNum]['sum'] / $columnData[$colNum]['count']), $columnData[$colNum]['precision']+1) . '';
break;
default:
return $cell;
}
}
private function countDecimalPlaces(float $num): int
{
$fNumber = floatval($num);
for ($iDecimals = 0; $fNumber != round($fNumber, $iDecimals); $iDecimals++);
return $iDecimals;
}
} // End class