daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Oct 19th, 2018 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2.  
  3. require 'rubygems'
  4. require 'erb'
  5. require 'differ'
  6.  
  7.  
  8. MyFormat = Differ::Format::HTML.clone
  9.  
  10. module MyFormat
  11.   class << self
  12.     private
  13.     def as_change(change)
  14.       as_delete(change) << "\n" << as_insert(change)
  15.     end
  16.   end
  17. end
  18.  
  19. def diff(prev_text, new_text)
  20.   prev, new = ERB::Util.h(prev_text), ERB::Util.h(new_text)
  21.   differ = Differ.diff_by_line(prev, new)
  22.   differ.format_as MyFormat
  23. end
  24.  
  25. def diff_html(prev_text, new_text)
  26.   String.new(<<-HTML)
  27. <html>
  28. <head>
  29. <title>diff</title>
  30. <style>
  31. ins.differ {
  32.   background: #cfc;
  33. }
  34. del.differ {
  35.   background: #fcc;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <h1>diff</h1>
  41. <pre class="diff">
  42. #{diff(prev_text, new_text)}
  43. </pre>
  44. </body>
  45. </html>
  46.   HTML
  47. end
  48.  
  49. if __FILE__ == $0
  50.  
  51. a =<<-EOL
  52. いろはにほへと
  53. ちりぬるをわか
  54. よたれそつねな
  55. らむうゐのおく
  56. やまけふこえて
  57. あさきゆめみし
  58. ゑひもせす
  59. EOL
  60.  
  61. b =<<-EOL
  62. いろはにほへど
  63. ちりぬるをわが
  64. よたれぞつねな
  65. らむうゐのおく
  66. やまけふこえて
  67. あさきゆめみじ
  68. ゑひもせず
  69. EOL
  70.  
  71. print diff_html(a, b)
  72. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top