Class: Diff::LCS::HTMLDiff

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

Overview

Produce a simple HTML diff view.

Defined Under Namespace

Classes: Callbacks

Constant Summary collapse

DEFAULT_OPTIONS =

standard:disable Style/HashSyntax

{
  :expand_tabs => nil,
  :output => nil,
  :css => nil,
  :title => nil
}.freeze
DEFAULT_CSS =

standard:disable Layout/HeredocIndentation

<<-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

standard:enable Layout/HeredocIndentation



94
95
96
97
98
99
100
# File 'lib/diff/lcs/htmldiff.rb', line 94

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:



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

def can_expand_tabs
  @can_expand_tabs
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



114
115
116
# File 'lib/diff/lcs/htmldiff.rb', line 114

def options
  @options
end

Instance Method Details

#runObject



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
149
150
151
152
153
154
155
156
157
# File 'lib/diff/lcs/htmldiff.rb', line 116

def run
  verify_options

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

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

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

  # standard:disable Layout/HeredocIndentation
  @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
  # standard:enable Layout/HeredocIndentation

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

  # standard:disable Layout/HeredocIndentation
  @options[:output] << <<-OUTPUT
  </div>
</body>
</html>
  OUTPUT
  # standard:enable Layout/HeredocIndentation
end