Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupLiteralBlockRule.php
12242 views
<?php12final class PhutilRemarkupLiteralBlockRule extends PhutilRemarkupBlockRule {34public function getPriority() {5return 450;6}78public function getMatchingLineCount(array $lines, $cursor) {9// NOTE: We're consuming all continguous blocks of %%% literals, so this:10//11// %%%a%%%12// %%%b%%%13//14// ...is equivalent to:15//16// %%%a17// b%%%18//19// If they are separated by a blank newline, they are parsed as two20// different blocks. This more clearly represents the original text in the21// output text and assists automated escaping of blocks coming into the22// system.2324$start_pattern = '(^\s*%%%)';25$end_pattern = '(%%%\s*$)';26$trivial_pattern = '(^\s*%%%\s*$)';2728if (!preg_match($start_pattern, $lines[$cursor])) {29return 0;30}3132$start_cursor = $cursor;3334$found_empty = false;35$block_start = null;36while (true) {37if (!isset($lines[$cursor])) {38break;39}4041$line = $lines[$cursor];4243if ($block_start === null) {44$is_start = preg_match($start_pattern, $line);4546// If we've matched a block and then consumed one or more empty lines47// after it, stop merging more blocks into the match.48if ($found_empty) {49break;50}5152if ($is_start) {53$block_start = $cursor;54}55}5657if ($block_start !== null) {58$is_end = preg_match($end_pattern, $line);5960// If a line contains only "%%%", it will match both the start and61// end patterns, but it only counts as a block start.62if ($is_end && ($cursor === $block_start)) {63$is_trivial = preg_match($trivial_pattern, $line);64if ($is_trivial) {65$is_end = false;66}67}6869if ($is_end) {70$block_start = null;71$cursor++;72continue;73}74}7576if ($block_start === null) {77if (strlen(trim($line))) {78break;79}80$found_empty = true;81}8283$cursor++;84}8586return ($cursor - $start_cursor);87}8889public function markupText($text, $children) {90$text = rtrim($text);91$text = phutil_split_lines($text, $retain_endings = true);92foreach ($text as $key => $line) {93$line = preg_replace('/^\s*%%%/', '', $line);94$line = preg_replace('/%%%(\s*)\z/', '\1', $line);95$text[$key] = $line;96}9798if ($this->getEngine()->isTextMode()) {99return implode('', $text);100}101102return phutil_tag(103'p',104array(105'class' => 'remarkup-literal',106),107phutil_implode_html(phutil_tag('br', array()), $text));108}109110}111112113