Created
June 13, 2012 02:17
-
-
Save aisuii/2921399 to your computer and use it in GitHub Desktop.
pre diff html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'rubygems' | |
require 'erb' | |
require 'differ' | |
MyFormat = Differ::Format::HTML.clone | |
module MyFormat | |
class << self | |
private | |
def as_change(change) | |
as_delete(change) << "\n" << as_insert(change) | |
end | |
end | |
end | |
def diff(prev_text, new_text) | |
prev, new = ERB::Util.h(prev_text), ERB::Util.h(new_text) | |
differ = Differ.diff_by_line(prev, new) | |
differ.format_as MyFormat | |
end | |
def diff_html(prev_text, new_text) | |
String.new(<<-HTML) | |
<html> | |
<head> | |
<title>diff</title> | |
<style> | |
ins.differ { | |
background: #cfc; | |
} | |
del.differ { | |
background: #fcc; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>diff</h1> | |
<pre class="diff"> | |
#{diff(prev_text, new_text)} | |
</pre> | |
</body> | |
</html> | |
HTML | |
end | |
if __FILE__ == $0 | |
a =<<-EOL | |
いろはにほへと | |
ちりぬるをわか | |
よたれそつねな | |
らむうゐのおく | |
やまけふこえて | |
あさきゆめみし | |
ゑひもせす | |
EOL | |
b =<<-EOL | |
いろはにほへど | |
ちりぬるをわが | |
よたれぞつねな | |
らむうゐのおく | |
やまけふこえて | |
あさきゆめみじ | |
ゑひもせず | |
EOL | |
print diff_html(a, b) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>diff</title> | |
<style> | |
ins.differ { | |
background: #cfc; | |
} | |
del.differ { | |
background: #fcc; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>diff</h1> | |
<pre class="diff"> | |
<del class="differ">いろはにほへど | |
ちりぬるをわが | |
よたれぞつねな</del> | |
<ins class="differ">いろはにほへと | |
ちりぬるをわか | |
よたれそつねな</ins> | |
らむうゐのおく | |
やまけふこえて | |
<del class="differ">あさきゆめみじ | |
ゑひもせず</del> | |
<ins class="differ">あさきゆめみし | |
ゑひもせす</ins> | |
</pre> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment