private string getTableBody(DiffContext ctx) { bool highlightSelected = ctx.Lines.Count() > 1; StringBuilder body = new StringBuilder(); int iLine = 0; foreach (DiffContext.Line line in ctx.Lines) { body.Append("<tr"); body.Append((iLine == ctx.SelectedIndex && highlightSelected ? " class=\"selected\"" : "")); body.Append(">"); body.Append("<td class=\"linenumbers\">"); body.Append(getLeftLineNumber(line)); body.Append("</td>"); body.Append("<td class=\"linenumbers\">"); body.Append(getRightLineNumber(line)); body.Append("</td>"); body.Append("<td class=\""); body.Append(getDiffCellClass(line)); body.Append("\">"); body.Append(getCode(line)); body.Append("</td>"); body.Append("</tr>"); ++iLine; } return(body.ToString()); }
// isRightSideContext is true when linenumber corresponds to the right side (sha2). // linenumber is one-based private DiffContext createDiffContext(int linenumber, bool isRightSideContext, FullContextDiff context, ContextDepth depth) { int startLineNumber = Math.Max(1, linenumber - depth.Up); int endLineNumber = linenumber + depth.Down; SparsedListIterator <string> itLeft = context.Left.Begin(); SparsedListIterator <string> itRight = context.Right.Begin(); if (isRightSideContext) { itRight = SparsedListUtils.FindNth(itRight, startLineNumber - 1); itLeft = SparsedListUtils.Advance(itLeft, itRight.Position); } else { itLeft = SparsedListUtils.FindNth(itLeft, startLineNumber - 1); itRight = SparsedListUtils.Advance(itRight, itLeft.Position); } DiffContext diffContext = new DiffContext { Lines = new List <DiffContext.Line>() }; int iContextLine = 0; while (true) { int?leftLineNumber = itLeft.LineNumber != null ? itLeft.LineNumber + 1 : null; int?rightLineNumber = itRight.LineNumber != null ? itRight.LineNumber + 1 : null; DiffContext.Line line = getLineContext(leftLineNumber, rightLineNumber, itLeft.Current, itRight.Current); diffContext.Lines.Add(line); if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber == linenumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber == linenumber)) { // zero-based index of a selected line in DiffContext.Lines diffContext.SelectedIndex = iContextLine; } if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber >= endLineNumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber >= endLineNumber)) { // we've just reached a line that should not be included in the context break; } if (!itLeft.Next() || !itRight.Next()) { // we've just reached the end break; } ++iContextLine; } return(diffContext); }
private static string getTable(DiffContext ctx) { string commonBegin = @" <table cellspacing=""0"" cellpadding=""0""> <tbody>"; string commonEnd = @" </tbody> </table>"; return(String.Format("{0} {1} {2}", commonBegin, getTableBody(ctx), commonEnd)); }
private string getTableBody(DiffContext ctx) { bool highlightSelected = ctx.Lines.Count > 1; string body = string.Empty; for (int iLine = 0; iLine < ctx.Lines.Count; ++iLine) { var line = ctx.Lines[iLine]; body += "<tr" + (iLine == ctx.SelectedIndex && highlightSelected ? " class=\"selected\"" : "") + ">" + "<td class=\"linenumbers\">" + getLeftLineNumber(line) + "</td>" + "<td class=\"linenumbers\">" + getRightLineNumber(line) + "</td>" + "<td class=\"" + getDiffCellClass(line) + "\">" + getCode(line) + "</td>" + "</tr>"; } return(body); }
private string getContextHTML(DiffContext ctx, int fontSizePx, int rowsVPaddingPx) { string customStyle = getCustomStyle(fontSizePx, rowsVPaddingPx); string commonBegin = string.Format(@" <html> <head> <style>{0}{1}</style> </head> <body> <table cellspacing=""0"" cellpadding=""0""> <tbody>", _css, customStyle); string commonEnd = @" </tbody> </table> </body> </html>"; return(commonBegin + getTableBody(ctx) + commonEnd); }
/// <summary> /// Throws ArgumentException if DiffContext is invalid /// </summary> public static string GetHtml(DiffContext context, double fontSizePx, int rowsVPaddingPx, bool fullWidth) { if (!context.IsValid()) { string errorMessage = "Cannot render HTML context."; return(String.Format("<html><body>{0} See logs for details</body></html>", errorMessage)); } return(String.Format( @"<html> <head> <style> {0} </style> </head> <body> {1} <body> </html>", getStylesheet(fontSizePx, rowsVPaddingPx, fullWidth), getTable(context))); }
// isRightSideContext is true when linenumber and sha correspond to the right side // linenumber is one-based private DiffContext createDiffContext(int linenumber, List <string> contents, bool isRightSideContext, ContextDepth depth) { DiffContext diffContext = new DiffContext { Lines = new List <DiffContext.Line>() }; int startLineNumber = Math.Max(1, linenumber - depth.Up); IEnumerable <string> shiftedContents = contents.Skip(startLineNumber - 1); foreach (string text in shiftedContents) { diffContext.Lines.Add(getContextLine(startLineNumber + diffContext.Lines.Count, isRightSideContext, text)); if (diffContext.Lines.Count == depth.Size + 1) { break; } } // zero-based index of a selected line in DiffContext.Lines diffContext.SelectedIndex = linenumber - startLineNumber; return(diffContext); }
/// <summary> /// Throws ArgumentException if DiffContext is invalid /// </summary> public string FormatAsHTML(DiffContext context, int fontSizePx = 12, int rowsVPaddingPx = 2) { return(getContextHTML(context, fontSizePx, rowsVPaddingPx)); }
/// <summary> /// Throws ArgumentException if DiffContext is invalid /// </summary> public string GetBody(DiffContext context) { return(getContextHTML(context)); }