Hatena::Groupsubtech

冬通りに消え行く制服ガールは✖夢物語にリアルを求めない。

 | 

Jun 26, 2007 (Tue)

diff memo 14:49 はてなブックマーク - diff memo - 冬通りに消え行く制服ガールは✖夢物語にリアルを求めない。

begin
require "rubygems"
rescue LoadError
end
require "diff/lcs"

s1 = <<EOS
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccc
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffff
EOS

s2 = <<EOS
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
aaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbb
dddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeee
fffffffffffffggggggggggggg

zzzzzzzzzzzzzzzzzzzzzzzzzz
zzzzzzzzzzzzzzzzzzzzzzzzzz
EOS

#require "pp"
#Diff::LCS.sdiff(s1.to_a, s2.to_a).each do |l|
#	case l.action
#	when "="
#		puts " " + l.old_element
#	when "!"
#		puts "-" + l.old_element
#		puts "+" + l.new_element
#	when "+"
#		puts "+" + l.new_element
#	when "-"
#		puts "-" + l.old_element
#	end
#	#pp l
#end

diffs = Diff::LCS.sdiff(s1.to_a, s2.to_a)
require "erb"

include  ERB::Util
File.open("test.html", "w") {|f| f.puts ERB.new(DATA.read, nil, "%>").result(binding) }

__END__
<style type="text/css">
.diff {
	line-height: 1.33;
	font-size: 90%;
	width: 100%;
}

.diff del {
	display: block;
	background: #fdd;
	text-decoration: none;
}

.diff ins {
	display: block;
	background: #dfd;
	text-decoration: none;
}

.diff th {
	color: #888866;
	background: #EEEEDD;
	text-align: right;
	padding: 0 0.5em;
	border-style: solid;
	border-width: 0 0 1px 1px;
	border-color: #998 #d7d7d7;
	width: 2em;
}

.diff td {
	font-family: monospace;
}
</style>
<table class="diff" cellspacing="0">
	<thead>
		<tr>
			<th>old</th>
			<th>new</th>
			<th>content</th>
		</tr>
	</thead>
	<tbody>
	<% diffs.each do |c| %>
		<% case c.action
		   when "=" %>
		<tr>
			<th><%=h c.old_position + 1 %></th>
			<th><%=h c.new_position + 1 %></th>
			<td><%=h c.new_element %></td>
		</tr>
		<% when "!" %>
		<tr>
			<th><%=h c.old_position + 1 %></th>
			<th></th>
			<td><del><%=h c.old_element%></del></td>
		</tr>
		<tr>
			<th></th>
			<th><%=h c.new_position + 1 %></th>
			<td><ins><%=h c.new_element%></ins></td>
		</tr>
		<% when "+" %>
		<tr>
			<th></th>
			<th><%=h c.new_position + 1 %></th>
			<td><ins><%=h c.new_element %></ins></td>
		</tr>
		<% when "-" %>
		<tr>
			<th><%=h c.old_position + 1 %></th>
			<th></th>
			<td><del><%=h c.old_element %></del></td>
		</tr>
		<% end %>
	<% end %>
	</tbody>
</table>

書くほどでもないなぁって思ってるといつのまにか失くしてしまう……

 | 

スポンサード リンク

書いてる人

cho45 (佐藤広央) (www.lowreal.net)

Perl, JavaScript, Ruby, HTML, CSS, Web etc


スポンサード リンク