Indefero

Indefero Commit Details


Date:2011-10-08 18:54:51 (13 years 2 months ago)
Author:Thomas Keller
Branch:develop, feature.diff-whitespace, feature.wiki-default-page, release-1.2, release-1.3
Commit:6abd0b6faa461df3a19873a2ec656fb33ea64c7d
Parents: fef2bd15bffb413cb27a470b1a419fc40b1515cc
Message:- Move common static methods out of IDF_Diff and into IDF_FileUtil. - Make stuff that should be private in IDF_Diff really private and comment out a test that was the only call path for a previously public method. - Apply the whitespace emphasizing on the normal file view as well and get finally rid of padLine()

Changes:

File differences

src/IDF/Diff.php
3535
3636
3737
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
38
6139
6240
6341
......
192170
193171
194172
195
173
196174
197175
198176
......
245223
246224
247225
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275226
276227
277228
......
299250
300251
301252
302
253
303254
304255
305256
......
396347
397348
398349
399
350
400351
401352
402353
public function __construct($diff, $path_strip_level = 0)
{
$this->path_strip_level = $path_strip_level;
$this->lines = self::splitIntoLines($diff);
}
/**
* Splits a diff into separate lines while retaining the individual
* line ending character for every line
*/
private static function splitIntoLines($diff)
{
// this works because in unified diff format even empty lines are
// either prefixed with a '+', '-' or ' '
$splitted = preg_split("/\r\n|\n/", $diff, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
$last_off = -1;
$lines = array();
while (($split = array_shift($splitted)) !== null) {
if ($last_off != -1) {
$lines[] .= substr($diff, $last_off, $split[1] - $last_off);
}
$last_off = $split[1];
}
$lines[] = substr($diff, $last_off);
return $lines;
$this->lines = IDF_FileUtil::splitIntoLines($diff, true);
}
public function parse()
$offsets[] = sprintf('<td>%s</td><td>%s</td>', $left, $right);
$content = Pluf_esc($content);
$content = self::makeNonPrintableCharsVisible($content);
$content = IDF_FileUtil::emphasizeControlCharacters($content);
$contents[] = sprintf('<td class="%s%s mono">%s</td>', $class, $pretty, $content);
}
if (count($file['chunks']) > $cc) {
return Pluf_Template::markSafe($out);
}
private static function makeNonPrintableCharsVisible($line)
{
// This translates most of the C0 ASCII control characters into
// their visual counterparts in the 0x24## unicode plane
// (http://en.wikipedia.org/wiki/C0_and_C1_control_codes).
// We could add DEL (0x7F) to this set, but unfortunately this
// is not nicely mapped to 0x247F in the control plane, but 0x2421
// and adding an if expression below just for this is a little bit
// of a hassle. And of course, the more esoteric ones from C1 are
// missing as well...
return preg_replace('/([\x00-\x1F])/ue',
'"<span class=\"non-printable\" title=\"0x".bin2hex("\\1")."\">&#x24".bin2hex("\\1")."</span>"',
$line);
}
public static function padLine($line)
{
$line = str_replace("\t", ' ', $line);
$n = strlen($line);
for ($i=0;$i<$n;$i++) {
if (substr($line, $i, 1) != ' ') {
break;
}
}
return str_repeat('&nbsp;', $i).substr($line, $i);
}
/**
* Review patch.
*
return $this->renderCompared($new_chunks, $filename);
}
public function mergeChunks($orig_lines, $chunks, $context=10)
private function mergeChunks($orig_lines, $chunks, $context=10)
{
$spans = array();
$new_chunks = array();
return $nnew_chunks;
}
public function renderCompared($chunks, $filename)
private function renderCompared($chunks, $filename)
{
$fileinfo = IDF_FileUtil::getMimeType($filename);
$pretty = '';
src/IDF/FileUtil.php
6565
6666
6767
68
68
6969
70
70
7171
7272
7373
......
144144
145145
146146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
147197
148198
149199
}
$table = array();
$i = 1;
foreach (preg_split("/\015\012|\015|\012/", $content) as $line) {
foreach (self::splitIntoLines($content) as $line) {
$table[] = '<tr class="c-line"><td class="code-lc" id="L'.$i.'"><a href="#L'.$i.'">'.$i.'</a></td>'
.'<td class="code mono'.$pretty.'">'.IDF_Diff::padLine(Pluf_esc($line)).'</td></tr>';
.'<td class="code mono'.$pretty.'">'.self::emphasizeControlCharacters(Pluf_esc($line)).'</td></tr>';
$i++;
}
return Pluf_Template::markSafe(implode("\n", $table));
}
/**
* Splits a string into separate lines while retaining the individual
* line ending character for every line.
*
* OS9 line endings are not supported.
*
* @param string content
* @param boolean if true, skip completely empty lines
* @return string
*/
public static function splitIntoLines($content, $skipEmpty = false)
{
$flags = PREG_SPLIT_OFFSET_CAPTURE;
if ($skipEmpty) $flags |= PREG_SPLIT_NO_EMPTY;
$splitted = preg_split("/\r\n|\n/", $content, -1, $flags);
$last_off = -1;
$lines = array();
while (($split = array_shift($splitted)) !== null) {
if ($last_off != -1) {
$lines[] .= substr($content, $last_off, $split[1] - $last_off);
}
$last_off = $split[1];
}
$lines[] = substr($content, $last_off);
return $lines;
}
/**
* This translates most of the C0 ASCII control characters into
* their visual counterparts in the 0x24## unicode plane
* (http://en.wikipedia.org/wiki/C0_and_C1_control_codes).
*
* We could add DEL (0x7F) to this set, but unfortunately this
* is not nicely mapped to 0x247F in the control plane, but 0x2421
* and adding an if expression below just for this is a little bit
* of a hassle. And of course, the more esoteric ones from C1 are
* missing as well...
*
* @param string $content
* @return string
*/
public static function emphasizeControlCharacters($content)
{
return preg_replace(
'/([\x00-\x1F])/ue',
'"<span class=\"ctrl-char\" title=\"0x".bin2hex("\\1")."\">&#x24".bin2hex("\\1")."</span>"',
$content);
}
/**
* Find if a given mime type is a text file.
* This uses the output of the self::getMimeType function.
*
src/IDF/Tests/TestDiff.php
3232
3333
3434
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5053
5154
5255
parent::__construct('Test the diff parser.');
}
public function testBinaryDiff()
{
$diff_content = file_get_contents(dirname(__FILE__).'/test-diff.diff');
$orig = file_get_contents(dirname(__FILE__).'/test-diff-view.html');
$diff = new IDF_Diff($diff_content);
$diff->parse();
$def = $diff->files['src/IDF/templates/idf/issues/view.html'];
$orig_lines = preg_split("/\015\012|\015|\012/", $orig);
$merged = $diff->mergeChunks($orig_lines, $def, 10);
$lchunk = end($merged);
$lline = end($lchunk);
$this->assertEqual(array('', '166', '{/if}{/block}'),
$lline);
}
//
// IDF_Diff::mergeChunks() is now private, so this test needs to be rewritten
//
//public function testBinaryDiff()
//{
// $diff_content = file_get_contents(dirname(__FILE__).'/test-diff.diff');
// $orig = file_get_contents(dirname(__FILE__).'/test-diff-view.html');
// $diff = new IDF_Diff($diff_content);
// $diff->parse();
// $def = $diff->files['src/IDF/templates/idf/issues/view.html'];
//
// $orig_lines = preg_split("/\015\012|\015|\012/", $orig);
// $merged = $diff->mergeChunks($orig_lines, $def, 10);
// $lchunk = end($merged);
// $lline = end($lchunk);
// $this->assertEqual(array('', '166', '{/if}{/block}'),
// $lline);
//}
public function testDiffWithHeaders()
{
www/media/idf/css/style.css
571571
572572
573573
574
575
576
577
578
579
580
581
582
583
584
585
586
574587
575588
576589
......
658671
659672
660673
661
674
662675
663
664
665
666
667
668
669
670
671
672
673
674
675676
676677
677
678
678679
679680
680681
681
682
682683
683684
684685
685
686
686687
687688
688689
689
690
691
692
693690
694
691
695692
696693
697694
......
737734
738735
739736
737
738
739
740
741
742
743
744
740745
741746
742747
/**
* syntax highlighting of diffs
*/
span.ctrl-char {
color: white;
background: black;
text-align: center;
display: inline-block;
padding: 1px 1px 0px 1px;
margin-left: 1px;
margin-right: 1px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
cursor: default;
}
table.diff {
width: 100%;
table-layout: fixed;
background-color: #fdd;
}
table.diff-contents td > span.non-printable {
table.diff-contents td > span.ctrl-char {
visibility: hidden;
color: white;
text-transform: uppercase;
float: none;
text-align: center;
display: inline-block;
padding: 1px 1px 0px 1px;
margin-left: 1px;
margin-right: 1px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
cursor: default;
vertical-align: 10%;
}
table.diff-contents td:hover > span.non-printable {
table.diff-contents td:hover > span.ctrl-char {
visibility: visible;
}
table.diff-contents td.added > span.non-printable {
table.diff-contents td.added > span.ctrl-char {
background: #0A0;
}
table.diff-contents td.removed > span.non-printable {
table.diff-contents td.removed > span.ctrl-char {
background: #A00;
}
table.diff-contents td.context > span.non-printable {
background: black;
}
/* override prettify css rule */
table.diff-contents td > span.non-printable > * {
table.diff-contents td > span.ctrl-char > * {
color: white;
}
padding-left: 5px;
}
table.code td.code span.ctrl-char {
visibility: hidden;
}
table.code td.code:hover span.ctrl-char {
visibility: visible;
}
table.code td.code-lc {
text-align: right;
padding: 1px 5px;

Archive Download the corresponding diff file

Page rendered in 0.09025s using 13 queries.