Class: Diff::LCS::HTMLDiff

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/diff-lcs-1.5.0/lib/diff/lcs/htmldiff.rb

Overview

Produce a simple HTML diff view.

Defined Under Namespace

Classes: Callbacks

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :expand_tabs => nil,
  :output => nil,
  :css => nil,
  :title => nil
}.freeze
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.



90
91
92
93
94
95
96
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/diff-lcs-1.5.0/lib/diff/lcs/htmldiff.rb', line 90

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/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/diff-lcs-1.5.0/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.



110
111
112
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/diff-lcs-1.5.0/lib/diff/lcs/htmldiff.rb', line 110

def options
  @options
end

Instance Method Details

#runObject



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
149
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/diff-lcs-1.5.0/lib/diff/lcs/htmldiff.rb', line 112

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! do |line| formatter.expand(line.chomp) end
    @right.map! do |line| formatter.expand(line.chomp) end
  end

  @left.map! do |line| CGI.escapeHTML(line.chomp) end
  @right.map! do |line| CGI.escapeHTML(line.chomp) end

  @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