pretty_diff 0.6.0 → 0.7.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.
@@ -42,4 +42,8 @@ Read more about Open Source Fridays here:
42
42
 
43
43
  http://wildbit.com/blog/2010/01/15/open-source-fridays-at-wildbit/
44
44
 
45
- Copyright (c) 2010 Ilya Sabanin, Wildbit; see LICENSE for details.
45
+ == Contributors
46
+
47
+ 1. mrsimo
48
+
49
+ Copyright (c) 2010 Ilya Sabanin, Wildbit; see LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -5,7 +5,6 @@ def require_local(suffix)
5
5
  require(File.expand_path(File.join(File.dirname(__FILE__), suffix)))
6
6
  end
7
7
 
8
- require_local 'pretty_diff/support'
9
8
  require_local 'pretty_diff/diff'
10
9
  require_local 'pretty_diff/chunk'
11
10
  require_local 'pretty_diff/line_numbers'
@@ -14,4 +13,4 @@ require_local 'pretty_diff/line'
14
13
  require_local 'pretty_diff/html_generators/diff_generator'
15
14
  require_local 'pretty_diff/html_generators/chunk_generator'
16
15
  require_local 'pretty_diff/html_generators/line_generator'
17
- require_local 'pretty_diff/html_generators/line_numbers_generator'
16
+ require_local 'pretty_diff/html_generators/line_numbers_generator'
@@ -1,22 +1,22 @@
1
1
  #
2
2
  # Represent a single piece of a diff.
3
- #
3
+ #
4
4
  class PrettyDiff::Chunk #:nodoc:
5
5
  attr_reader :diff, :meta_info, :input, :lines
6
-
6
+
7
7
  def initialize(diff, meta_info, input)
8
8
  @diff = diff
9
9
  @meta_info = meta_info
10
10
  @input = input
11
11
  end
12
-
12
+
13
13
  # Generate HTML presentation for a Chunk. Return a string.
14
14
  def to_html
15
15
  # We have to find lines before we can call line numbers methods.
16
16
  find_lines!
17
17
  generator.generate
18
18
  end
19
-
19
+
20
20
  # Return LineNumbers object that represents two columns of numbers
21
21
  # that will be displayed on the left of the HTML presentation.
22
22
  #
@@ -25,7 +25,7 @@ class PrettyDiff::Chunk #:nodoc:
25
25
  def line_numbers
26
26
  @_line_numbers ||= PrettyDiff::LineNumbers.new(diff, meta_info)
27
27
  end
28
-
28
+
29
29
  private
30
30
 
31
31
  def generator
@@ -35,7 +35,8 @@ private
35
35
  # Parse the input searching for lines. Initialize Line object for every line.
36
36
  # Return an array of Line objects.
37
37
  def find_lines!
38
- returning(@lines = []) do
38
+ @lines = []
39
+ @lines.tap do
39
40
  input.split(/\r?\n/).each do |line_str|
40
41
  line = PrettyDiff::Line.new(diff, line_str)
41
42
  next if line.ignore?
@@ -45,4 +46,4 @@ private
45
46
  end
46
47
  end
47
48
 
48
- end
49
+ end
@@ -3,7 +3,7 @@ require 'cgi'
3
3
  #
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
7
  # === Usage example
8
8
  # pretty = PrettyDiff::Diff.new(udiff)
9
9
  # pretty.to_html
@@ -26,10 +26,10 @@ class PrettyDiff::Diff
26
26
  end
27
27
 
28
28
  # Generate HTML presentation. Return a string.
29
- def to_html
29
+ def to_html
30
30
  generator.generate
31
31
  end
32
-
32
+
33
33
  # Return an array of Chunk objects that Diff found in the input.
34
34
  def chunks
35
35
  @_chunks ||= find_chunks(input)
@@ -45,17 +45,18 @@ private
45
45
  # Return an array of Chunks.
46
46
  def find_chunks(text)
47
47
  meta_info = text.scan(CHUNK_REGEXP)
