[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/view/layout/ -> PhabricatorSourceCodeView.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1