forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorSourceCodeView.php
141 lines (118 loc) · 3.32 KB
/
PhabricatorSourceCodeView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
final class PhabricatorSourceCodeView extends AphrontView {
private $lines;
private $uri;
private $highlights = array();
private $canClickHighlight = true;
private $truncatedFirstBytes = false;
private $truncatedFirstLines = false;
public function setLines(array $lines) {
$this->lines = $lines;
return $this;
}
public function setURI(PhutilURI $uri) {
$this->uri = $uri;
return $this;
}
public function setHighlights(array $array) {
$this->highlights = array_fuse($array);
return $this;
}
public function disableHighlightOnClick() {
$this->canClickHighlight = false;
return $this;
}
public function setTruncatedFirstBytes($truncated_first_bytes) {
$this->truncatedFirstBytes = $truncated_first_bytes;
return $this;
}
public function setTruncatedFirstLines($truncated_first_lines) {
$this->truncatedFirstLines = $truncated_first_lines;
return $this;
}
public function render() {
require_celerity_resource('phabricator-source-code-view-css');
require_celerity_resource('syntax-highlighting-css');
Javelin::initBehavior('phabricator-oncopy', array());
if ($this->canClickHighlight) {
Javelin::initBehavior('phabricator-line-linker');
}
$line_number = 1;
$rows = array();
$lines = $this->lines;
if ($this->truncatedFirstLines) {
$lines[] = phutil_tag(
'span',
array(
'class' => 'c',
),
pht('...'));
} else if ($this->truncatedFirstBytes) {
$last_key = last_key($lines);
$lines[$last_key] = hsprintf(
'%s%s',
$lines[$last_key],
phutil_tag(
'span',
array(
'class' => 'c',
),
pht('...')));
}
foreach ($lines as $line) {
// NOTE: See phabricator-oncopy behavior.
$content_line = hsprintf("\xE2\x80\x8B%s", $line);
$row_attributes = array();
if (isset($this->highlights[$line_number])) {
$row_attributes['class'] = 'phabricator-source-highlight';
}
if ($this->canClickHighlight) {
$line_uri = $this->uri.'$'.$line_number;
$line_href = (string)new PhutilURI($line_uri);
$tag_number = javelin_tag(
'a',
array(
'href' => $line_href,
),
$line_number);
} else {
$tag_number = javelin_tag(
'span',
array(),
$line_number);
}
$rows[] = phutil_tag(
'tr',
$row_attributes,
array(
javelin_tag(
'th',
array(
'class' => 'phabricator-source-line',
'sigil' => 'phabricator-source-line',
),
$tag_number),
phutil_tag(
'td',
array(
'class' => 'phabricator-source-code',
),
$content_line),
));
$line_number++;
}
$classes = array();
$classes[] = 'phabricator-source-code-view';
$classes[] = 'remarkup-code';
$classes[] = 'PhabricatorMonospaced';
return phutil_tag_div(
'phabricator-source-code-container',
javelin_tag(
'table',
array(
'class' => implode(' ', $classes),
'sigil' => 'phabricator-source',
),
phutil_implode_html('', $rows)));
}
}