Diff.php
[Pman.MTrack] / Diff.php
1 <?php
2 class Pman_MTrack_Diff extends Pman {
3     
4     function getAuth()
5     {
6         die("nothing here yet");
7     }
8    
9    
10     static function toHTML($input)
11     {
12          
13         $nlines = 0;
14
15         if (is_resource($input)) {
16             $lines = array();
17             while (($line = fgets($input)) !== false) {
18               $lines[] = rtrim($line, "\r\n");
19             }
20             $input = $lines;
21         }
22         
23         // abase???
24         if (is_string($input)) {
25             $abase = md5($input);
26             $input = preg_split("/\r?\n/", $input);
27         } else {
28             $abase = md5(join("\n", $input));
29         }
30         //echo '<pre>' . implode("\n", $input);exit;
31         $html = "<code><table class='code diff'>";
32             //$html = "<pre class='code diff'>";
33           
34         while (true) {
35             if (!count($input)) {
36                 break;
37             }
38             $line = array_shift($input);
39             $nlines++;
40             if (!strncmp($line, '@@ ', 3)) {
41                 /* done with preamble */
42                 break;
43             }
44             $html .=  "<tr class='meta'><td class='lineno'></td>".
45                     "<td class='lineno'></td><td class='lineno'></td><td width='100%'>". 
46                     htmlspecialchars($line, ENT_QUOTES, 'utf-8') .
47                     '</tr>';
48         }
49         $class = 'meta';
50           
51         $lines = array(0, 0);
52         $first = false;
53         
54         $in_meta = false;
55         
56         while (true) {
57             $class = 'unmod';
58             
59             if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
60                   $line, $M)) {
61                 $lines[0] = (int)$M[1] - 1;
62                 $lines[1] = (int)$M[2] - 1;
63                 $class = 'meta';
64                 $first = true;
65                 $in_meta = false;
66             } else if (preg_match("/^diff /", $line)) {
67                 $class = 'head';
68                 $in_meta=true;
69             
70             } elseif (strlen($line)) {
71                 if ($line[0] == '-') {
72                     $lines[0]++;
73                     $class = 'removed';
74                 } elseif ($line[0] == '+') {
75                     $lines[1]++;
76                     $class = 'added';
77                 } else {
78                     $lines[0]++;
79                     $lines[1]++;
80                 }
81             } else {
82                 $lines[0]++;
83                 $lines[1]++;
84             }
85             
86             
87             $row = "<tr class='$class";
88             if ($first) {
89                 $row .= ' first';
90             }
91             if ($class != 'meta' && $first) {
92                 $first = false;
93             }
94             $row .= "'>";
95
96             $anchor = $abase . '.' . $nlines;
97             $anchor_row = "<td class='linelink'><a name='$anchor'></a>".
98                     "<a href='#$anchor' title='link to this line'>#</a></td>";
99           
100             $txt_data = '<td class="line" width="100%">'.
101                 htmlspecialchars($line, ENT_QUOTES, 'utf-8') .
102                 "</td></tr>\n";
103           
104             switch ($class) {
105                 case 'head':
106                     $line_info = '';
107                     $row .= '<td colspan="4" class="line" width="100%">' .
108                         htmlspecialchars($line, ENT_QUOTES, 'utf-8') . "</td></tr>\n";
109                     $html .= $row;
110                     
111                     break;
112                 
113                 case 'meta':
114                     $line_info = '';
115                     $row .= '<td colspan="4" class="line" width="100%">' .
116                         htmlspecialchars($line, ENT_QUOTES, 'utf-8') . "</td></tr>\n";
117                     $html .= $row;
118                     break;
119                 
120                 case 'added':
121                     $row .= "<td class='lineno'></td><td class='lineno'>" .
122                         $lines[1] . "</td>" .
123                         $anchor_row . $txt_data   ;
124                     $html .= $row;
125
126                     break;
127                 
128                 case 'removed':
129                     $row .= "<td class='lineno'>" . $lines[0] .
130                             "</td><td class='lineno'></td>" .
131                         $anchor_row   . $txt_data ;
132                     $html .= $row;
133
134                     break;
135                 
136                 default:
137                     $row .= "<td class='lineno'>" . $lines[0] .
138                         "</td><td class='lineno'>" . $lines[1] . "</td>" .
139                         $anchor_row   . $txt_data ;
140                        $html .= $row;
141             }
142             
143            
144         
145             if (!count($input)) {
146                 break;
147             }
148             $line = array_shift($input);
149             $nlines++;
150         }
151           
152         if ($nlines == 0) {
153           return null;
154         }
155       
156         $html .= "</table>";
157         return $html;
158     }
159 }