protected function handleTag_table()
{
if ($this->parser->isStartTag) {
// check if upcoming table can be converted
if ($this->keepHTML) {
if (preg_match($this->tableLookaheadHeader, $this->parser->html, $matches)) {
// header seems good, now check body
// get align & number of cols
preg_match_all('#<th(?:\\s+align=("|\')(left|right|center)\\1)?\\s*>#si', $matches[0], $cols);
$regEx = '';
$i = 1;
$aligns = array();
foreach ($cols[2] as $align) {
$align = strtolower($align);
array_push($aligns, $align);
if (empty($align)) {
$align = 'left';
// default value
}
$td = '\\s+align=("|\')' . $align . '\\' . $i;
$i++;
if ($align == 'left') {
// look for empty align or left
$td = '(?:' . $td . ')?';
}
$td = '<td' . $td . '\\s*>';
$regEx .= $td . $this->tdSubstitute;
}
$regEx = sprintf($this->tableLookaheadBody, $regEx);
if (preg_match($regEx, $this->parser->html, $matches, null, strlen($matches[0]))) {
// this is a markdownable table tag!
$this->table = array('rows' => array(), 'col_widths' => array(), 'aligns' => $aligns);
$this->row = 0;
} else {
// non markdownable table
$this->handleTagToText();
}
} else {
// non markdownable table
$this->handleTagToText();
}
} else {
$this->table = array('rows' => array(), 'col_widths' => array(), 'aligns' => array());
$this->row = 0;
}
} else {
// finally build the table in Markdown Extra syntax
$separator = array();
if (!isset($this->table['aligns'])) {
$this->table['aligns'] = array();
}
// seperator with correct align identifiers
foreach ($this->table['aligns'] as $col => $align) {
if (!$this->keepHTML && !isset($this->table['col_widths'][$col])) {
break;
}
$left = ' ';
$right = ' ';
switch ($align) {
case 'left':
$left = ':';
break;
case 'center':
$right = ':';
$left = ':';
case 'right':
$right = ':';
break;
}
array_push($separator, $left . str_repeat('-', $this->table['col_widths'][$col]) . $right);
}
$separator = '|' . implode('|', $separator) . '|';
$rows = array();
// add padding
array_walk_recursive($this->table['rows'], array(&$this, 'alignTdContent'));
$header = array_shift($this->table['rows']);
array_push($rows, '| ' . implode(' | ', $header) . ' |');
array_push($rows, $separator);
foreach ($this->table['rows'] as $row) {
array_push($rows, '| ' . implode(' | ', $row) . ' |');
}
$this->out(implode("\n" . $this->indent, $rows));
$this->table = array();
$this->setLineBreaks(2);
}
}