Class: Diff::LCS::HTMLDiff
- Inherits:
-
Object
- Object
- Diff::LCS::HTMLDiff
- 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
-
.can_expand_tabs ⇒ Object
standard:disable ThreadSafety/ClassAndModuleAttributes.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(left, right, options = nil) ⇒ HTMLDiff
constructor
standard:enable Layout/HeredocIndentation.
- #run ⇒ Object
Constructor Details
#initialize(left, right, options = nil) ⇒ HTMLDiff
standard:enable Layout/HeredocIndentation
96 97 98 99 100 101 102 |
# File 'lib/diff/lcs/htmldiff.rb', line 96 def initialize(left, right, = nil) @left = left @right = right @options = @options = DEFAULT_OPTIONS.dup if @options.nil? end |
Class Attribute Details
.can_expand_tabs ⇒ Object
standard:disable ThreadSafety/ClassAndModuleAttributes
9 10 11 |
# File 'lib/diff/lcs/htmldiff.rb', line 9 def @can_expand_tabs end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
116 117 118 |
# File 'lib/diff/lcs/htmldiff.rb', line 116 def @options end |
Instance Method Details
#run ⇒ Object
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 158 159 |
# File 'lib/diff/lcs/htmldiff.rb', line 118 def run if @options[:expand_tabs].positive? && self.class. formatter = Text::Format.new formatter.tabstop = @options[:expand_tabs] @left.map! { |line| formatter.(line.chomp) } @right.map! { |line| formatter.(line.chomp) } end @left.map! { |line| ERB::Util.html_escape(line.chomp) } @right.map! { |line| ERB::Util.html_escape(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> <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 |