48
- returning(chunks = []) do
48
+ chunks = []
49
+ chunks.tap do
49
50
  split = text.split(CHUNK_REGEXP)
50
51
  split.shift
51
- split.each_with_index do |lines, idx|
52
+ split.each_with_index do |lines, idx|
52
53
  chunks << PrettyDiff::Chunk.new(self, meta_info[idx], lines)
53
54
  end
54
55
  end
55
56
  end
56
-
57
+
57
58
  def escape_html(input_text)
58
59
  CGI.escapeHTML(input_text)
59
60
  end
60
61
 
61
- end
62
+ end
@@ -1,18 +1,18 @@
1
1
  class PrettyDiff::ChunkGenerator
2
-
2
+
3
3
  attr_reader :chunk
4
-
4
+
5
5
  def initialize(chunk)
6
6
  @chunk = chunk
7
7
  end
8
-
8
+
9
9
  def generate
10
10
  start_html +
11
11
  chunk.line_numbers.to_html +
12
12
  code_html(content) +
13
13
  end_html
14
14
  end
15
-
15
+
16
16
  private
17
17
 
18
18
  def content
@@ -22,14 +22,14 @@ private
22
22
  def start_html
23
23
  %Q[<tr>]
24
24
  end
25
-
25
+
26
26
  def code_html(text)
