Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupQuotedBlockRule.php
12241 views
<?php12abstract class PhutilRemarkupQuotedBlockRule3extends PhutilRemarkupBlockRule {45final public function supportsChildBlocks() {6return true;7}89public function willMarkupChildBlocks() {10$engine = $this->getEngine();1112$depth = $engine->getQuoteDepth();13$depth = $depth + 1;14$engine->setQuoteDepth($depth);15}1617public function didMarkupChildBlocks() {18$engine = $this->getEngine();1920$depth = $engine->getQuoteDepth();21$depth = $depth - 1;22$engine->setQuoteDepth($depth);23}2425final protected function normalizeQuotedBody($text) {26$text = phutil_split_lines($text, true);27foreach ($text as $key => $line) {28$text[$key] = substr($line, 1);29}3031// If every line in the block is empty or begins with at least one leading32// space, strip the initial space off each line. When we quote text, we33// normally add "> " (with a space) to the beginning of each line, which34// can disrupt some other rules. If the block appears to have this space35// in front of each line, remove it.3637$strip_space = true;38foreach ($text as $key => $line) {39$len = strlen($line);4041if (!$len) {42// We'll still strip spaces if there are some completely empty43// lines, they may have just had trailing whitespace trimmed.44continue;45}4647// If this line is part of a nested quote block, just ignore it when48// realigning this quote block. It's either an author attribution49// line with ">>!", or we'll deal with it in a subrule when processing50// the nested quote block.51if ($line[0] == '>') {52continue;53}5455if ($line[0] == ' ' || $line[0] == "\n") {56continue;57}5859// The first character of this line is something other than a space, so60// we can't strip spaces.61$strip_space = false;62break;63}6465if ($strip_space) {66foreach ($text as $key => $line) {67$len = strlen($line);68if (!$len) {69continue;70}7172if ($line[0] !== ' ') {73continue;74}7576$text[$key] = substr($line, 1);77}78}7980// Strip leading empty lines.81foreach ($text as $key => $line) {82if (!strlen(trim($line))) {83unset($text[$key]);84} else {85break;86}87}8889return implode('', $text);90}9192final protected function getQuotedText($text) {93$text = rtrim($text, "\n");9495$no_whitespace = array(96// For readability, we render nested quotes as ">> quack",97// not "> > quack".98'>' => true,99100// If the line is empty except for a newline, do not add an101// unnecessary dangling space.102"\n" => true,103);104105$text = phutil_split_lines($text, true);106foreach ($text as $key => $line) {107$c = null;108if (isset($line[0])) {109$c = $line[0];110} else {111$c = null;112}113114if (isset($no_whitespace[$c])) {115$text[$key] = '>'.$line;116} else {117$text[$key] = '> '.$line;118}119}120$text = implode('', $text);121122return $text;123}124125}126127128