Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupInterpreterBlockRule.php
12242 views
1
<?php
2
3
final class PhutilRemarkupInterpreterBlockRule extends PhutilRemarkupBlockRule {
4
5
const START_BLOCK_PATTERN = '/^([\w]+)\s*(?:\(([^)]+)\)\s*)?{{{/';
6
const END_BLOCK_PATTERN = '/}}}\s*$/';
7
8
public function getMatchingLineCount(array $lines, $cursor) {
9
$num_lines = 0;
10
11
if (preg_match(self::START_BLOCK_PATTERN, $lines[$cursor])) {
12
$num_lines++;
13
14
while (isset($lines[$cursor])) {
15
if (preg_match(self::END_BLOCK_PATTERN, $lines[$cursor])) {
16
break;
17
}
18
$num_lines++;
19
$cursor++;
20
}
21
}
22
23
return $num_lines;
24
}
25
26
public function markupText($text, $children) {
27
$lines = explode("\n", $text);
28
$first_key = head_key($lines);
29
$last_key = last_key($lines);
30
while (trim($lines[$last_key]) === '') {
31
unset($lines[$last_key]);
32
$last_key = last_key($lines);
33
}
34
$matches = null;
35
36
preg_match(self::START_BLOCK_PATTERN, head($lines), $matches);
37
38
$argv = array();
39
if (isset($matches[2])) {
40
$argv = id(new PhutilSimpleOptions())->parse($matches[2]);
41
}
42
43
$interpreters = id(new PhutilClassMapQuery())
44
->setAncestorClass('PhutilRemarkupBlockInterpreter')
45
->execute();
46
47
foreach ($interpreters as $interpreter) {
48
$interpreter->setEngine($this->getEngine());
49
}
50
51
$lines[$first_key] = preg_replace(
52
self::START_BLOCK_PATTERN,
53
'',
54
$lines[$first_key]);
55
$lines[$last_key] = preg_replace(
56
self::END_BLOCK_PATTERN,
57
'',
58
$lines[$last_key]);
59
60
if (trim($lines[$first_key]) === '') {
61
unset($lines[$first_key]);
62
}
63
if (trim($lines[$last_key]) === '') {
64
unset($lines[$last_key]);
65
}
66
67
$content = implode("\n", $lines);
68
69
$interpreters = mpull($interpreters, null, 'getInterpreterName');
70
71
if (isset($interpreters[$matches[1]])) {
72
return $interpreters[$matches[1]]->markupContent($content, $argv);
73
}
74
75
$message = pht('No interpreter found: %s', $matches[1]);
76
77
if ($this->getEngine()->isTextMode()) {
78
return '('.$message.')';
79
}
80
81
return phutil_tag(
82
'div',
83
array(
84
'class' => 'remarkup-interpreter-error',
85
),
86
$message);
87
}
88
89
}
90
91