1- #!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp -s
2-
31using System . IO ;
4- using System.Reflection ;
2+ using System . Net ;
53using System . Text ;
6- using System.Web;
7-
8- LoadAssembly ("System.Web.dll");
94
10- Func<string, string> ParseDiff = (string diff) =>
11- {
12- StringBuilder result = new StringBuilder (diff.Length);
13- string line;
14- int old_ln = 0, new_ln = 0;
15- int old_d = 0, new_d = 0;
16- bool started = false;
17- result.Append ("<div>");
18- using (StringReader reader = new StringReader (diff)) {
19- while ((line = reader.ReadLine ()) != null) {
20- if (line.StartsWith ("diff --git")) {
21- // New file
22- if (started)
23- result.AppendLine ("</table>");
24- started = true;
25- result.AppendLine ("<table class='diff_view_table'>");
26- var fn = line.Substring (10).Trim ().Split (' ') [0];
27- var fn_split = fn.Split ('/').ToList ();
28- fn_split.RemoveAt (0);
29- fn_split.RemoveAt (0);
30- fn = string.Join ("/", fn_split);
31-
32- result.AppendFormat ("<tr><td class='diff_view_header_td' colspan='3'>{0}</td></tr>\n", fn);
33- } else if (line.StartsWith ("index")) {
34- // Not sure what this is
35- result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_index_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
36- } else if (line.StartsWith ("---") || line.StartsWith ("+++")) {
37- // Ignore this for now
38- // style = "background-color: white";
39- // result.AppendFormat ("<tr style='{1}'><td colspan='3'>{0}</td></tr>", line, style);
40- } else if (line.StartsWith ("@@")) {
41- // line numbers
42- string [] nl = line.Replace ("@@", "").Trim ().Split (' ');
43- var oldc = nl [0].IndexOf (',');
44- var newc = nl [1].IndexOf (',');
45- old_ln = int.Parse (nl [0].Substring (1, oldc > 0 ? oldc - 1 : nl [0].Length - 1));
46- new_ln = int.Parse (nl [1].Substring (1, newc > 0 ? newc - 1 : nl [1].Length - 1));
47- result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_at_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
48- } else {
49- string cl;
50- if (line.StartsWith ("-")) {
51- cl = "diff_view_removed_line";
52- old_d = 1;
53- new_d = 0;
54- } else if (line.StartsWith ("+")) {
55- cl = "diff_view_added_line";
56- old_d = 0;
57- new_d = 1;
58- } else {
59- cl = "diff_view_normal_line";
60- old_d = 1;
61- new_d = 1;
62- }
63- result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>{2}</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>{3}</pre></td><td class='{1}'><pre class='diff_view_pre'>{0}</pre></td></tr>\n",
64- HttpUtility.HtmlEncode (line), cl, old_d == 0 ? string.Empty : old_ln.ToString (), new_d == 0 ? string.Empty : new_ln.ToString ());
65- old_ln += old_d;
66- new_ln += new_d;
67- }
68- }
5+ try {
6+ var expectedArgumentCount = 2 ;
7+ if ( args . Length != expectedArgumentCount ) {
8+ Console . WriteLine ( $ "Need { expectedArgumentCount } arguments, got { args . Length } arguments") ;
9+ return 1 ;
6910 }
70- result.AppendLine ("</table>");
71- result.AppendLine ("</div>");
72- return result.ToString ();
73- };
74-
75- var args = Args;
76- var expectedArgumentCount = 2;
77- if (args.Length != expectedArgumentCount) {
78- Console.WriteLine ($"Need {expectedArgumentCount} arguments, got {args.Length}");
79- Environment.Exit (1);
80- return;
81- }
8211
83- var idx = 0;
84- var input = args [idx++];
85- var output = args [idx++];
12+ var input = args [ 0 ] ;
13+ var output = args [ 1 ] ;
8614
87- var diff = ParseDiff (File.ReadAllText (input));
88- diff = @"
15+ var diff = ParseDiff ( File . ReadAllText ( input ) ) ;
16+ diff = @"
8917<html>
9018<head>
9119<style>
9220/* diff view */
9321
94- .diff_view_line_number
22+ .diff_view_line_number
9523{
9624 background-color: #ececec;
9725 text-align: right;
10634 padding-right: 5px;
10735}
10836
109- .diff_view_normal_line
37+ .diff_view_normal_line
11038{
11139 background-color: white;
11240 width: 100%;
11341}
11442
115- .diff_view_removed_line
43+ .diff_view_removed_line
11644{
11745 background-color: #ffd0d0;
11846 width: 100%;
11947}
12048
121- .diff_view_added_line
49+ .diff_view_added_line
12250{
12351 background-color: #d0ffd0;
12452 width: 100%;
13361 border-bottom-width: 1px;
13462 border-right-width: 0px;
13563 border-right-color: #777777;
136- padding-top: 5px;
64+ padding-top: 5px;
13765 padding-bottom: 5px;
138- padding-left: 5px;
66+ padding-left: 5px;
13967 padding-right: 5px;
14068}
14169
14270td.diff_view_td
14371{
14472 border-width: 0px;
145- padding-top: 0px;
73+ padding-top: 0px;
14674 padding-bottom: 0px;
147- padding-left: 5px;
75+ padding-left: 5px;
14876 padding-right: 5px;
14977}
15078
15179td.diff_view_at_td
15280{
15381 background-color: #f0f0f0;
15482 border-width: 0px;
155- padding-top: 0px;
83+ padding-top: 0px;
15684 padding-bottom: 0px;
157- padding-left: 5px;
85+ padding-left: 5px;
15886 padding-right: 5px;
15987}
16088
16189td.diff_view_index_td
16290{
16391 background-color: #f0f0f0;
16492 border-width: 0px;
165- padding-top: 0px;
93+ padding-top: 0px;
16694 padding-bottom: 0px;
167- padding-left: 5px;
95+ padding-left: 5px;
16896 padding-right: 5px;
16997}
17098
181109 margin-top: 5px;
182110 margin-bottom: 5px;
183111 width: 100%;
184- border-width: 1px;
185- border-style: outset;
112+ border-width: 1px;
113+ border-style: outset;
186114 border-color: #808080;
187115 border-collapse: separate;
188116}
193121" + diff + @"
194122</html>
195123" ;
196- File.WriteAllText (output, diff);
124+ File . WriteAllText ( output , diff ) ;
125+
126+ return 0 ;
127+ } catch ( Exception e ) {
128+ Console . WriteLine ( "Failed: {0}" , e ) ;
129+ return 1 ;
130+ }
197131
198- Environment.Exit (0);
132+ static string ParseDiff ( string diff )
133+ {
134+ StringBuilder result = new StringBuilder ( diff . Length ) ;
135+ string ? line ;
136+ int old_ln = 0 , new_ln = 0 ;
137+ int old_d = 0 , new_d = 0 ;
138+ bool started = false ;
139+ result . Append ( "<div>" ) ;
140+ using ( StringReader reader = new StringReader ( diff ) ) {
141+ while ( ( line = reader . ReadLine ( ) ) is not null ) {
142+ if ( line . StartsWith ( "diff --git" ) ) {
143+ // New file
144+ if ( started )
145+ result . AppendLine ( "</table>" ) ;
146+ started = true ;
147+ result . AppendLine ( "<table class='diff_view_table'>" ) ;
148+ var fn = line . Substring ( 10 ) . Trim ( ) . Split ( ' ' ) [ 0 ] ;
149+ var fn_split = fn . Split ( '/' ) . ToList ( ) ;
150+ fn_split . RemoveAt ( 0 ) ;
151+ fn_split . RemoveAt ( 0 ) ;
152+ fn = string . Join ( "/" , fn_split ) ;
153+
154+ result . AppendFormat ( "<tr><td class='diff_view_header_td' colspan='3'>{0}</td></tr>\n " , fn ) ;
155+ } else if ( line . StartsWith ( "index" ) ) {
156+ // Not sure what this is
157+ result . AppendFormat ( "<tr><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_index_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n " , line ) ;
158+ } else if ( line . StartsWith ( "---" ) || line . StartsWith ( "+++" ) ) {
159+ // Ignore this for now
160+ } else if ( line . StartsWith ( "@@" ) ) {
161+ // line numbers
162+ string [ ] nl = line . Replace ( "@@" , "" ) . Trim ( ) . Split ( ' ' ) ;
163+ var oldc = nl [ 0 ] . IndexOf ( ',' ) ;
164+ var newc = nl [ 1 ] . IndexOf ( ',' ) ;
165+ old_ln = int . Parse ( nl [ 0 ] . Substring ( 1 , oldc > 0 ? oldc - 1 : nl [ 0 ] . Length - 1 ) ) ;
166+ new_ln = int . Parse ( nl [ 1 ] . Substring ( 1 , newc > 0 ? newc - 1 : nl [ 1 ] . Length - 1 ) ) ;
167+ result . AppendFormat ( "<tr><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'> </pre></td><td class='diff_view_at_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n " , line ) ;
168+ } else {
169+ string cl ;
170+ if ( line . StartsWith ( "-" ) ) {
171+ cl = "diff_view_removed_line" ;
172+ old_d = 1 ;
173+ new_d = 0 ;
174+ } else if ( line . StartsWith ( "+" ) ) {
175+ cl = "diff_view_added_line" ;
176+ old_d = 0 ;
177+ new_d = 1 ;
178+ } else {
179+ cl = "diff_view_normal_line" ;
180+ old_d = 1 ;
181+ new_d = 1 ;
182+ }
183+ result . AppendFormat ( "<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>{2}</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>{3}</pre></td><td class='{1}'><pre class='diff_view_pre'>{0}</pre></td></tr>\n " ,
184+ WebUtility . HtmlEncode ( line ) , cl , old_d == 0 ? string . Empty : old_ln . ToString ( ) , new_d == 0 ? string . Empty : new_ln . ToString ( ) ) ;
185+ old_ln += old_d ;
186+ new_ln += new_d ;
187+ }
188+ }
189+ }
190+ result . AppendLine ( "</table>" ) ;
191+ result . AppendLine ( "</div>" ) ;
192+ return result . ToString ( ) ;
193+ }
0 commit comments