Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupBlockRule.php
12241 views
<?php12abstract class PhutilRemarkupBlockRule extends Phobject {34private $engine;5private $rules = array();67/**8* Determine the order in which blocks execute. Blocks with smaller priority9* numbers execute sooner than blocks with larger priority numbers. The10* default priority for blocks is `500`.11*12* Priorities are used to disambiguate syntax which can match multiple13* patterns. For example, ` - Lorem ipsum...` may be a code block or a14* list.15*16* @return int Priority at which this block should execute.17*/18public function getPriority() {19return 500;20}2122final public function getPriorityVector() {23return id(new PhutilSortVector())24->addInt($this->getPriority())25->addString(get_class($this));26}2728abstract public function markupText($text, $children);2930/**31* This will get an array of unparsed lines and return the number of lines32* from the first array value that it can parse.33*34* @param array $lines35* @param int $cursor36*37* @return int38*/39abstract public function getMatchingLineCount(array $lines, $cursor);4041protected function didMarkupText() {42return;43}4445public function willMarkupChildBlocks() {46return;47}4849public function didMarkupChildBlocks() {50return;51}5253final public function setEngine(PhutilRemarkupEngine $engine) {54$this->engine = $engine;55$this->updateRules();56return $this;57}5859final protected function getEngine() {60return $this->engine;61}6263public function setMarkupRules(array $rules) {64assert_instances_of($rules, 'PhutilRemarkupRule');65$this->rules = $rules;66$this->updateRules();67return $this;68}6970private function updateRules() {71$engine = $this->getEngine();72if ($engine) {73$this->rules = msort($this->rules, 'getPriority');74foreach ($this->rules as $rule) {75$rule->setEngine($engine);76}77}78return $this;79}8081final public function getMarkupRules() {82return $this->rules;83}8485final public function postprocess() {86$this->didMarkupText();87}8889final protected function applyRules($text) {90foreach ($this->getMarkupRules() as $rule) {91$text = $rule->apply($text);92}93return $text;94}9596public function supportsChildBlocks() {97return false;98}99100public function extractChildText($text) {101throw new PhutilMethodNotImplementedException();102}103104protected function renderRemarkupTable(array $out_rows) {105assert_instances_of($out_rows, 'array');106107if ($this->getEngine()->isTextMode()) {108$lengths = array();109foreach ($out_rows as $r => $row) {110foreach ($row['content'] as $c => $cell) {111$text = $this->getEngine()->restoreText($cell['content']);112$lengths[$c][$r] = phutil_utf8_strlen($text);113}114}115$max_lengths = array_map('max', $lengths);116117$out = array();118foreach ($out_rows as $r => $row) {119$headings = false;120foreach ($row['content'] as $c => $cell) {121$length = $max_lengths[$c] - $lengths[$c][$r];122$out[] = '| '.$cell['content'].str_repeat(' ', $length).' ';123if ($cell['type'] == 'th') {124$headings = true;125}126}127$out[] = "|\n";128129if ($headings) {130foreach ($row['content'] as $c => $cell) {131$char = ($cell['type'] == 'th' ? '-' : ' ');132$out[] = '| '.str_repeat($char, $max_lengths[$c]).' ';133}134$out[] = "|\n";135}136}137138return rtrim(implode('', $out), "\n");139}140141if ($this->getEngine()->isHTMLMailMode()) {142$table_attributes = array(143'style' => 'border-collapse: separate;144border-spacing: 1px;145background: #d3d3d3;146margin: 12px 0;',147);148$cell_attributes = array(149'style' => 'background: #ffffff;150padding: 3px 6px;',151);152} else {153$table_attributes = array(154'class' => 'remarkup-table',155);156$cell_attributes = array();157}158159$out = array();160$out[] = "\n";161foreach ($out_rows as $row) {162$cells = array();163foreach ($row['content'] as $cell) {164$cells[] = phutil_tag(165$cell['type'],166$cell_attributes,167$cell['content']);168}169$out[] = phutil_tag($row['type'], array(), $cells);170$out[] = "\n";171}172173$table = phutil_tag('table', $table_attributes, $out);174return phutil_tag_div('remarkup-table-wrap', $table);175}176177}178179180