pretty_diff 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/lib/pretty_diff/chunk.rb +7 -3
- data/lib/pretty_diff/diff.rb +9 -10
- data/lib/pretty_diff/html_generators/chunk_generator.rb +35 -0
- data/lib/pretty_diff/html_generators/diff_generator.rb +28 -0
- data/lib/pretty_diff/html_generators/line_generator.rb +43 -0
- data/lib/pretty_diff/html_generators/line_numbers_generator.rb +28 -0
- data/lib/pretty_diff/line.rb +10 -2
- data/lib/pretty_diff/line_numbers.rb +13 -9
- data/lib/pretty_diff.rb +14 -6
- data/pretty_diff.gemspec +6 -3
- data/test/chunk_test.rb +1 -0
- metadata +6 -3
- data/lib/pretty_diff/html_generator.rb +0 -91
data/README.rdoc
CHANGED
@@ -20,7 +20,7 @@ http://ilya.sabanin.ru/projects/pretty_diff_example.html
|
|
20
20
|
|
21
21
|
== Features
|
22
22
|
|
23
|
-
The library is really easy to read and change. All HTML is
|
23
|
+
The library is really easy to read and change. All HTML is generated by a set of classes called generators, it's very easy to customize the output to suit your needs.
|
24
24
|
|
25
25
|
By default PrettyDiff will generate HTML that can be easily colored with the CSS that you can find in the example above.
|
26
26
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/lib/pretty_diff/chunk.rb
CHANGED
@@ -13,11 +13,9 @@ class PrettyDiff::Chunk #:nodoc:
|
|
13
13
|
def to_html
|
14
14
|
# We have to find lines before we can call line numbers methods.
|
15
15
|
find_lines!
|
16
|
-
|
16
|
+
generator.generate
|
17
17
|
end
|
18
18
|
|
19
|
-
private
|
20
|
-
|
21
19
|
# Return LineNumbers object that represents two columns of numbers
|
22
20
|
# that will be displayed on the left of the HTML presentation.
|
23
21
|
#
|
@@ -26,6 +24,12 @@ private
|
|
26
24
|
def line_numbers
|
27
25
|
@_line_numbers ||= PrettyDiff::LineNumbers.new(meta_info)
|
28
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def generator
|
31
|
+
@_generator ||= PrettyDiff::ChunkGenerator.new(self)
|
32
|
+
end
|
29
33
|
|
30
34
|
# Parse the input searching for lines. Initialize Line object for every line.
|
31
35
|
# Return an array of Line objects.
|
data/lib/pretty_diff/diff.rb
CHANGED
@@ -4,40 +4,39 @@ require 'cgi'
|
|
4
4
|
# Main class to interact with. In fact this is the only class you should interact with
|
5
5
|
# when using the library.
|
6
6
|
#
|
7
|
-
# === Usage example
|
7
|
+
# === Usage example
|
8
8
|
# pretty = PrettyDiff::Diff.new(udiff)
|
9
9
|
# pretty.to_html
|
10
10
|
#
|
11
11
|
# Keep in mind that Diff will automatically escape all HTML tags from the intput string
|
12
12
|
# so that it doesn't interfere with the output.
|
13
13
|
#
|
14
|
-
# === Behind The Scenes:
|
15
|
-
# The Diff class is parsing input string and searching for Chunks.
|
16
|
-
# It's then asking HtmlGenerator to generate HTML for those chunks.
|
17
|
-
#
|
18
14
|
class PrettyDiff::Diff
|
19
15
|
CHUNK_REGEXP = /@@ .+ @@\n/
|
20
16
|
|
21
17
|
attr_reader :input
|
22
18
|
|
23
19
|
# Create new Diff object. Accept a String in unified diff format.
|
24
|
-
# Will automatically escape HTML tags from it.
|
25
20
|
def initialize(unified_diff)
|
26
21
|
@input = escape_html(unified_diff)
|
27
22
|
end
|
28
23
|
|
29
24
|
# Generate HTML presentation. Return a string.
|
30
25
|
def to_html
|
31
|
-
|
26
|
+
generator.generate
|
32
27
|
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
28
|
+
|
36
29
|
# Return an array of Chunk objects that Diff found in the input.
|
37
30
|
def chunks
|
38
31
|
@_chunks ||= find_chunks(input)
|
39
32
|
end
|
40
33
|
|
34
|
+
private
|
35
|
+
|
36
|
+
def generator
|
37
|
+
@_generator ||= PrettyDiff::DiffGenerator.new(self)
|
38
|
+
end
|
39
|
+
|
41
40
|
# Parse the input for diff chunks and initialize a Chunk object for each of them.
|
42
41
|
# Return an array of Chunks.
|
43
42
|
def find_chunks(text)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class PrettyDiff::ChunkGenerator
|
2
|
+
|
3
|
+
attr_reader :chunk
|
4
|
+
|
5
|
+
def initialize(chunk)
|
6
|
+
@chunk = chunk
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate
|
10
|
+
start_html +
|
11
|
+
chunk.line_numbers.to_html +
|
12
|
+
code_html(content) +
|
13
|
+
end_html
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def content
|
19
|
+
chunk.lines.map{|l| l.to_html }.join("\n")
|
20
|
+
end
|
21
|
+
|
22
|
+
def start_html
|
23
|
+
%Q[<tr>]
|
24
|
+
end
|
25
|
+
|
26
|
+
def code_html(text)
|
27
|
+
%Q[<td class="code"><div class="highlight"><pre>
|
28
|
+
#{text}</pre></div></td>]
|
29
|
+
end
|
30
|
+
|
31
|
+
def end_html
|
32
|
+
%Q[</tr>]
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class PrettyDiff::DiffGenerator
|
2
|
+
|
3
|
+
attr_reader :diff
|
4
|
+
|
5
|
+
def initialize(diff)
|
6
|
+
@diff = diff
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate
|
10
|
+
chunks_html = diff.chunks.map{|c| c.to_html}.join(chunk_separator_html).to_s
|
11
|
+
intro_html + chunks_html + outro_html
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def intro_html
|
17
|
+
%Q[<table class="highlighttable">]
|
18
|
+
end
|
19
|
+
|
20
|
+
def chunk_separator_html
|
21
|
+
%Q[<tr class="chunk_separator"><td colspan="3"></td></tr>]
|
22
|
+
end
|
23
|
+
|
24
|
+
def outro_html
|
25
|
+
%Q[</table>]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class PrettyDiff::LineGenerator
|
2
|
+
|
3
|
+
attr_reader :line
|
4
|
+
|
5
|
+
def initialize(line)
|
6
|
+
@line = line
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate
|
10
|
+
if line.added?
|
11
|
+
added_html(content)
|
12
|
+
elsif line.deleted?
|
13
|
+
deleted_html(content)
|
14
|
+
else
|
15
|
+
not_modified_html(content)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def content
|
22
|
+
@_content ||= line.format
|
23
|
+
end
|
24
|
+
|
25
|
+
def wrapper_html
|
26
|
+
%Q[<div>] +
|
27
|
+
yield +
|
28
|
+
%Q[</div>]
|
29
|
+
end
|
30
|
+
|
31
|
+
def added_html(text)
|
32
|
+
wrapper_html { %Q[<span class="gi">#{text}</span>] }
|
33
|
+
end
|
34
|
+
|
35
|
+
def deleted_html(text)
|
36
|
+
wrapper_html { %Q[<span class="gd">#{text}</span>] }
|
37
|
+
end
|
38
|
+
|
39
|
+
def not_modified_html(text)
|
40
|
+
wrapper_html { %Q[#{text}] }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class PrettyDiff::LineNumbersGenerator
|
2
|
+
|
3
|
+
attr_reader :line_numbers
|
4
|
+
|
5
|
+
def initialize(line_numbers)
|
6
|
+
@line_numbers = line_numbers
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate
|
10
|
+
column_html(left_column) + column_html(right_column)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def left_column
|
16
|
+
line_numbers.left_column.join("\n")
|
17
|
+
end
|
18
|
+
|
19
|
+
def right_column
|
20
|
+
line_numbers.right_column.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
def column_html(text)
|
24
|
+
%Q[<td class="linenos"><pre>
|
25
|
+
#{text}</pre></td>]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/lib/pretty_diff/line.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
# Represent a single line of the diff.
|
3
3
|
#
|
4
4
|
class PrettyDiff::Line #:nodoc:
|
5
|
+
|
5
6
|
attr_reader :input
|
6
7
|
|
7
8
|
def initialize(input)
|
@@ -10,13 +11,13 @@ class PrettyDiff::Line #:nodoc:
|
|
10
11
|
|
11
12
|
# Generate HTML presentation for a Line. Return a string.
|
12
13
|
def to_html
|
13
|
-
|
14
|
+
generator.generate
|
14
15
|
end
|
15
16
|
|
16
17
|
# Prepare Line contents for injection into HTML structure.
|
17
18
|
# Currently used for replacing Tab symbols with spaces.
|
18
19
|
# Return a string.
|
19
|
-
def
|
20
|
+
def format
|
20
21
|
input.gsub("\t", ' ')
|
21
22
|
end
|
22
23
|
|
@@ -50,4 +51,11 @@ class PrettyDiff::Line #:nodoc:
|
|
50
51
|
def not_modified?
|
51
52
|
status == :not_modified
|
52
53
|
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def generator
|
58
|
+
@_generator ||= PrettyDiff::LineGenerator.new(self)
|
59
|
+
end
|
60
|
+
|
53
61
|
end
|
@@ -23,14 +23,7 @@ class PrettyDiff::LineNumbers #:nodoc:
|
|
23
23
|
|
24
24
|
# Generate HTML presentation for a both line numbers columns. Return a string.
|
25
25
|
def to_html
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
# Search for information about line numbers changes provided by unified diff format.
|
32
|
-
def scan_meta(target)
|
33
|
-
meta_info.scan(target).flatten.first
|
26
|
+
generator.generate
|
34
27
|
end
|
35
28
|
|
36
29
|
def left_column
|
@@ -41,6 +34,17 @@ private
|
|
41
34
|
@right_column ||= []
|
42
35
|
end
|
43
36
|
|
37
|
+
private
|
38
|
+
|
39
|
+
def generator
|
40
|
+
@_generator ||= PrettyDiff::LineNumbersGenerator.new(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Search for information about line numbers changes provided by unified diff format.
|
44
|
+
def scan_meta(target)
|
45
|
+
meta_info.scan(target).flatten.first
|
46
|
+
end
|
47
|
+
|
44
48
|
# Return starting number for the left column according to unified diff information.
|
45
49
|
def left_starts_at
|
46
50
|
scan_meta(/^@@ -(\d+),/).to_i
|
@@ -54,7 +58,7 @@ private
|
|
54
58
|
# Increase left column line number by one.
|
55
59
|
def increase_left
|
56
60
|
left_column << increase_or_start(:left)
|
57
|
-
right_column << nil
|
61
|
+
right_column << nil
|
58
62
|
end
|
59
63
|
|
60
64
|
# Increase right column line number by one.
|
data/lib/pretty_diff.rb
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
module PrettyDiff #:nodoc:
|
2
2
|
end
|
3
3
|
|
4
|
-
|
5
|
-
require
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def require_local(suffix)
|
5
|
+
require(File.expand_path(File.join(File.dirname(__FILE__), suffix)))
|
6
|
+
end
|
7
|
+
|
8
|
+
require_local 'pretty_diff/support'
|
9
|
+
require_local 'pretty_diff/diff'
|
10
|
+
require_local 'pretty_diff/chunk'
|
11
|
+
require_local 'pretty_diff/line_numbers'
|
12
|
+
require_local 'pretty_diff/line'
|
13
|
+
|
14
|
+
require_local 'pretty_diff/html_generators/diff_generator'
|
15
|
+
require_local 'pretty_diff/html_generators/chunk_generator'
|
16
|
+
require_local 'pretty_diff/html_generators/line_generator'
|
17
|
+
require_local 'pretty_diff/html_generators/line_numbers_generator'
|
data/pretty_diff.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pretty_diff}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ilya Sabanin"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-24}
|
13
13
|
s.description = %q{PrettyDiff is a highly customizable library for creating fully featured HTML
|
14
14
|
listings out of unified diff format strings.
|
15
15
|
Include copy/paste-safe line numbers and built-in syntax highlighting.}
|
@@ -28,7 +28,10 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/pretty_diff.rb",
|
29
29
|
"lib/pretty_diff/chunk.rb",
|
30
30
|
"lib/pretty_diff/diff.rb",
|
31
|
-
"lib/pretty_diff/
|
31
|
+
"lib/pretty_diff/html_generators/chunk_generator.rb",
|
32
|
+
"lib/pretty_diff/html_generators/diff_generator.rb",
|
33
|
+
"lib/pretty_diff/html_generators/line_generator.rb",
|
34
|
+
"lib/pretty_diff/html_generators/line_numbers_generator.rb",
|
32
35
|
"lib/pretty_diff/line.rb",
|
33
36
|
"lib/pretty_diff/line_numbers.rb",
|
34
37
|
"lib/pretty_diff/support.rb",
|
data/test/chunk_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Sabanin
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-24 00:00:00 +07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -44,7 +44,10 @@ files:
|
|
44
44
|
- lib/pretty_diff.rb
|
45
45
|
- lib/pretty_diff/chunk.rb
|
46
46
|
- lib/pretty_diff/diff.rb
|
47
|
-
- lib/pretty_diff/
|
47
|
+
- lib/pretty_diff/html_generators/chunk_generator.rb
|
48
|
+
- lib/pretty_diff/html_generators/diff_generator.rb
|
49
|
+
- lib/pretty_diff/html_generators/line_generator.rb
|
50
|
+
- lib/pretty_diff/html_generators/line_numbers_generator.rb
|
48
51
|
- lib/pretty_diff/line.rb
|
49
52
|
- lib/pretty_diff/line_numbers.rb
|
50
53
|
- lib/pretty_diff/support.rb
|
@@ -1,91 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# This is where all HTML stuff is kept. The module is responsible for converting
|
3
|
-
# various PrettyDiff classes into HTML.
|
4
|
-
#
|
5
|
-
# By customizing this file you can suit the output precisely to your needs.
|
6
|
-
#
|
7
|
-
# By default all HTML is generated with a built-in support for coloring using a
|
8
|
-
# CSS file that's available here:
|
9
|
-
#
|
10
|
-
# http://ilya.sabanin.ru/projects/pretty_diff_example.html
|
11
|
-
#
|
12
|
-
module PrettyDiff::HtmlGenerator #:nodoc:
|
13
|
-
extend self
|
14
|
-
|
15
|
-
def intro
|
16
|
-
%Q[<table class="highlighttable">]
|
17
|
-
end
|
18
|
-
|
19
|
-
def chunk_start
|
20
|
-
%Q[<tr>]
|
21
|
-
end
|
22
|
-
|
23
|
-
def line_numbers_column(text)
|
24
|
-
%Q[<td class="linenos"><pre>
|
25
|
-
#{text}</pre></td>]
|
26
|
-
end
|
27
|
-
|
28
|
-
def code(text)
|
29
|
-
%Q[<td class="code"><div class="highlight"><pre>
|
30
|
-
#{text}</pre></div></td>]
|
31
|
-
end
|
32
|
-
|
33
|
-
def added_line(text)
|
34
|
-
%Q[<div><span class="gi">#{text}</span></div>]
|
35
|
-
end
|
36
|
-
|
37
|
-
def deleted_line(text)
|
38
|
-
%Q[<div><span class="gd">#{text}</span></div>]
|
39
|
-
end
|
40
|
-
|
41
|
-
def not_modified_line(text)
|
42
|
-
%Q[<div>#{text}</div>]
|
43
|
-
end
|
44
|
-
|
45
|
-
def chunk_end
|
46
|
-
%Q[</tr>]
|
47
|
-
end
|
48
|
-
|
49
|
-
def chunk_separator
|
50
|
-
%Q[<tr class="chunk_separator"><td colspan="3"></td></tr>]
|
51
|
-
end
|
52
|
-
|
53
|
-
def outro
|
54
|
-
%Q[</table>]
|
55
|
-
end
|
56
|
-
|
57
|
-
def generate_diff(chunks)
|
58
|
-
chunks_html = chunks.map{|c| c.to_html}.join(chunk_separator).to_s
|
59
|
-
intro + chunks_html + outro
|
60
|
-
end
|
61
|
-
|
62
|
-
def generate_chunk(numbers, lines)
|
63
|
-
lines_html = lines.map{|l| l.to_html }.join("\n")
|
64
|
-
chunk_start +
|
65
|
-
numbers.to_html +
|
66
|
-
code(lines_html) +
|
67
|
-
chunk_end
|
68
|
-
end
|
69
|
-
|
70
|
-
def generate_line_numbers(left, right)
|
71
|
-
[left, right].each do |column|
|
72
|
-
column.map! do |num|
|
73
|
-
if num.nil? then '' else %Q{<a href="#">#{num}</a>} end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
line_numbers_column(left.join("\n")) +
|
77
|
-
line_numbers_column(right.join("\n"))
|
78
|
-
end
|
79
|
-
|
80
|
-
def generate_line(obj)
|
81
|
-
content = obj.rendered
|
82
|
-
if obj.added?
|
83
|
-
added_line(content)
|
84
|
-
elsif obj.deleted?
|
85
|
-
deleted_line(content)
|
86
|
-
else
|
87
|
-
not_modified_line(content)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|