我正在使用 diff-lcs gem 输出两个 html 内容主体之间的差异。这是示例内容。
版本一:
<p>Paragraph one. Sentence one.</p>
<p>Paragraph two. Another sentence.</p>
<p>Paragraph three. I dare you to change me!</p>
版本二:
<p>Paragraph one. Sentence two.</p>
<p>Paragraph two. Another sentence.</p>
<p>Paragraph three. I dare you to update me!</p>
使用这个:
seq1 = @versionOne.body
seq2 = @versionTwo.body
seq = Diff::LCS.diff(seq1, seq2)
你得到这个怪物:
seq => [[#<Diff::LCS::Change:0x0000000be539f8 @action="-", @position=27, @element="t">, #<Diff::LCS::Change:0x0000000be538b8 @action="-", @position=28, @element="w">], [#<Diff::LCS::Change:0x0000000be53520 @action="+", @position=28, @element="n">, #<Diff::LCS::Change:0x0000000be53408 @action="+", @position=29, @element="e">], [#<Diff::LCS::Change:0x0000000be3aa70 @action="-", @position=110, @element="u">, #<Diff::LCS::Change:0x0000000be3a840 @action="-", @position=111, @element="p">, #<Diff::LCS::Change:0x0000000be34ee0 @action="-", @position=112, @element="d">, #<Diff::LCS::Change:0x0000000be349e0 @action="+", @position=110, @element="c">, #<Diff::LCS::Change:0x0000000be348a0 @action="+", @position=111, @element="h">], [#<Diff::LCS::Change:0x0000000be34580 @action="-", @position=114, @element="t">, #<Diff::LCS::Change:0x0000000be34210 @action="+", @position=113, @element="n">, #<Diff::LCS::Change:0x0000000be33f40 @action="+", @position=114, @element="g">], [#<Diff::LCS::Change:0x0000000be331d0 @action="-", @position=124, @element="">]]
文档中的输出sdiff
和其他方法同样令人恐惧。我了解数组(数组)的结构,但必须有一种简单的方法以人类可读和可样式化的方式显示差异。
PS - 如果有人想创建一个diff-lcs
标签,那将不胜感激。