From 30a17c2039ef41ea903ce51eb2d594cf5cd563ca Mon Sep 17 00:00:00 2001 From: Lauri-Henrik Jalonen Date: Thu, 14 Mar 2013 09:32:02 -0700 Subject: [PATCH] Embed pastes now support highlight Summary: - Added support for highlighting to PhabricatorSourceCodeView - Added support for highlighting to embed pastes Test Plan: {F35975} Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T1770 Differential Revision: https://secure.phabricator.com/D5346 --- .../remarkup/PhabricatorPasteRemarkupRule.php | 28 +++++++++++++++++++ .../paste/view/PasteEmbedView.php | 9 +++++- src/view/layout/PhabricatorSourceCodeView.php | 25 +++++++++++++---- webroot/rsrc/css/application/paste/paste.css | 5 ++-- .../layout/phabricator-source-code-view.css | 4 +++ 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/applications/paste/remarkup/PhabricatorPasteRemarkupRule.php b/src/applications/paste/remarkup/PhabricatorPasteRemarkupRule.php index c7c88dd7a2..93b987e570 100644 --- a/src/applications/paste/remarkup/PhabricatorPasteRemarkupRule.php +++ b/src/applications/paste/remarkup/PhabricatorPasteRemarkupRule.php @@ -26,6 +26,34 @@ final class PhabricatorPasteRemarkupRule ->setPaste($object) ->setHandle($handle); + if (strlen($options)) { + $parser = new PhutilSimpleOptions(); + $opts = $parser->parse(substr($options, 1)); + + foreach ($opts as $key => $value) { + if ($key == 'lines') { + // placeholder for now + } else if ($key == 'highlight') { + $highlights = explode('&', preg_replace('/\s+/', '', $value)); + + $to_highlight = array(); + foreach ($highlights as $highlight) { + $highlight = explode('-', $highlight); + + if (!empty($highlight)) { + sort($highlight); + $to_highlight = array_merge( + $to_highlight, + range(head($highlight), last($highlight))); + } + } + + $embed_paste->setHighlights(array_unique($to_highlight)); + } + } + + } + return $embed_paste->render(); } diff --git a/src/applications/paste/view/PasteEmbedView.php b/src/applications/paste/view/PasteEmbedView.php index de7825ecc3..315404cf49 100644 --- a/src/applications/paste/view/PasteEmbedView.php +++ b/src/applications/paste/view/PasteEmbedView.php @@ -4,6 +4,7 @@ final class PasteEmbedView extends AphrontView { private $paste; private $handle; + private $highlights = array(); public function setPaste(PhabricatorPaste $paste) { $this->paste = $paste; @@ -15,6 +16,11 @@ final class PasteEmbedView extends AphrontView { return $this; } + public function setHighlights(array $highlights) { + $this->highlights = $highlights; + return $this; + } + public function render() { if (!$this->paste) { throw new Exception("Call setPaste() before render()!"); @@ -41,7 +47,8 @@ final class PasteEmbedView extends AphrontView { 'div', array(), id(new PhabricatorSourceCodeView()) - ->setLines($lines)); + ->setLines($lines) + ->setHighlights($this->highlights)); return phutil_tag( 'div', diff --git a/src/view/layout/PhabricatorSourceCodeView.php b/src/view/layout/PhabricatorSourceCodeView.php index f957c6ef88..70956a4be8 100644 --- a/src/view/layout/PhabricatorSourceCodeView.php +++ b/src/view/layout/PhabricatorSourceCodeView.php @@ -4,6 +4,7 @@ final class PhabricatorSourceCodeView extends AphrontView { private $lines; private $limit; + private $highlights = array(); public function setLimit($limit) { $this->limit = $limit; @@ -15,6 +16,11 @@ final class PhabricatorSourceCodeView extends AphrontView { return $this; } + public function setHighlights(array $highlights) { + $this->highlights = $highlights; + return $this; + } + public function render() { require_celerity_resource('phabricator-source-code-view-css'); require_celerity_resource('syntax-highlighting-css'); @@ -25,6 +31,7 @@ final class PhabricatorSourceCodeView extends AphrontView { $rows = array(); foreach ($this->lines as $line) { + $hit_limit = $this->limit && ($line_number == $this->limit) && (count($this->lines) != $this->limit); @@ -42,15 +49,21 @@ final class PhabricatorSourceCodeView extends AphrontView { $content_line = hsprintf("\xE2\x80\x8B%s", $line); } + $row_attributes = array(); + if (in_array($line_number, $this->highlights)) { + $row_attributes['class'] = 'phabricator-source-highlight'; + } + // TODO: Provide nice links. - $rows[] = hsprintf( - ''. + $rows[] = phutil_tag( + 'tr', + $row_attributes, + hsprintf( '%s'. - '%s'. - '', - $content_number, - $content_line); + '%s', + $content_number, + $content_line)); if ($hit_limit) { break; diff --git a/webroot/rsrc/css/application/paste/paste.css b/webroot/rsrc/css/application/paste/paste.css index 92e86d0c28..9050efe54f 100644 --- a/webroot/rsrc/css/application/paste/paste.css +++ b/webroot/rsrc/css/application/paste/paste.css @@ -3,14 +3,13 @@ */ .paste-embed { - display: inline-block; padding: 5px; background: #f7f7f7; + border: 1px solid #dbdbdb; } .paste-embed-head { - border-bottom: 1px solid #3d3d3d; - padding: 2px; + border-bottom: 1px solid #dbdbdb; margin:2px; } diff --git a/webroot/rsrc/css/layout/phabricator-source-code-view.css b/webroot/rsrc/css/layout/phabricator-source-code-view.css index fcd4058225..dcb8f2faef 100644 --- a/webroot/rsrc/css/layout/phabricator-source-code-view.css +++ b/webroot/rsrc/css/layout/phabricator-source-code-view.css @@ -30,6 +30,10 @@ user-select: none; } +.phabricator-source-highlight { + background: #ff0; +} + .phabricator-source-code-summary { padding-bottom: 8px; }