Differential D25234 Diff 813 src/infrastructure/markup/blockrule/PhutilRemarkupInterpreterBlockRule.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/markup/blockrule/PhutilRemarkupInterpreterBlockRule.php
<?php | <?php | ||||
final class PhutilRemarkupInterpreterBlockRule extends PhutilRemarkupBlockRule { | final class PhutilRemarkupInterpreterBlockRule extends PhutilRemarkupBlockRule { | ||||
/** | |||||
* This regex tries to find something like: | |||||
* interpreterName {{{ stuff }}} | |||||
* And, also it tries to find this alternative: | |||||
* interpreterName (options) {{{ stuff }}} | |||||
* If you are asking, yep! You have found the kernel of cowsay and figlet! | |||||
*/ | |||||
const START_BLOCK_PATTERN = '/^([\w]+)\s*(?:\(([^)]+)\)\s*)?{{{/'; | const START_BLOCK_PATTERN = '/^([\w]+)\s*(?:\(([^)]+)\)\s*)?{{{/'; | ||||
const END_BLOCK_PATTERN = '/}}}\s*$/'; | const END_BLOCK_PATTERN = '/}}}\s*$/'; | ||||
public function getMatchingLineCount(array $lines, $cursor) { | public function getMatchingLineCount(array $lines, $cursor) { | ||||
$num_lines = 0; | $num_lines = 0; | ||||
if (preg_match(self::START_BLOCK_PATTERN, $lines[$cursor])) { | if (preg_match(self::START_BLOCK_PATTERN, $lines[$cursor])) { | ||||
$num_lines++; | $num_lines++; | ||||
Show All 22 Lines | public function markupText($text, $children) { | ||||
preg_match(self::START_BLOCK_PATTERN, head($lines), $matches); | preg_match(self::START_BLOCK_PATTERN, head($lines), $matches); | ||||
$argv = array(); | $argv = array(); | ||||
if (isset($matches[2])) { | if (isset($matches[2])) { | ||||
$argv = id(new PhutilSimpleOptions())->parse($matches[2]); | $argv = id(new PhutilSimpleOptions())->parse($matches[2]); | ||||
} | } | ||||
$interpreters = id(new PhutilClassMapQuery()) | $interpreters = id(new PhutilClassMapQuery()) | ||||
->setAncestorClass('PhutilRemarkupBlockInterpreter') | ->setAncestorClass('PhutilRemarkupBlockInterpreter') | ||||
->execute(); | ->execute(); | ||||
valerio.bozzolan: Thanks to your patch I discovered this `$interpreters` variable here that is apparently… | |||||
foreach ($interpreters as $interpreter) { | foreach ($interpreters as $interpreter) { | ||||
$interpreter->setEngine($this->getEngine()); | $interpreter->setEngine($this->getEngine()); | ||||
} | } | ||||
$lines[$first_key] = preg_replace( | $lines[$first_key] = preg_replace( | ||||
self::START_BLOCK_PATTERN, | self::START_BLOCK_PATTERN, | ||||
'', | '', | ||||
Show All 15 Lines | public function markupText($text, $children) { | ||||
$interpreters = mpull($interpreters, null, 'getInterpreterName'); | $interpreters = mpull($interpreters, null, 'getInterpreterName'); | ||||
if (isset($interpreters[$matches[1]])) { | if (isset($interpreters[$matches[1]])) { | ||||
return $interpreters[$matches[1]]->markupContent($content, $argv); | return $interpreters[$matches[1]]->markupContent($content, $argv); | ||||
} | } | ||||
$message = pht('No interpreter found: %s', $matches[1]); | $message = pht('No interpreter found: %s', $matches[1]); | ||||
// The syntax of this thing is so recondite that, showing a message, | |||||
// it simply risks making people screaming. | |||||
// Let's just log this information, | |||||
// preferring a "don't care strategy" on rendering. | |||||
phlog($message); | |||||
aviveyUnsubmitted Not Done Inline ActionsI don't think this log is useful - it's a user error, not a program error, and the user will not see it. avivey: I don't think this log is useful - it's a user error, not a program error, and the user will… | |||||
valerio.bozzolanUnsubmitted Not Done Inline ActionsI see your point. Premising that phlog() is useful to server operators, but it seems is useful also to DarkConsole users to understand what is going on. Last word to you. I can surely remove that if you still dislike. I have not any strong opinion here 👍 valerio.bozzolan: I see your point. Premising that `phlog()` is useful to server operators, but it seems is… | |||||
aviveyUnsubmitted Not Done Inline ActionsI'd rather see this class unchanged, and the root cause of the exception handled. avivey: I'd rather see this class unchanged, and the root cause of the exception handled. | |||||
// After logging the error, just render the text as-is. | |||||
if ($this->getEngine()->isTextMode()) { | if ($this->getEngine()->isTextMode()) { | ||||
return '('.$message.')'; | return $text; | ||||
} | } | ||||
return phutil_tag( | if ($this->getEngine()->getConfig('preserve-linebreaks')) { | ||||
'div', | $text = phutil_escape_html_newlines($text); | ||||
array( | |||||
'class' => 'remarkup-interpreter-error', | |||||
), | |||||
$message); | |||||
} | } | ||||
return $text; | |||||
} | |||||
} | } |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
Thanks to your patch I discovered this $interpreters variable here that is apparently designed to support a very extensible system to support whatever weird custom interpreter. So probably we cannot just hammer cowsay and figlet in the regular expression, otherwise we block extensions, and we should preserve the good things from the person who over-engineered of this parser.