Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/blockrule/PhutilRemarkupBlockRule.php
12241 views
1
<?php
2
3
abstract class PhutilRemarkupBlockRule extends Phobject {
4
5
private $engine;
6
private $rules = array();
7
8
/**
9
* Determine the order in which blocks execute. Blocks with smaller priority
10
* numbers execute sooner than blocks with larger priority numbers. The
11
* default priority for blocks is `500`.
12
*
13
* Priorities are used to disambiguate syntax which can match multiple
14
* patterns. For example, ` - Lorem ipsum...` may be a code block or a
15
* list.
16
*
17
* @return int Priority at which this block should execute.
18
*/
19
public function getPriority() {
20
return 500;
21
}
22
23
final public function getPriorityVector() {
24
return id(new PhutilSortVector())
25
->addInt($this->getPriority())
26
->addString(get_class($this));
27
}
28
29
abstract public function markupText($text, $children);
30
31
/**
32
* This will get an array of unparsed lines and return the number of lines
33
* from the first array value that it can parse.
34
*
35
* @param array $lines
36
* @param int $cursor
37
*
38
* @return int
39
*/
40
abstract public function getMatchingLineCount(array $lines, $cursor);
41
42
protected function didMarkupText() {
43
return;
44
}
45
46
public function willMarkupChildBlocks() {
47
return;
48
}
49
50
public function didMarkupChildBlocks() {
51
return;
52
}
53
54
final public function setEngine(PhutilRemarkupEngine $engine) {
55
$this->engine = $engine;
56
$this->updateRules();
57
return $this;
58
}
59
60
final protected function getEngine() {
61
return $this->engine;
62
}
63
64
public function setMarkupRules(array $rules) {
65
assert_instances_of($rules, 'PhutilRemarkupRule');
66
$this->rules = $rules;
67
$this->updateRules();
68
return $this;
69
}
70
71
private function updateRules() {
72
$engine = $this->getEngine();
73
if ($engine) {
74
$this->rules = msort($this->rules, 'getPriority');
75
foreach ($this->rules as $rule) {
76
$rule->setEngine($engine);
77
}
78
}
79
return $this;
80
}
81
82
final public function getMarkupRules() {
83
return $this->rules;
84
}
85
86
final public function postprocess() {
87
$this->didMarkupText();
88
}
89
90
final protected function applyRules($text) {
91
foreach ($this->getMarkupRules() as $rule) {
92
$text = $rule->apply($text);
93
}
94
return $text;
95
}
96
97
public function supportsChildBlocks() {
98
return false;
99
}
100
101
public function extractChildText($text) {
102
throw new PhutilMethodNotImplementedException();
103
}
104
105
protected function renderRemarkupTable(array $out_rows) {
106
assert_instances_of($out_rows, 'array');
107
108
if ($this->getEngine()->isTextMode()) {
109
$lengths = array();
110
foreach ($out_rows as $r => $row) {
111
foreach ($row['content'] as $c => $cell) {
112
$text = $this->getEngine()->restoreText($cell['content']);
113
$lengths[$c][$r] = phutil_utf8_strlen($text);
114
}
115
}
116
$max_lengths = array_map('max', $lengths);
117
118
$out = array();
119
foreach ($out_rows as $r => $row) {
120
$headings = false;
121
foreach ($row['content'] as $c => $cell) {
122
$length = $max_lengths[$c] - $lengths[$c][$r];
123
$out[] = '| '.$cell['content'].str_repeat(' ', $length).' ';
124
if ($cell['type'] == 'th') {
125
$headings = true;
126
}
127
}
128
$out[] = "|\n";
129
130
if ($headings) {
131
foreach ($row['content'] as $c => $cell) {
132
$char = ($cell['type'] == 'th' ? '-' : ' ');
133
$out[] = '| '.str_repeat($char, $max_lengths[$c]).' ';
134
}
135
$out[] = "|\n";
136
}
137
}
138
139
return rtrim(implode('', $out), "\n");
140
}
141
142
if ($this->getEngine()->isHTMLMailMode()) {
143
$table_attributes = array(
144
'style' => 'border-collapse: separate;
145
border-spacing: 1px;
146
background: #d3d3d3;
147
margin: 12px 0;',
148
);
149
$cell_attributes = array(
150
'style' => 'background: #ffffff;
151
padding: 3px 6px;',
152
);
153
} else {
154
$table_attributes = array(
155
'class' => 'remarkup-table',
156
);
157
$cell_attributes = array();
158
}
159
160
$out = array();
161
$out[] = "\n";
162
foreach ($out_rows as $row) {
163
$cells = array();
164
foreach ($row['content'] as $cell) {
165
$cells[] = phutil_tag(
166
$cell['type'],
167
$cell_attributes,
168
$cell['content']);
169
}
170
$out[] = phutil_tag($row['type'], array(), $cells);
171
$out[] = "\n";
172
}
173
174
$table = phutil_tag('table', $table_attributes, $out);
175
return phutil_tag_div('remarkup-table-wrap', $table);
176
}
177
178
}
179
180