Extensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. using AngleSharp.Text;
  4. using SixLabors.ImageSharp.Drawing;
  5. namespace Masuit.Tools.TextDiff;
  6. public static partial class Extensions
  7. {
  8. internal static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T item)
  9. {
  10. foreach (var i in items)
  11. {
  12. yield return i;
  13. }
  14. yield return item;
  15. }
  16. internal static IEnumerable<T> ItemAsEnumerable<T>(this T item)
  17. {
  18. yield return item;
  19. }
  20. internal static void Splice<T>(this List<T> input, int start, int count, params T[] objects) => input.Splice(start, count, (IEnumerable<T>)objects);
  21. internal static void Splice<T>(this List<T> input, int start, int count, IEnumerable<T> objects)
  22. {
  23. input.RemoveRange(start, count);
  24. input.InsertRange(start, objects);
  25. }
  26. internal static IEnumerable<string> SplitBy(this string s, char separator)
  27. {
  28. StringBuilder sb = new();
  29. foreach (var c in s)
  30. {
  31. if (c == separator)
  32. {
  33. yield return sb.ToString();
  34. sb.Clear();
  35. }
  36. else
  37. {
  38. sb.Append(c);
  39. }
  40. }
  41. if (sb.Length > 0)
  42. {
  43. yield return sb.ToString();
  44. }
  45. }
  46. public static (string html1, string html2) HtmlDiff(this string text1, string text2)
  47. {
  48. if (string.IsNullOrWhiteSpace(text1) || string.IsNullOrWhiteSpace(text2))
  49. {
  50. return (text1, text2);
  51. }
  52. var regex = new Regex(@"<pre[\s\S]*?</pre>|<[^>]+>");
  53. const string sep = "\f";
  54. var tags1 = regex.Matches(text1).Select(m => m.Value).Append("").ToArray();
  55. var tags2 = regex.Matches(text2).Select(m => m.Value).Append("").ToArray();
  56. var html1 = regex.Replace(text1, sep);
  57. var html2 = regex.Replace(text2, sep);
  58. var diffs = TextDiffer.Compute(html1, html2);
  59. var s1 = diffs.Where(d => d.Operation != DiffOperation.Insert).Select(diff => diff.Operation == DiffOperation.Equal || string.IsNullOrWhiteSpace(diff.Text) ? diff.Text : diff.Text.Split(sep).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<del>{s}</del>").Join(sep)).Join("");
  60. var s2 = diffs.Where(d => d.Operation != DiffOperation.Delete).Select(diff => diff.Operation == DiffOperation.Equal || string.IsNullOrWhiteSpace(diff.Text) ? diff.Text : diff.Text.Split(sep).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<ins>{s}</ins>").Join(sep)).Join("");
  61. return (s1.Split(sep).Select((s, i) => s + tags1[i]).Join(""), s2.Split(sep).Select((s, i) => s + tags2[i]).Join(""));
  62. }
  63. public static string HtmlDiffMerge(this string text1, string text2)
  64. {
  65. if (string.IsNullOrWhiteSpace(text1))
  66. {
  67. return text2;
  68. }
  69. if (string.IsNullOrWhiteSpace(text2))
  70. {
  71. return text1;
  72. }
  73. var regex = new Regex(@"<pre[\s\S]*?</pre>|<[^>]+>");
  74. const string sep = "\f";
  75. var tags1 = regex.Matches(text1).Select(m => m.Value).Append("").ToQueue();
  76. var tags2 = regex.Matches(text2).Select(m => m.Value).Append("").ToQueue();
  77. var html1 = regex.Replace(text1, sep);
  78. var html2 = regex.Replace(text2, sep);
  79. var diffs = TextDiffer.Compute(html1, html2);
  80. return diffs.Select(diff =>
  81. {
  82. switch (diff.Operation)
  83. {
  84. case DiffOperation.Equal:
  85. {
  86. var str = diff.Text;
  87. foreach (Match m in Regex.Matches(str, sep))
  88. {
  89. tags1.Dequeue();
  90. var tag = tags2.Dequeue();
  91. str = str.ReplaceFirst(m.Value, tag);
  92. }
  93. return str;
  94. }
  95. case DiffOperation.Delete:
  96. {
  97. var str = diff.Text.Split(sep).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<del>{s}</del>").Join(sep);
  98. foreach (Match m in Regex.Matches(str, sep))
  99. {
  100. var tag = tags1.Dequeue();
  101. str = str.ReplaceFirst(m.Value, tag);
  102. }
  103. return str;
  104. }
  105. case DiffOperation.Insert:
  106. {
  107. var str = diff.Text.Split(sep).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<ins>{s}</ins>").Join(sep);
  108. foreach (Match m in Regex.Matches(str, sep))
  109. {
  110. var tag = tags2.Dequeue();
  111. str = str.ReplaceFirst(m.Value, tag);
  112. }
  113. return str;
  114. }
  115. default:
  116. throw new ArgumentOutOfRangeException();
  117. }
  118. }).Join("");
  119. }
  120. }