Extensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if NETSTANDARD2_1_OR_GREATER
  55. var tags1 = regex.Matches(text1).Select(m => m.Value).Append("").ToArray();
  56. var tags2 = regex.Matches(text2).Select(m => m.Value).Append("").ToArray();
  57. #else
  58. var tags1 = regex.Matches(text1).Cast<Match>().Select(m => m.Value).Append("").ToArray();
  59. var tags2 = regex.Matches(text2).Cast<Match>().Select(m => m.Value).Append("").ToArray();
  60. #endif
  61. var html1 = regex.Replace(text1, sep);
  62. var html2 = regex.Replace(text2, sep);
  63. var diffs = TextDiffer.Compute(html1, html2);
  64. 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[0]).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<del>{s}</del>").Join(sep)).Join("");
  65. 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[0]).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<ins>{s}</ins>").Join(sep)).Join("");
  66. return (s1.Split(sep[0]).Select((s, i) => s + tags1[i]).Join(""), s2.Split(sep[0]).Select((s, i) => s + tags2[i]).Join(""));
  67. }
  68. public static string HtmlDiffMerge(this string text1, string text2)
  69. {
  70. if (string.IsNullOrWhiteSpace(text1))
  71. {
  72. return text2;
  73. }
  74. if (string.IsNullOrWhiteSpace(text2))
  75. {
  76. return text1;
  77. }
  78. var regex = new Regex(@"<pre[\s\S]*?</pre>|<[^>]+>");
  79. const string sep = "\f";
  80. #if NETSTANDARD2_1_OR_GREATER
  81. var tags1 = regex.Matches(text1).Select(m => m.Value).Append("").ToQueue();
  82. var tags2 = regex.Matches(text2).Select(m => m.Value).Append("").ToQueue();
  83. #else
  84. var tags1 = regex.Matches(text1).Cast<Match>().Select(m => m.Value).Append("").ToQueue();
  85. var tags2 = regex.Matches(text2).Cast<Match>().Select(m => m.Value).Append("").ToQueue();
  86. #endif
  87. var html1 = regex.Replace(text1, sep);
  88. var html2 = regex.Replace(text2, sep);
  89. var diffs = TextDiffer.Compute(html1, html2);
  90. return diffs.Select(diff =>
  91. {
  92. switch (diff.Operation)
  93. {
  94. case DiffOperation.Equal:
  95. {
  96. var str = diff.Text;
  97. foreach (Match m in Regex.Matches(str, sep))
  98. {
  99. tags1.Dequeue();
  100. var tag = tags2.Dequeue();
  101. str = str.ReplaceFirst(m.Value, tag);
  102. }
  103. return str;
  104. }
  105. case DiffOperation.Delete:
  106. {
  107. var str = diff.Text.Split(sep[0]).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<del>{s}</del>").Join(sep);
  108. foreach (Match m in Regex.Matches(str, sep))
  109. {
  110. var tag = tags1.Dequeue();
  111. str = str.ReplaceFirst(m.Value, tag);
  112. }
  113. return str;
  114. }
  115. case DiffOperation.Insert:
  116. {
  117. var str = diff.Text.Split(sep[0]).Select(s => string.IsNullOrWhiteSpace(s) ? s : $"<ins>{s}</ins>").Join(sep);
  118. foreach (Match m in Regex.Matches(str, sep))
  119. {
  120. var tag = tags2.Dequeue();
  121. str = str.ReplaceFirst(m.Value, tag);
  122. }
  123. return str;
  124. }
  125. default:
  126. throw new ArgumentOutOfRangeException();
  127. }
  128. }).Join("");
  129. }
  130. }