Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupInterpreterBlockRule.php
12242 views
<?php12final class PhutilRemarkupInterpreterBlockRule extends PhutilRemarkupBlockRule {34const START_BLOCK_PATTERN = '/^([\w]+)\s*(?:\(([^)]+)\)\s*)?{{{/';5const END_BLOCK_PATTERN = '/}}}\s*$/';67public function getMatchingLineCount(array $lines, $cursor) {8$num_lines = 0;910if (preg_match(self::START_BLOCK_PATTERN, $lines[$cursor])) {11$num_lines++;1213while (isset($lines[$cursor])) {14if (preg_match(self::END_BLOCK_PATTERN, $lines[$cursor])) {15break;16}17$num_lines++;18$cursor++;19}20}2122return $num_lines;23}2425public function markupText($text, $children) {26$lines = explode("\n", $text);27$first_key = head_key($lines);28$last_key = last_key($lines);29while (trim($lines[$last_key]) === '') {30unset($lines[$last_key]);31$last_key = last_key($lines);32}33$matches = null;3435preg_match(self::START_BLOCK_PATTERN, head($lines), $matches);3637$argv = array();38if (isset($matches[2])) {39$argv = id(new PhutilSimpleOptions())->parse($matches[2]);40}4142$interpreters = id(new PhutilClassMapQuery())43->setAncestorClass('PhutilRemarkupBlockInterpreter')44->execute();4546foreach ($interpreters as $interpreter) {47$interpreter->setEngine($this->getEngine());48}4950$lines[$first_key] = preg_replace(51self::START_BLOCK_PATTERN,52'',53$lines[$first_key]);54$lines[$last_key] = preg_replace(55self::END_BLOCK_PATTERN,56'',57$lines[$last_key]);5859if (trim($lines[$first_key]) === '') {60unset($lines[$first_key]);61}62if (trim($lines[$last_key]) === '') {63unset($lines[$last_key]);64}6566$content = implode("\n", $lines);6768$interpreters = mpull($interpreters, null, 'getInterpreterName');6970if (isset($interpreters[$matches[1]])) {71return $interpreters[$matches[1]]->markupContent($content, $argv);72}7374$message = pht('No interpreter found: %s', $matches[1]);7576if ($this->getEngine()->isTextMode()) {77return '('.$message.')';78}7980return phutil_tag(81'div',82array(83'class' => 'remarkup-interpreter-error',84),85$message);86}8788}899091