Example #1
0
        /// <summary>
        /// Convert a Diff list into a pretty HTML report.
        /// </summary>
        /// <param name="diffs"></param>
        /// <returns></returns>
        public static string ToHtmlDocument(this IEnumerable <Diff> diffs)
        {
            var textBuilder = new StringBuilder();

            textBuilder.AppendLine("<html>");
            textBuilder.AppendLine("<head>");
            textBuilder.AddDiffStyle();
            textBuilder.AppendLine("</head>");
            textBuilder.AppendLine("<body>");
            textBuilder.AppendLine();
            textBuilder.AppendLine(diffs.ToHtml(false));
            textBuilder.AppendLine();
            textBuilder.AppendLine("</body>");
            textBuilder.AppendLine("</html>");
            return(textBuilder.ToStringWithoutTrailingLine());
        }
Example #2
0
        /// <summary>
        /// Convert a Diff list into a pretty HTML snippet.
        /// </summary>
        /// <param name="diffs"></param>
        /// <param name="includeStyle"></param>
        /// <returns></returns>
        public static string ToHtml(this IEnumerable <Diff> diffs, bool includeStyle = true)
        {
            var textBuilder = new StringBuilder();

            if (includeStyle)
            {
                textBuilder.AddDiffStyle();
            }
            textBuilder.AppendLine("<!-- START OF DIFFS -->");
            textBuilder.AppendLine("<div class=\"diffContainer\">");

            var diffText = new StringBuilder();

            foreach (var aDiff in diffs)
            {
                var text = HttpUtility.HtmlEncode(aDiff.FormattedText).Replace("\n", "<br />\n");

                if (text.EndsWith("\n"))
                {
                    text = text.Substring(0, text.Length - 1);
                }

                switch (aDiff.Operation)
                {
                case Operation.Insert:
                    diffText.Append($"<ins class=\"diffInsert\">{text}</ins>");
                    break;

                case Operation.Delete:
                    diffText.Append($"<del class=\"diffDelete\">{text}</del>");
                    break;

                case Operation.Equal:
                    diffText.Append($"<span class=\"diffUnchanged\">{text}</span>");
                    break;

                default: throw new ArgumentException($"Invalid Operation: {aDiff.Operation}");
                }
            }

            textBuilder.AppendLine(diffText.ToStringWithoutTrailingLine());
            textBuilder.AppendLine("</div>");
            textBuilder.AppendLine("<!-- END OF DIFFS -->");
            return(textBuilder.ToStringWithoutTrailingLine());
        }
Example #3
0
        /// <summary>
        /// Convert a Patch list into a pretty HTML snippet.
        /// </summary>
        /// <param name="patches"></param>
        /// <param name="includeStyle"></param>
        /// <returns></returns>
        public static string ToHtml(this IEnumerable <Patch> patches, bool includeStyle = true)
        {
            var textBuilder = new StringBuilder();

            if (includeStyle)
            {
                textBuilder.AddDiffStyle();
                textBuilder.AddPatchStyle();
            }
            textBuilder.AppendLine("<!-- START OF PATCHES -->");

            var counter = 0;

            foreach (var patch in patches)
            {
                counter++;
                textBuilder.AppendLine("<div class=\"patchContainer\">");
                textBuilder.AppendLine("<div class=\"patchTitle\">");
                textBuilder.AppendLine($"<div>Patch number: {counter}</div>");
                textBuilder.AppendLine($"<div>Delete character coordinates: {patch.Coordinates1}</div>");
                textBuilder.AppendLine($"<div>Insert character coordinates: {patch.Coordinates2}</div>");
                if (counter > 1)
                {
                    textBuilder.AppendLine($"<div class=\"patchNote\">NOTE: Patch coordinates are based on patch {counter - 1} being applied</div>");
                }
                if (patch.Diffs.All(x => x.Operation == Operation.Equal || x.WhitespaceOnlyDiff))
                {
                    textBuilder.AppendLine("<div class=\"patchNote\">NOTE: Patch contains whitespace only differences</div>");
                }
                textBuilder.AppendLine("</div>");
                textBuilder.AppendLine("<div class=\"patchDiffs\">");
                textBuilder.AppendLine(patch.Diffs.ToHtml(false));
                textBuilder.AppendLine("</div>");
                textBuilder.AppendLine("</div>");
            }

            textBuilder.AppendLine("<!-- END OF PATCHES -->");
            return(textBuilder.ToStringWithoutTrailingLine());
        }