27
27
  %Q[<td class="code"><div class="highlight"><pre>
28
28
  #{text}</pre></div></td>]
29
29
  end
30
-
30
+
31
31
  def end_html
32
32
  %Q[</tr>]
33
33
  end
34
-
35
- end
34
+
35
+ end
@@ -1,11 +1,11 @@
1
1
  class PrettyDiff::DiffGenerator
2
-
2
+
3
3
  attr_reader :diff
4
-
4
+
5
5
  def initialize(diff)
6
6
  @diff = diff
7
7
  end
8
-
8
+
9
9
  def generate
10
10
  chunks_html = diff.chunks.map{|c| c.to_html}.join(chunk_separator_html).to_s
11
11
  intro_html + chunks_html + outro_html
@@ -16,13 +16,13 @@ private
16
16
  def intro_html
17
17
  %Q[<table class="highlighttable">]
18
18
  end
19
-
19
+
20
20
  def chunk_separator_html
21
21
  %Q[<tr class="chunk_separator"><td colspan="3"></td></tr>]
22
22
  end
23
-
23
+
24
24
  def outro_html
25
25
  %Q[</table>]
26
26
  end
27
-
28
- end
27
+
28
+ end
@@ -1,11 +1,11 @@
1
1
  class PrettyDiff::LineGenerator
2
-
2
+
3
3
  attr_reader :line
4
-
4
+
5
5
  def initialize(line)
6
6
  @line = line
7
7
  end
8
-
8
+
9
9
  def generate
10
10
  if line.added?
11
11
  added_html(content)
@@ -15,13 +15,13 @@ class PrettyDiff::LineGenerator
15
15
  not_modified_html(content)
16
16
  end
17
17
  end
18
-
18
+
19
19
  private
20
20
 
21
21
  def content
22
22
  @_content ||= line.format
23
23
  end
24
-
24
+
25
25
  def wrapper_html
26
26
  if line.diff.options[:wrap_lines]
27
27
  "<div> #{yield} </div>"
@@ -41,5 +41,5 @@ private
41
41
  def not_modified_html(text)
42
42
  wrapper_html { %Q[#{text}] }
43
43
  end
44
-
45
- end
44
+
45
+ end
@@ -1,28 +1,28 @@
1
1
  class PrettyDiff::LineNumbersGenerator
2
-
2
+
3
3
  attr_reader :line_numbers
4
-
4
+
5
5
  def initialize(line_numbers)
6
6
  @line_numbers = line_numbers
7
7
  end
8
-
8
+
9
9
  def generate
10
10
  column_html(left_column) + column_html(right_column)
11
11
  end
12
-
12
+
13
13
  private
14
14
 
15
15
  def left_column
16
16
  line_numbers.left_column.join("\n")
17
17
  end
18
-
18
+
19
19
  def right_column
20
20
  line_numbers.right_column.join("\n")
21
21
  end
22
-
22
+
23
23
  def column_html(text)
24
24
  %Q[<td width="2%" class="linenos"><pre>
25
25
  #{text}</pre></td>]
26
26
  end
27
-
28
- end
27
+
28
+ end
@@ -2,33 +2,33 @@
2
2
  # Represent a single line of the diff.
3
3
  #
4
4
  class PrettyDiff::Line #:nodoc:
5
-
5
+
6
6
  attr_reader :diff, :input
7
-
7
+
8
8
  def initialize(diff, input)
9
9
  @diff = diff
10
10
  @input = input
11
11
  end
12
-
12
+
13
13
  # Generate HTML presentation for a Line. Return a string.
14
14
  def to_html
15
15
  generator.generate
16
16
  end
17
-
17
+
18
18
  # Prepare Line contents for injection into HTML structure.
19
19
  # Currently used for replacing Tab symbols with spaces.
20
20
  # Return a string.
21
21
  def format
22
22
  input.gsub("\t", ' ')
23
23
  end
24
-
24
+
25
25
  # Unified Diff sometimes emit a special line at the end of the file
26
26
  # that we should not display in the output.
27
27
  # Return true or false.
28
28
  def ignore?
29
29
  input =~ /\/
30
30
  end
31
-
31
+
32
32
  # Return status of the Line. Can be :added, :deleted or :not_modified.
33
33
  def status
34
34
  case input
@@ -40,23 +40,23 @@ class PrettyDiff::Line #:nodoc:
40
40
  :not_modified
41
41
  end
42
42
  end
43
-
43
+
44
44
  def added?
45
45
  status == :added
46
46
  end
47
-
47
+
48
48
  def deleted?
49
49
  status == :deleted
50
50
  end
51
-
51
+
52
52
  def not_modified?
53
53
  status == :not_modified
54
54
  end
55
-
55
+
56
56
  private
57
57
 
58
58
  def generator
59
59
  @_generator ||= PrettyDiff::LineGenerator.new(self)
60
60
  end
61
-
62
- end
61
+
62
+ end
@@ -3,14 +3,14 @@
3
3
  # on the left of the HTML presentation.
4
4
  #
5
5
  class PrettyDiff::LineNumbers #:nodoc:
6
-
6
+
7
7
  attr_reader :diff, :meta_info
8
-
8
+
9
9
  def initialize(diff, meta)
10
10
  @diff = diff
11
11
  @meta_info = meta
12
12
  end
13
-
13
+
14
14
  # Increase either left column of numbers, right or both of them; depending on the Line status.
15
15
  def act_on_line(line)
16
16
  if line.added?
@@ -21,36 +21,36 @@ class PrettyDiff::LineNumbers #:nodoc:
21
21
  increase_both
22
22
  end
23
23
  end
24
-
24
+
25
25
  # Generate HTML presentation for a both line numbers columns. Return a string.
26
26
  def to_html
27
27
  generator.generate
28
28
  end
29
-
29
+
30
30
  def left_column
31
31
  @left_column ||= []
32
32
  end
33
-
33
+
34
34
  def right_column
35
35
  @right_column ||= []
36
36
  end
37
-
37
+
38
38
  private
39
39
 
40
40
  def generator
41
41
  @_generator ||= PrettyDiff::LineNumbersGenerator.new(self)
42
42
  end
43
-
43
+
44
44
  # Search for information about line numbers changes provided by unified diff format.
45
45
  def scan_meta(target)
46
46
  meta_info.scan(target).flatten.first
47
47
  end
48
-
48
+
49
49
  # Return starting number for the left column according to unified diff information.
50
50
  def left_starts_at
51
51
  scan_meta(/^@@ -(\d+),/).to_i
52
52
  end
53
-
53
+
54
54
  # Return starting number for the right column according to unified diff information.
55
55
  def right_starts_at
56
56
  scan_meta(/\+(\d+),\d+ @@$/).to_i
@@ -61,24 +61,24 @@ private
61
61
  left_column << increase_or_start(:left)
62
62
  right_column << nil
63
63
  end
64
-
64
+
65
65
  # Increase right column line number by one.
66
66
  def increase_right
67
67
  left_column << nil
68
68
  right_column << increase_or_start(:right)
69
69
  end
70
-
70
+
71
71
  # Increase both columns line numbers by one.
72
72
  def increase_both
73
73
  left_column << increase_or_start(:left)
74
74
  right_column << increase_or_start(:right)
75
75
  end
76
-
76
+
77
77
  # Either increasing existing line number by one or using the initial number provided by
78
78
  # unified diff format.
79
79
  def increase_or_start(which)
80
80
  previous = send("#{which}_column").reverse.find{|e| !e.nil?}
81
81
  if previous then previous + 1 else send("#{which}_starts_at") end
82
82
  end
83
-
84
- end
83
+
84
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pretty_diff}
8
- s.version = "0.6.0"
8
+ s.version = "0.7.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-03-03}
12
+ s.date = %q{2010-09-09}
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.}
@@ -34,7 +34,6 @@ Gem::Specification.new do |s|
34
34
  "lib/pretty_diff/html_generators/line_numbers_generator.rb",
35
35
  "lib/pretty_diff/line.rb",
36
36
  "lib/pretty_diff/line_numbers.rb",
37
- "lib/pretty_diff/support.rb",
38
37
  "pretty_diff.gemspec",
39
38
  "test/chunk_test.rb",
40
39
  "test/data/first.diff",
@@ -16,22 +16,22 @@ class ChunkTest < Test::Unit::TestCase
16
16
  @chunk = @diff.send(:chunks).first
17
17
  @chunk.send(:find_lines!)
18
18
  end
19
-
19
+
20
20
  should "generate HTML without errors" do
21
21
  assert @chunk.to_html
22
22
  end
23
-
23
+
24
24
  should "find correct amount of left line numbers" do
25
25
  assert @chunk.send(:line_numbers).send(:left_column).compact.size == 6
26
26
  end
27
-
27
+
28
28
  should "find correct amount of right line numbers" do
29
29
  assert @chunk.send(:line_numbers).send(:right_column).compact.size == 8
30
30
  end
31
-
31
+
32
32
  should "find correct amount of lines" do
33
33
  assert @chunk.lines.size == 8
34
34
  end
35
35
 
36
36
  end
37
- end
37
+ end
@@ -3,27 +3,27 @@ require 'cgi'
3
3
 
4
4
  class DiffTest < Test::Unit::TestCase
5
5
  context "Pretty Diff" do
6
-
6
+
7
7
  setup do
8
8
  @small_diff = PrettyDiff::Diff.new read_diff('first.diff')
9
9
  @big_diff = PrettyDiff::Diff.new read_diff('second.diff')
10
10
  end
11
-
11
+
12
12
  should "generate correct amount of chunks for each diff" do
13
13
  assert_equal 2, @small_diff.send(:chunks).size
14
14
  assert_equal 4, @big_diff.send(:chunks).size
15
15
  end
16
-
16
+
17
17
  should "generate HTML representation without errors" do
18
18
  assert @small_diff.to_html
19
19
  assert @big_diff.to_html
20
20
  end
21
-
21
+
22
22
  should "escape HTML from the input string" do
23
23
  text = "<b>Funky HTML</b>"
24
24
  diff = PrettyDiff::Diff.new(text)
25
25
  assert_equal "&lt;b&gt;Funky HTML&lt;/b&gt;", diff.input
26
26
  end
27
-
27
+
28
28
  end
29
- end
29
+ end
@@ -7,9 +7,9 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'pretty_diff'
8
8
 
9
9
  class Test::Unit::TestCase
10
-
10
+
11
11
  def read_diff(name)
12
12
  File.read(File.join(File.dirname(__FILE__), "data", name))
13
13
  end
14
-
14
+
15
15
  end
@@ -1,31 +1,31 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class HtmlGeneratorTest < Test::Unit::TestCase
4
-
4
+
5
5
  context "Html Generator" do
6
6
 
7
- context "for lines" do
7
+ context "for lines" do
8
8
  should "generate correct HTML for added line" do
9
9
  text = "+package chiro.methods.new.ones;"
10
10
  expected_html = %Q[<span class="gi">#{text}</span>]
11
- assert_equal expected_html, line_to_html(text)
11
+ assert_equal expected_html, line_to_html(text)
12
12
  end
13
-
13
+
14
14
  should "generate correct HTML for deleted line" do
15
15
  text = '- browser.setTimeout("50000");'
16
16
  expected_html = %Q[<span class="gd">- browser.setTimeout("50000");</span>]
17
- assert_equal expected_html, line_to_html(text)
17
+ assert_equal expected_html, line_to_html(text)
18
18
  end
19
-
19
+
20
20
  should "generate correct HTML for not modified line" do
21
21
  text = ' public static void close() {'
22
22
  expected_html = " public static void close() {"
23
23
  assert_equal expected_html, line_to_html(text)
24
- end
24
+ end
25
25
  end
26
-
26
+
27
27
  end
28
-
28
+
29
29
  private
30
30
 
31
31
  def new_line(text)
@@ -35,5 +35,5 @@ private
35
35
  def line_to_html(text)
36
36
  new_line(text).to_html
37
37
  end
38
-
39
- end
38
+
39
+ end
@@ -7,32 +7,32 @@ class LineTest < Test::Unit::TestCase
7
7
  added_line = new_diff("+package chiro.methods.new.ones;")
8
8
  assert_equal :added, added_line.status
9
9
  end
10
-
10
+
11
11
  should "indicate :deleted status correctly" do
12
12
  deleted_line = new_diff('-browser.setTimeout("50000");')
13
13
  assert_equal :deleted, deleted_line.status
14
14
  end
15
-
15
+
16
16
  should "indicate :not_modified status correctly" do
17
17
  not_modified_line = new_diff("class User < ActiveRecord::Base")
18
18
  assert_equal :not_modified, not_modified_line.status
19
19
  end
20
-
20
+
21
21
  should "ignore trailing 'no newline' text" do
22
22
  text = ''
23
23
  line = new_diff(text)
24
24
  assert line.ignore?
25
25
  end
26
-
26
+
27
27
  should "replace tabs with spaces" do
28
28
  text = " hello eptut {1, 2, 3}"
29
29
  expected_output = " hello eptut {1, 2, 3}"
30
30
  line = new_diff(text)
31
31
  assert_equal expected_output, line.rendered
32
32
  end
33
-
33
+
34
34
  end
35
-
35
+
36
36
  private
37
37
 
38
38
  def new_diff(text)
@@ -42,4 +42,4 @@ private
42
42
  def line_to_html(text)
43
43
  new_diff(text).to_html
44
44
  end
45
- end
45
+ end
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.6.0
4
+ version: 0.7.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-03-03 00:00:00 +07:00
12
+ date: 2010-09-09 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -50,7 +50,6 @@ files:
50
50
  - lib/pretty_diff/html_generators/line_numbers_generator.rb
51
51
  - lib/pretty_diff/line.rb
52
52
  - lib/pretty_diff/line_numbers.rb
53
- - lib/pretty_diff/support.rb
54
53
  - pretty_diff.gemspec
55
54
  - test/chunk_test.rb
56
55
  - test/data/first.diff
@@ -1,8 +0,0 @@
1
- class Object #:nodoc:
2
-
3
- def returning(value)
4
- yield(value)
5
- value
6
- end
7
-
8
- end