#!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp using System.IO; using System.Reflection; using System.Text; using System.Web; LoadAssembly ("System.Web.dll"); Func ParseDiff = (string diff) => { StringBuilder result = new StringBuilder (diff.Length); string line; int old_ln = 0, new_ln = 0; int old_d = 0, new_d = 0; bool started = false; result.Append ("
"); using (StringReader reader = new StringReader (diff)) { while ((line = reader.ReadLine ()) != null) { if (line.StartsWith ("diff --git")) { // New file if (started) result.AppendLine (""); started = true; result.AppendLine (""); var fn = line.Substring (10).Trim ().Split (' ') [0]; var fn_split = fn.Split ('/').ToList (); fn_split.RemoveAt (0); fn_split.RemoveAt (0); fn = string.Join ("/", fn_split); result.AppendFormat ("\n", fn); } else if (line.StartsWith ("index")) { // Not sure what this is result.AppendFormat ("\n", line); } else if (line.StartsWith ("---") || line.StartsWith ("+++")) { // Ignore this for now // style = "background-color: white"; // result.AppendFormat ("", line, style); } else if (line.StartsWith ("@@")) { // line numbers string [] nl = line.Replace ("@@", "").Trim ().Split (' '); var oldc = nl [0].IndexOf (','); var newc = nl [1].IndexOf (','); old_ln = int.Parse (nl [0].Substring (1, oldc > 0 ? oldc - 1 : nl [0].Length - 1)); new_ln = int.Parse (nl [1].Substring (1, newc > 0 ? newc - 1 : nl [1].Length - 1)); result.AppendFormat ("\n", line); } else { string cl; if (line.StartsWith ("-")) { cl = "diff_view_removed_line"; old_d = 1; new_d = 0; } else if (line.StartsWith ("+")) { cl = "diff_view_added_line"; old_d = 0; new_d = 1; } else { cl = "diff_view_normal_line"; old_d = 1; new_d = 1; } result.AppendFormat ("\n", HttpUtility.HtmlEncode (line), cl, old_d == 0 ? string.Empty : old_ln.ToString (), new_d == 0 ? string.Empty : new_ln.ToString ()); old_ln += old_d; new_ln += new_d; } } } result.AppendLine ("
{0}
 
 
{0}
{0}
 
 
{0}
{2}
{3}
{0}
"); result.AppendLine ("
"); return result.ToString (); }; var args = Environment.GetCommandLineArgs (); if (args.Length != 4 /* 2 default + 2 real */) { // first arg is "/Library/Frameworks/Mono.framework/Versions/4.8.0/lib/mono/4.5/csharp.exe" // second arg the script itself // then comes the ones we care about Console.WriteLine ("Need 2 arguments, got {0}", args.Length - 2); Environment.Exit (1); return; } var input = args [2]; var output = args [3]; var diff = ParseDiff (File.ReadAllText (input)); diff = @" Raw diff
" + diff + @" "; File.WriteAllText (output, diff); Environment.Exit (0);