? // 0 = unknown // 1 = inside (parsing cells) // 2 = false (no thead, only tbody) // 3 = true (there is a thead) $theadstatus = 0; // We always generate an Id for the table. // This is convenient for tables of class "sortable". // If user provides an Id, the generated Id will be overwritten below. $table->setAttr("id", GenerateId("MediawikiTable")); if (substr($lines[0], 0, 2) == "{|") { // Start of table $lines[0] = substr($lines[0], 2); } if (($lines[0][0] != '|') and ($lines[0][0] != '!')) { $line = array_shift($lines); $attrs = parse_attributes($line); foreach ($attrs as $key => $value) { if (in_array($key, array("id", "class", "title", "style", "bgcolor", "frame", "rules", "border", "cellspacing", "cellpadding", "summary", "align", "width")) ) { $table->setAttr($key, $value); } } } if (count($lines) == 1) { // empty table, we only have closing "|}" line return HTML::raw(''); } foreach ($lines as $line) { if (substr($line, 0, 2) == "|}") { // End of table continue; } if (substr($line, 0, 2) == "|-") { if (isset($row)) { if (isset($cell)) { if (isset($content)) { if (is_numeric(trim($content))) { $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content))); } else { $cell->pushContent(TransformText(trim($content), $markup, $basepage)); } unset($content); } $row->pushContent($cell); unset($cell); } if (!empty($row->_content)) { if ($theadstatus == 1) { // inside $theadstatus = 3; // true $thead->pushContent($row); } else { $tbody->pushContent($row); } } } $row = HTML::tr(); $attrs = parse_attributes(substr($line, 2)); foreach ($attrs as $key => $value) { if (in_array($key, array("id", "class", "title", "style", "bgcolor", "align", "valign")) ) { $row->setAttr($key, $value); } } continue; } // Table summary if (substr($line, 0, 2) == "|=") { $line = substr($line, 2); $table->setAttr("summary", trim($line)); } // Table caption if (substr($line, 0, 2) == "|+") { $line = substr($line, 2); $pospipe = strpos($line, "|"); $posbracket = strpos($line, "["); if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) { $attrs = parse_attributes(substr($line, 0, $pospipe)); foreach ($attrs as $key => $value) { if (in_array($key, array("id", "class", "title", "style", "align", "lang")) ) { $caption->setAttr($key, $value); } } $line = substr($line, $pospipe + 1); } $caption->setContent(TransformInline(trim($line))); } if (((substr($line, 0, 1) == "|") or (substr($line, 0, 1) == "!")) and isset($row)) { if (isset($cell)) { if (isset ($content)) { if (is_numeric(trim($content))) { $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content))); } else { $cell->pushContent(TransformText(trim($content), $markup, $basepage)); } unset($content); } $row->pushContent($cell); } if (substr($line, 0, 1) == "!") { if ($theadstatus == 0) { // unknown $theadstatus = 1; // inside } $cell = HTML::th(); // Header } else { if ($theadstatus == 1) { // inside $theadstatus = 2; // false } $cell = HTML::td(); } $line = substr($line, 1); // If there is a "|" in the line, the start of line // (before the "|") is made of attributes. // The end of the line (after the "|") is the cell content // This is not true if the pipe is inside [], {{}} or {{{}}} // | [foo|bar] // The following cases must work: // | foo // | [foo|bar] // | class="xxx" | foo // | class="xxx" | [foo|bar] // | {{tmpl|arg=val}} // | {{image.png|alt}} // | {{{ xxx | yyy }}} $pospipe = strpos($line, "|"); $posbracket = strpos($line, "["); $poscurly = strpos($line, "{"); if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe)) && (($poscurly === false) || ($poscurly > $pospipe))) { $attrs = parse_attributes(substr($line, 0, $pospipe)); foreach ($attrs as $key => $value) { if (in_array($key, array("id", "class", "title", "style", "scope", "colspan", "rowspan", "width", "height", "bgcolor", "align", "valign")) ) { $cell->setAttr($key, $value); } } $line = substr($line, $pospipe + 1); if (is_numeric(trim($line))) { $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($line))); } else { $cell->pushContent(TransformText(trim($line), $markup, $basepage)); } continue; } } if (isset($row) and isset($cell)) { $line = str_replace("?\>", "?>", $line); $line = str_replace("\~", "~", $line); if (empty($content)) $content = ''; $content .= $line . "\n"; } } if (isset($row)) { if (isset($cell)) { if (isset($content)) { if (is_numeric(trim($content))) { $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content))); } else { $cell->pushContent(TransformText(trim($content), $markup, $basepage)); } } $row->pushContent($cell); } // If user put and extra "|-" without cells just before "|}" // we ignore it to get valid XHTML code if (!empty($row->_content)) { $tbody->pushContent($row); } } if (!empty($caption->_content)) { $table->pushContent($caption); } if (!empty($thead->_content)) { $table->pushContent($thead); } if (!empty($tbody->_content)) { $table->pushContent($tbody); } if (!empty($table->_content)) { return $table; } else { return HTML::raw(''); } } } // Local Variables: // mode: php // tab-width: 8 // c-basic-offset: 4 // c-hanging-comment-ender-p: nil // indent-tabs-mode: nil // End: