Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupSimpleTableBlockRule.php
12241 views
<?php12final class PhutilRemarkupSimpleTableBlockRule extends PhutilRemarkupBlockRule {34public function getMatchingLineCount(array $lines, $cursor) {5$num_lines = 0;6while (isset($lines[$cursor])) {7if (preg_match('/^(\s*\|.*+\n?)+$/', $lines[$cursor])) {8$num_lines++;9$cursor++;10} else {11break;12}13}1415return $num_lines;16}1718public function markupText($text, $children) {19$matches = array();2021$rows = array();22foreach (explode("\n", $text) as $line) {23// Ignore ending delimiters.24$line = rtrim($line, '|');2526// NOTE: The complexity in this regular expression allows us to match27// a table like "| a | [[ href | b ]] | c |".2829preg_match_all(30'/\|'.31'('.32'(?:'.33'(?:\\[\\[.*?\\]\\])'. // [[ ... | ... ]], a link34'|'.35'(?:[^|[]+)'. // Anything but "|" or "[".36'|'.37'(?:\\[[^\\|[])'. // "[" followed by anything but "[" or "|"38')*'.39')/', $line, $matches);4041$any_header = false;42$any_content = false;4344$cells = array();45foreach ($matches[1] as $cell) {46$cell = trim($cell);4748// If this row only has empty cells and "--" cells, and it has at49// least one "--" cell, it's marking the rows above as <th> cells50// instead of <td> cells.5152// If it has other types of cells, it's always a content row.5354// If it has only empty cells, it's an empty row.5556if (strlen($cell)) {57if (preg_match('/^--+\z/', $cell)) {58$any_header = true;59} else {60$any_content = true;61}62}6364$cells[] = array('type' => 'td', 'content' => $this->applyRules($cell));65}6667$is_header = ($any_header && !$any_content);6869if (!$is_header) {70$rows[] = array('type' => 'tr', 'content' => $cells);71} else if ($rows) {72// Mark previous row with headings.73foreach ($cells as $i => $cell) {74if ($cell['content']) {75$last_key = last_key($rows);76if (!isset($rows[$last_key]['content'][$i])) {77// If this row has more cells than the previous row, there may78// not be a cell above this one to turn into a <th />.79continue;80}8182$rows[$last_key]['content'][$i]['type'] = 'th';83}84}85}86}8788if (!$rows) {89return $this->applyRules($text);90}9192return $this->renderRemarkupTable($rows);93}9495}969798