http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95043

Revision: 95043
Author:   hashar
Date:     2011-08-19 21:00:43 +0000 (Fri, 19 Aug 2011)
Log Message:
-----------
line numbers and better style

* new system to parse the diff, see parseLine() dispatcher.
* show lines numbering for each chunk.
* tweak style to replace the <pre> look'n feel, removing the first-child
  last-child selectors.
* line numbers are not selected on mouse selection. Makes it easy to
  copy paste the patch.

Note: old code left unchanged. To use it again, change the array_map
callback in splitLines from 'parseLine' to 'colorLine'.

Modified Paths:
--------------
    trunk/extensions/CodeReview/backend/DiffHighlighter.php
    trunk/extensions/CodeReview/modules/ext.codereview.styles.css

Modified: trunk/extensions/CodeReview/backend/DiffHighlighter.php
===================================================================
--- trunk/extensions/CodeReview/backend/DiffHighlighter.php     2011-08-19 
20:58:32 UTC (rev 95042)
+++ trunk/extensions/CodeReview/backend/DiffHighlighter.php     2011-08-19 
21:00:43 UTC (rev 95043)
@@ -4,6 +4,9 @@
  * Highlight a SVN diff for easier readibility
  */
 class CodeDiffHighlighter {
+       private $left  = 0;
+       private $right = 0;
+       private $chunk = 0;
 
        /**
         * Main entry point. Given a diff text, highlight it
@@ -26,11 +29,132 @@
         */
        function splitLines( $text ) {
                return implode( "\n",
-                       array_map( array( $this, 'colorLine' ),
+                       array_map( array( $this, 'parseLine' ),
                                explode( "\n", $text ) ) );
        }
 
        /**
+        * Internal dispatcher to a handler depending on line
+        * Handles lines beginning with '-' '+' '@' and ' '
+        * @param string $line Diff line to parse
+        * @return string HTML table line (with <tr></tr>)
+        */
+       function parseLine( $line ) {
+               if( $line === '' ) { return ""; } // do not create bogus lines
+
+               # Dispatch diff lines to the proper handler
+               switch( substr( $line, 0, 1 ) ) {
+               case '-':
+                       if( substr( $line, 0, 3 ) === '---' ) {
+                               return;
+                       }
+                       $r = $this->handleLineDeletion( $line );
+                       break;
+               case '+':
+                       if( substr( $line, 0, 3 ) === '+++' ) {
+                               return;
+                       }
+                       $r = $this->handleLineAddition( $line );
+                       break;
+               case '@':
+                       $r = $this->handleChunkDelimiter( $line );
+                       break;
+               case ' ':
+                       $r = $this->handleUnchanged( $line );
+                       break;
+
+               # Patch lines that will be skipped:
+               case '=':
+                       return;
+
+               # Remaining case should be the file name
+               default:
+                       $r = $this->handleLineFile( $line );
+               }
+
+               # Return HTML generated by one of the handler
+               return $r;
+       }
+
+       function formatLine( $content, $class = null ) {
+               $formatLN =
+                         "<td class=\"linenumbers\">%s</td>"
+                       . "<td class=\"linenumbers\">%s</td>"
+                       ;
+
+               if( is_null($class) ) {
+                       return sprintf( "<tr>{$formatLN}<td>%s</td></tr>\n",
+                               $this->left,
+                               $this->right,
+                               $content
+                       );
+               }
+
+               # Skip line number when they do not apply
+               $left = $right = '&nbsp;';
+
+               switch( $class ) {
+               case 'chunkdelimiter':
+                       $left = $right = '&mdash;';
+                       break;
+               case 'unchanged':
+                       $left  = $this->left;
+                       $right = $this->right;
+                       break;
+               case 'del':
+                       $left  = $this->left;
+                       break;
+               case 'ins':
+                       $right = $this->right;
+                       break;
+
+               default:
+                       # Rely on $left, $right initialization above
+               }
+
+               $classAttr = is_null($class) ? '' : " class=\"$class\"";
+               return sprintf( "<tr>{$formatLN}<td%s>%s</td></tr>\n",
+                       $left, $right,
+                       $classAttr, $content
+               );
+       }
+
+       #### LINES HANDLERS ################################################
+       function handleLineDeletion( $line ) {
+               $this->left++;
+               return $this->formatLine( $line, 'del' );
+       }
+
+       function handleLineAddition( $line ) {
+               $this->right++;
+               return $this->formatLine( $line, 'ins' );
+       }
+
+       function handleChunkDelimiter( $line ) {
+               $this->chunk++;
+
+               list(
+                       $this->left,
+                       $leftChanged,  # unused
+                       $this->right,
+                       $rightChanged  # unused
+               ) = $this->parseChunkDelimiter( $line );
+
+               return self::formatLine( $line, 'chunkdelimiter' );
+       }
+
+       function handleUnchanged( $line ) {
+               $this->left++;
+               $this->right++;
+               return $this->formatLine( $line, 'unchanged' );
+       }
+
+       function handleLineFile( $line ) {
+               return "<tr class=\"patchedfile\"><td 
colspan=\"3\">$line</td></tr>";
+       }
+       #### END OF LINES HANDLERS #########################################
+
+       /**
         * Turn a diff line into a properly formatted string suitable
         * for output
         * @param $line string Line from a diff

Modified: trunk/extensions/CodeReview/modules/ext.codereview.styles.css
===================================================================
--- trunk/extensions/CodeReview/modules/ext.codereview.styles.css       
2011-08-19 20:58:32 UTC (rev 95042)
+++ trunk/extensions/CodeReview/modules/ext.codereview.styles.css       
2011-08-19 21:00:43 UTC (rev 95043)
@@ -134,39 +134,41 @@
 .mw-codereview-diff table {
        /* @noflip */direction: ltr; /* Source code is always LTR */
 
-       /* mimic MediaWiki <pre> style */
        font-family: monospace, "Courer New";
        line-height: 1.3em;
        background-color: #F9F9F9;
-       border: 1px dashed #2F6FAB;
+       border: 1px solid #CCC;
        color: black;
 
-       /* fix up space between <tr> */
+       /* fix up space between cells (cellspacing) */
        border-collapse: collapse;
 }
+
+.mw-codereview-diff tr.patchedfile {
+       background-color: #EEE;
+       color: black;
+}
+.mw-codereview-diff tr.patchedfile td {
+       padding: 1em;
+       border: 1px solid #CCC;
+}
+
 .mw-codereview-diff td {
-       margin:0;
+       border: 1px #CCC;
+       border-style: none solid;
 
-       /* keep padding on left and right just like <pre>
-        * top bottom paddings are defined below for first/last childs
-        */
-       padding:0 1em;
-
        /* respect white spaces just like <pre> */
        white-space: pre;
 }
 
-/* "table border-collapse: collapse;" overrides padding
- * The two next rules mimic <pre> padding by applying one to the first
- * and last childs
- */
-.mw-codereview-diff tr:first-child td {
-       padding-top: 1em;
+.mw-codereview-diff td.linenumbers{
+       background-color: #EEE;
+       -webkit-user-select: none;
+       -khtml-user-select: none;
+       -moz-user-select: none;
+       -o-user-select: none;
+       user-select: none;
 }
-.mw-codereview-diff tr:last-child td {
-       padding-bottom: 1em;
-}
-
 .mw-codereview-diff td.ins {
        text-decoration: none;
        color: green;
@@ -175,6 +177,10 @@
        text-decoration: none;
        color: red;
 }
+.mw-codereview-diff td.chunkdelimiter {
+       background-color: #EDEDFF;
+       color: black;
+}
 
 .mw-codereview-diff .meta {
        color: #008b8b;


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to