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-03-14 17:32:02 +01:00
|
|
|
private $highlights = array();
|
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-03-14 17:32:02 +01:00
|
|
|
public function setHighlights(array $highlights) {
|
|
|
|
$this->highlights = $highlights;
|
|
|
|
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());
|
|
|
|
|
|
|
|
$line_number = 1;
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
foreach ($this->lines as $line) {
|
2013-03-14 17:32:02 +01:00
|
|
|
|
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();
|
|
|
|
if (in_array($line_number, $this->highlights)) {
|
|
|
|
$row_attributes['class'] = 'phabricator-source-highlight';
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
// TODO: Provide nice links.
|
|
|
|
|
2013-03-14 17:32:02 +01:00
|
|
|
$rows[] = phutil_tag(
|
|
|
|
'tr',
|
|
|
|
$row_attributes,
|
|
|
|
hsprintf(
|
2013-02-13 23:50:15 +01:00
|
|
|
'<th class="phabricator-source-line">%s</th>'.
|
2013-03-14 17:32:02 +01:00
|
|
|
'<td class="phabricator-source-code">%s</td>',
|
|
|
|
$content_number,
|
|
|
|
$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-01-18 03:47:13 +01:00
|
|
|
return phutil_tag(
|
2012-12-17 02:58:16 +01:00
|
|
|
'div',
|
2012-08-15 19:45:06 +02:00
|
|
|
array(
|
2012-12-17 02:58:16 +01:00
|
|
|
'class' => 'phabricator-source-code-container',
|
2012-08-15 19:45:06 +02:00
|
|
|
),
|
2013-01-18 03:47:13 +01:00
|
|
|
phutil_tag(
|
2012-12-17 02:58:16 +01:00
|
|
|
'table',
|
|
|
|
array(
|
|
|
|
'class' => implode(' ', $classes),
|
|
|
|
),
|
2013-02-13 23:50:15 +01:00
|
|
|
phutil_implode_html('', $rows)));
|
2012-08-15 19:45:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|