Class: Diff::LCS::HTMLDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/diff/lcs/htmldiff.rb

Defined Under Namespace

Classes: Callbacks

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :expand_tabs => nil,
  :output => nil,
  :css => nil,
  :title => nil,
}
DEFAULT_CSS =
<<-CSS
body { margin: 0; }
.diff
{
border: 1px solid black;
margin: 1em 2em;
}
p
{
margin-left: 2em;
}
pre
{
padding-left: 1em;
margin: 0;
font-family: Inconsolata, Consolas, Lucida, Courier, monospaced;
	white-space: pre;
}
.match { }
.only_a
{
background-color: #fdd;
color: red;
text-decoration: line-through;
}
.only_b
{
background-color: #ddf;
color: blue;
border-left: 3px solid blue
}
h1 { margin-left: 2em; }
CSS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, options = nil) ⇒ HTMLDiff

Returns a new instance of HTMLDiff.



89
90
91
92
93
94
95
# File 'lib/diff/lcs/htmldiff.rb', line 89

def initialize(left, right, options = nil)
  @left     = left
  @right    = right
  @options  = options

  @options = DEFAULT_OPTIONS.dup if @options.nil?
end

Class Attribute Details

.can_expand_tabsObject

:nodoc:



7
8
9
# File 'lib/diff/lcs/htmldiff.rb', line 7

def can_expand_tabs
  @can_expand_tabs
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



109
110
111
# File 'lib/diff/lcs/htmldiff.rb', line 109

def options
  @options
end

Instance Method Details

#runObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/diff/lcs/htmldiff.rb', line 111

def run
  verify_options

  if @options[:expand_tabs] > 0 && self.class.can_expand_tabs
    formatter = Text::Format.new
    formatter.tabstop = @options[:expand_tabs]

    @left = left.map { |line| formatter.expand(line.chomp) }
    @right = right.map { |line| formatter.expand(line.chomp) }
  end

  @left.map! { |line| CGI.escapeHTML(line.chomp) }
  @right.map! { |line| CGI.escapeHTML(line.chomp) }

  @options[:output] << <<-OUTPUT
<html>
<head>
  <title>#{@options[:title]}</title>
  <style type="text/css">
  #{@options[:css]}
  </style>
</head>
<body>
  <h1>#{@options[:title]}</h1>
  <p>Legend: <span class="only_a">Only in Old</span>&nbsp;
  <span class="only_b">Only in New</span></p>
  <div class="diff">
  OUTPUT

  callbacks = Callbacks.new(@options[:output])
  Diff::LCS.traverse_sequences(@left, @right, callbacks)

  @options[:output] << <<-OUTPUT
  </div>
</body>
</html>
  OUTPUT
end