问题描述
我正在使用diff-lcs gem输出html内容的两个正文之间的差异.这是示例内容.
I'm using the diff-lcs gem to output a difference between two bodies of html content. Here's the sample content.
版本一:
<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
的输出以及在文档中找到的其他方法同样令人恐惧.我了解(数组的)数组的结构,但是必须有一种简单的方法来以人类可读和可样式化的方式显示差异.
The outputs of sdiff
and other methods found in the documentation are similarly horrifying. I understand the structure of the array (of arrays) but there must be a simple way to show differences in a human readable and style-able manner.
PS-如果有人要创建diff-lcs
标签,将不胜感激.
PS - If someone wants to create a diff-lcs
tag, that would be appreciated.
推荐答案
我给diff-lcs提供的内容是一个常规字符串-字符数组.如果我想要的是字符比较,那么我得到了想要的东西,但是我想要更易读的东西-单词,线条或句子的比较.我选择了句子.
What I was feeding diff-lcs was a regular string - an array of characters. If what I wanted was a compare of characters, I got what I wanted, but I wanted something more readable - a compare of words, lines or sentences. I chose sentences.
seq1 = @versionOne.body.split('.')
seq2 = @versionTwo.body.split('.')
compareDiff = Diff::LCS.sdiff(seq1, seq2)
这产生了更具可读性和可解析性的内容.实际上,我还要将!
和?
分开.但是,该结构不是数组或哈希的常规数组.浏览器中的输出使我失望,但这是一个对象数组,您可以像其他任何东西一样解析它.这是我在rails控制台中获得的YAML格式的输出(不知道为什么它没有在浏览器中显示):
This produced much more readable and parse-able content. Realistically, I'll also want to split by !
and ?
. The structure however is not a normal array of arrays or hash. The output in the browser threw me off but it's an array of objects and you can parse it like anything else. This is the YAML formatted output I got in the rails console (no idea why it didn't show this in the browser):
---
- !ruby/object:Diff::LCS::ContextChange
action: "="
new_element: <p>Paragraph one
new_position: 0
old_element: <p>Paragraph one
old_position: 0
- !ruby/object:Diff::LCS::ContextChange
action: "!"
new_element: " Sentence two"
new_position: 1
old_element: " Sentence one"
old_position: 1
- !ruby/object:Diff::LCS::ContextChange
action: "="
new_element: |-
</p>
<p>Paragraph two
new_position: 2
old_element: |-
</p>
<p>Paragraph two
old_position: 2
- !ruby/object:Diff::LCS::ContextChange
action: "="
new_element: " Another sentence"
new_position: 3
old_element: " Another sentence"
old_position: 3
- !ruby/object:Diff::LCS::ContextChange
action: "="
new_element: |-
</p>
<p>Paragraph three
new_position: 4
old_element: |-
</p>
<p>Paragraph three
old_position: 4
- !ruby/object:Diff::LCS::ContextChange
action: "!"
new_element: " I dare you to update me!</p>"
new_position: 5
old_element: " I dare you to change me!</p>"
old_position: 5
=> nil
超级有帮助!这将输出类似wiki的差异:
Super helpful! This will output a wiki-like diff:
sdiff = Diff::LCS.sdiff(seq2, seq1)
diffHTML = ''
sdiff.each do |diff|
case diff.action
when '='
diffHTML << diff.new_element + "."
when '!'
# strip_tags only needed on old_element. removes pre-mature end tags.
diffHTML << "<del>#{diff.old_element.strip_tags}</del> <add>#{diff.new_element}</add>. "
end
end
@compareBody = diffHTML.html
...[format do block]
然后根据需要设置<del>
和<add>
的样式.如果您正在寻找更简单的方法,可以使用 diffy ,但是一旦您发现它就会非常灵活
Then just style <del>
and <add>
as you wish. If you're looking for something easier, diffy might be it, but this is very flexible once you figure it out.
这篇关于使diff-lcs的输出易于阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!