2012-08-15 19:45:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorSourceCodeView extends AphrontView {
|
|
|
|
|
|
|
|
private $lines;
|
2012-12-17 01:33:42 +01:00
|
|
|
private $limit;
|
2013-08-04 21:11:10 +02:00
|
|
|
private $uri;
|
2013-03-14 17:32:02 +01:00
|
|
|
private $highlights = array();
|
2013-09-25 14:57:03 +02:00
|
|
|
private $canClickHighlight = true;
|
2012-12-17 01:33:42 +01:00
|
|
|
|
|
|
|
public function setLimit($limit) {
|
|
|
|
$this->limit = $limit;
|
|
|
|
return $this;
|
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
|
|
|
public function setLines(array $lines) {
|
|
|
|
$this->lines = $lines;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-04 21:11:10 +02:00
|
|
|
public function setURI(PhutilURI $uri) {
|
|
|
|
$this->uri = $uri;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHighlights(array $array) {
|
|
|
|
$this->highlights = array_fuse($array);
|
2013-03-14 17:32:02 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-09-25 14:57:03 +02:00
|
|
|
public function disableHighlightOnClick() {
|
|
|
|
$this->canClickHighlight = false;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
public function render() {
|
|
|
|
require_celerity_resource('phabricator-source-code-view-css');
|
|
|
|
require_celerity_resource('syntax-highlighting-css');
|
|
|
|
|
|
|
|
Javelin::initBehavior('phabricator-oncopy', array());
|
2013-09-25 14:57:03 +02:00
|
|
|
if ($this->canClickHighlight) {
|
|
|
|
Javelin::initBehavior('phabricator-line-linker');
|
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
|
|
|
$line_number = 1;
|
|
|
|
|
|
|
|
$rows = array();
|
2013-08-04 21:11:10 +02:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
foreach ($this->lines as $line) {
|
2012-12-17 01:33:42 +01:00
|
|
|
$hit_limit = $this->limit &&
|
|
|
|
($line_number == $this->limit) &&
|
|
|
|
(count($this->lines) != $this->limit);
|
|
|
|
|
|
|
|
if ($hit_limit) {
|
|
|
|
$content_number = '';
|
2013-01-18 04:15:06 +01:00
|
|
|
$content_line = phutil_tag(
|
2012-12-17 01:33:42 +01:00
|
|
|
'span',
|
|
|
|
array(
|
|
|
|
'class' => 'c',
|
|
|
|
),
|
|
|
|
pht('...'));
|
|
|
|
} else {
|
2013-02-13 23:50:15 +01:00
|
|
|
$content_number = $line_number;
|
|
|
|
$content_line = hsprintf("\xE2\x80\x8B%s", $line);
|
2012-12-17 01:33:42 +01:00
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
2013-03-14 17:32:02 +01:00
|
|
|
$row_attributes = array();
|
2013-03-14 18:51:34 +01:00
|
|
|
if (isset($this->highlights[$line_number])) {
|
2013-03-14 17:32:02 +01:00
|
|
|
$row_attributes['class'] = 'phabricator-source-highlight';
|
|
|
|
}
|
|
|
|
|
2013-09-25 14:57:03 +02:00
|
|
|
if ($this->canClickHighlight) {
|
|
|
|
$line_uri = $this->uri . "$" . $line_number;
|
|
|
|
$line_href = (string) new PhutilURI($line_uri);
|
2013-08-04 21:11:10 +02:00
|
|
|
|
2013-09-25 14:57:03 +02:00
|
|
|
$tag_number = javelin_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $line_href
|
|
|
|
),
|
|
|
|
$line_number);
|
|
|
|
} else {
|
|
|
|
$tag_number = javelin_tag(
|
|
|
|
'span',
|
|
|
|
array(),
|
|
|
|
$line_number);
|
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
2013-03-14 17:32:02 +01:00
|
|
|
$rows[] = phutil_tag(
|
|
|
|
'tr',
|
|
|
|
$row_attributes,
|
2013-08-04 21:11:10 +02:00
|
|
|
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)));
|
2012-08-15 19:45:06 +02:00
|
|
|
|
2012-12-17 01:33:42 +01:00
|
|
|
if ($hit_limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$line_number++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes = array();
|
|
|
|
$classes[] = 'phabricator-source-code-view';
|
|
|
|
$classes[] = 'remarkup-code';
|
|
|
|
$classes[] = 'PhabricatorMonospaced';
|
|
|
|
|
2013-11-09 19:48:19 +01:00
|
|
|
return phutil_tag_div(
|
|
|
|
'phabricator-source-code-container',
|
2013-08-04 21:11:10 +02:00
|
|
|
javelin_tag(
|
2012-12-17 02:58:16 +01:00
|
|
|
'table',
|
|
|
|
array(
|
|
|
|
'class' => implode(' ', $classes),
|
2013-08-04 21:11:10 +02:00
|
|
|
'sigil' => 'phabricator-source'
|
2012-12-17 02:58:16 +01:00
|
|
|
),
|
2013-02-13 23:50:15 +01:00
|
|
|
phutil_implode_html('', $rows)));
|
2012-08-15 19:45:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|