-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Would like to embed diff-lcs such that it yields a html diff as a string #41
Comments
So…I’m not 100% sure, to be honest. I don't see adding an |
Just to explain the context. I have a bunch of text snippets ( in particular a bunch of requirement spec items), and I want to generate an HTML file which shows the difference between the two versions as a table with one row per spec item. The styling of the difference is done by css. I don't care if it uses li or div or whatever as long as I can style it properly. As of now, I use, what Diffy provides. I managed to somehow use
so I think I will try to add another method to Diffy (Diffy.diff_noshell) and replace the code that calls the external diff command by some code from ldiff.rb. There is a related issue there samg/diffy#63. So we can close this if you do not want to provide a htmldiff method. |
I’m not against the idea of an |
I went in to the thing again and found a solution which works for me. As it is not generic I cannot prepare a pull request. describe "arhtmldiff" do
it "calls sdiff" do
require 'diff/lcs'
class Callbacks
attr_accessor :output
def initialize(output, options = {})
@output = output
@state = :init
options ||= {}
@styles={
ins: "text-decoration: underline;color:blue;",
del: "text-decoration: line-through;color:red;",
eq: ""
}
end
def to_html(element)
element #.gsub(/./, {'<' => '<', '>' => '>', '&' => '&'})
end
def handle_entry(element, state)
#binding.pry
unless @state == state
@output.push "</span>" unless @state == :init
@state = state
@output.push %Q{<span style="#{@styles[state]}">}
end
@output.push(to_html(element))
end
private :handle_entry
# This will be called with both lines are the same
def match(event)
handle_entry(event.old_element, :eq)
end
# This will be called when there is a line in A that isn't in B
def discard_a(event)
handle_entry(event.old_element, :del)
end
# This will be called when there is a line in B that isn't in A
def discard_b(event)
handle_entry(event.new_element, :ins)
end
end
data_old = %q{
foo
bar}
data_new = %q{
boo
far
and more
}
output = []
output.push %Q{<pre>}
callback_obj = Callbacks.new(output)
xx = Diff::LCS.traverse_sequences(data_old, data_new, callback_obj)
output.push %Q{</pre>}
puts output.join
expect(output.join).to eq %q{<pre><span style="">
</span><span style="text-decoration: line-through;color:red;">f</span><span style="text-decoration: underline;color:blue;">b</span><span style="">oo
</span><span style="text-decoration: line-through;color:red;">b</span><span style="text-decoration: underline;color:blue;">f</span><span style="">ar</span><span style="text-decoration: underline;color:blue;">
and more
</pre>}
end
end |
Thanks. I’ll look this over to see if I can extract something common. |
Thanks @bwl21 - that was very useful! |
I currently use
But diffy calls another executable. In our case we use ldiff as provided by liff-lcs.
I wanted to achieve something like
which should yield
"<div class=\"diff\">\n <ul>\n <li class=\"del\"><del>foo</del></li>\n <li class=\"ins\"><ins>foo<strong>bar</strong></ins></li>\n </ul>\n</div>\n"
How can I achieve this?
The text was updated successfully, but these errors were encountered: