View.php
[Pman.MTrack] / Diff.php
index 4b878b4..e92b0a1 100644 (file)
--- a/Diff.php
+++ b/Diff.php
@@ -7,11 +7,9 @@ class Pman_MTrack_Diff extends Pman {
     }
    
    
-    function toHTML($input)
+    static function toHTML($input)
     {
-        
-    
-    
+         
         $nlines = 0;
 
         if (is_resource($input)) {
@@ -29,95 +27,108 @@ class Pman_MTrack_Diff extends Pman {
         } else {
             $abase = md5(join("\n", $input));
         }
-
-        $html = "<table class='code diff'>";
-            //$html = "<pre class='code diff'>";
-          
+        
+        $ret = array();
+           
         while (true) {
+             
+            
             if (!count($input)) {
                 break;
             }
+            
             $line = array_shift($input);
             $nlines++;
+            
             if (!strncmp($line, '@@ ', 3)) {
                 /* done with preamble */
                 break;
             }
-            $line = 
-            $line = "<tr class='meta'><td class='lineno'></td>".
-                    "<td class='lineno'></td><td class='lineno'></td><td width='100%'>". 
-                    htmlspecialchars($line, ENT_QUOTES, 'utf-8') .
-                    '</tr>';
-            $html .= $line . "\n";
+            $ret[] = (object)array(
+                
+                'line' => $line,
+                'meta' => true,
+                'cls' =>'meta'
+                
+            );
+             
         }
+        $class = 'meta';
           
-            $lines = array(0, 0);
-            $first = false;
-            while (true) {
-              $class = 'unmod';
-          
-              if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
+        $lines = array(0, 0);
+        $first = false;
+        
+        $in_meta = false;
+        
+        while (true) {
+            $class = 'unmod';
+            
+            if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
                   $line, $M)) {
                 $lines[0] = (int)$M[1] - 1;
                 $lines[1] = (int)$M[2] - 1;
                 $class = 'meta';
                 $first = true;
-              } elseif (strlen($line)) {
+                $in_meta = false;
+            } else if (preg_match("/^diff /", $line)) {
+                $class = 'meta';
+                $in_meta=true;
+            
+            } elseif (strlen($line)) {
                 if ($line[0] == '-') {
-                  $lines[0]++;
-                  $class = 'removed';
+                    $lines[0]++;
+                    $class = 'removed';
                 } elseif ($line[0] == '+') {
-                  $lines[1]++;
-                  $class = 'added';
+                    $lines[1]++;
+                    $class = 'added';
                 } else {
-                  $lines[0]++;
-                  $lines[1]++;
+                    $lines[0]++;
+                    $lines[1]++;
                 }
-              } else {
+            } else {
                 $lines[0]++;
                 $lines[1]++;
-              }
-              $row = "<tr class='$class";
-              if ($first) {
-                $row .= ' first';
-              }
-              if ($class != 'meta' && $first) {
+            }
+            
+            
+            if ($in_meta) {
+                $class = 'meta';
+            }
+            $cls = $class;
+            
+            if ($first) {
+                $cls .= ' first';
+            } else
+            if ($class != 'meta' && $first) {
                 $first = false;
-              }
-              $row .= "'>";
-          
-              switch ($class) {
-                case 'meta':
-                  $line_info = '';
-                  $row .= "<td class='lineno'></td><td class='lineno'></td>";
-                  break;
-                case 'added':
-                  $row .= "<td class='lineno'></td><td class='lineno'>" . $lines[1] . "</td>";
-                  break;
-                case 'removed':
-                  $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'></td>";
-                  break;
-                default:
-                  $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'>" . $lines[1] . "</td>";
-              }
-              $anchor = $abase . '.' . $nlines;
-              $row .= "<td class='linelink'><a name='$anchor'></a><a href='#$anchor' title='link to this line'>#</a></td>";
-          
-              $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
-              $row .= "<td class='line' width='100%'>$line</td></tr>\n";
-              $html .= $row;
-          
-              if (!count($input)) {
-                break;
-              }
-              $line = array_shift($input);
-              $nlines++;
             }
-          
-            if ($nlines == 0) {
-              return null;
+        
+
+            $anchor = $abase . '.' . $nlines;
+            $add = (object)array(
+                
+                'line' => $class == 'meta' ? $line : substr($line, 1),
+                'meta' => $class == 'meta',
+                'cls' => $cls,
+                'anchor' => $class == 'meta' ? $anchor : false
+            );
+            
+            $add->$class = true;
+            
+            $ret[] = $add;
+           
+        
+            if (!count($input)) {
+                break;
             }
+            $line = array_shift($input);
+            $nlines++;
+        }
           
-            $html .= "</table>";
-            return $html;
+        if ($nlines == 0) {
+            return null;
+        }
+      
+        return $ret; 
+    }
 }