-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathcode_samples_usage_diff2html.php
More file actions
80 lines (74 loc) · 2.6 KB
/
Copy pathcode_samples_usage_diff2html.php
File metadata and controls
80 lines (74 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Transform a unified diff between two code_sample_usage.php outputs into a side-by-side HTML table.
*
* source_length=`wc -l < $HOME/code_samples_usage_source.txt`
* target_length=`wc -l < $HOME/code_samples_usage_target.txt`
* diff -U $(( source_length > target_length ? source_length : target_length )) $HOME/code_samples_usage_target.txt $HOME/code_samples_usage_source.txt > $HOME/code_samples_usage.diff
* php tools/code_samples/code_sample_usage_diff2html.php $HOME/code_samples_usage.diff > code_samples_usage.diff.html
*/
$diffFile = $argv[1];
$diffFileContents = file($diffFile, FILE_IGNORE_NEW_LINES);
//echo '<meta charset="UTF-8">';
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th scope="col">Before (on target branch)</th>';
echo '<th scope="col">After (in current PR)</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
$previousStatusChar = '';
$leftTargetBlock = '';
$rightSourceBlock = '';
foreach ($diffFileContents as $diffLineIndex => $diffLine) {
if (3 > $diffLineIndex) {
// Skip metadata
continue;
}
$statusChar = strlen($diffLine) ? $diffLine[0] : '';
$realLine = str_replace(['<', '>'], ['<', '>'], substr($diffLine, 1));
if ($previousStatusChar !== $statusChar) {
switch ("$previousStatusChar$statusChar") {
case ' +':
case ' -':
case '+':
case '-':
case '+ ':
case '- ':
echo "<td><pre>$leftTargetBlock</pre></td><td><pre>$rightSourceBlock</pre></td>";
$leftTargetBlock = '';
$rightSourceBlock = '';
echo '</tr><tr>';
break;
case ' ':
case '-+':
case '+-':
break;
default:
throw new \RuntimeException("Unknown leading status characters transition: '$previousStatusChar$statusChar'");
}
}
switch ($statusChar) {
case '':
case ' ':
$leftTargetBlock .= "<code>$realLine</code><br>";
$rightSourceBlock .= "<code>$realLine</code><br>";
break;
case '-':
$leftTargetBlock .= "<code>$realLine</code><br>";
break;
case '+':
$rightSourceBlock .= "<code>$realLine</code><br>";
break;
default:
throw new \RuntimeException("Unknown leading status character: '$statusChar'");
}
$previousStatusChar = $statusChar;
}
echo "<td><pre>$leftTargetBlock</pre></td><td><pre>$rightSourceBlock</pre></td>";
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo PHP_EOL;