Esempio n. 1
0
        string PrepareDifAsync(string before, string after)
        {
            var sb   = new StringBuilder();
            var diff = _inlineDiffBuilder.BuildDiffModel(before, after);

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    sb.Append("<div class=\"inserted-line\">");
                    sb.Append(line.Text);
                    sb.Append("</div>");
                    break;

                case ChangeType.Deleted:
                    sb.Append("<div class=\"deleted-line\">");
                    sb.Append(line.Text);
                    sb.Append("</div>");
                    break;

                case ChangeType.Imaginary:
                    sb.Append("<div class=\"imaginary-line\">");
                    sb.Append(line.Text);
                    sb.Append("</div>");
                    break;

                case ChangeType.Modified:
                    foreach (var character in line.SubPieces)
                    {
                        if (character.Type == ChangeType.Imaginary)
                        {
                            continue;
                        }
                        sb.Append("<span class=\"")
                        .Append(character.Type.ToString().ToLower())
                        .Append("-character\">")
                        .Append(character.Text)
                        .Append("</span>");
                    }
                    break;

                default:
                    sb.Append(line.Text);
                    break;
                }
            }

            return(sb.ToString());
        }
Esempio n. 2
0
        public void Compare()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            var      compareModel = diffBuilder.BuildDiffModel(this._oldText, this._newText);
            TimeSpan ts           = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            /*   for (int i = 0; i < compareModel.NewText.Lines.Count; i++)
             * {
             *     if (compareModel.NewText.Lines[i].Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted)
             *     {
             *         compareModel.NewText.Lines[i].Text = "<span class=\"del diff\">" + compareModel.NewText.Lines[i].Text + "</span>";
             *     }
             *     else if (compareModel.NewText.Lines[i].Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted)
             *     {
             *         compareModel.NewText.Lines[i].Text = "<span class=\"ins diff\">" + compareModel.NewText.Lines[i].Text + "</span>";
             *     }
             *     else if (compareModel.NewText.Lines[i].Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified)
             *     {
             *         compareModel.NewText.Lines[i].Text = "<span class=\"mod diff\">" + compareModel.NewText.Lines[i].Text + "</span>";
             *     }
             * }*/

            var htmlTextSplitted = this._originalHtml.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            for (int i = compareModel.Lines.Count - 1; i >= 0; i--)
            {
                try
                {
                    var currentLine = compareModel.Lines[i];
                    if (currentLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted)
                    {
                        htmlTextSplitted[i] = "<span class=\"ins diff\">" + htmlTextSplitted[i] + "</span>";
                    }
                    else if (currentLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted)
                    {
                        htmlTextSplitted[i] = "<span class=\"del diff\">" + htmlTextSplitted[i] + "</span>";
                    }
                    else if (currentLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified)
                    {
                        htmlTextSplitted[i] = "<span class=\"mod diff\">" + htmlTextSplitted[i] + "</span>";
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

            // var changedCompareLines = compareModel.Lines.Where(l => l.Type != DiffPlex.DiffBuilder.Model.ChangeType.Unchanged).Select(l.);

            /* foreach (var line in changedCompareLines)
             * {
             *   if (line.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted)
             *   {
             *       line.Text = "<span class=\"del\">" + line.Text + "</span>";
             *   }
             *   else if (line.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted)
             *   {
             *       line.Text = "<span class=\"ins\">" + line.Text + "</span>";
             *   }
             * }*/

            //  this._html = String.Join("", compareModel.Lines.Select(l => l.Text));
            //  this._html = String.Join("", compareModel.NewText.Lines.Select(l => l.Text));
            this._html      = String.Join(Environment.NewLine, htmlTextSplitted);
            this._diffCount = compareModel.Lines.Where(l => l.Type != DiffPlex.DiffBuilder.Model.ChangeType.Unchanged && l.Type != DiffPlex.DiffBuilder.Model.ChangeType.Imaginary).Count();

            /* HtmlDiff.HtmlDiff diffHelper = new HtmlDiff.HtmlDiff(this._oldXml, this._newXml);
             * //this._html = diffHelper.Build();
             *
             * this._html = Regex.Replace(diffHelper.Build(), @"<diff class='ins'>(\s*)</diff>", "");
             *
             * string pattern = @"<diff ";
             * Regex rgx = new Regex(pattern);
             * this._diffCount = rgx.Matches(this.Html).Count;*/
        }