pretty_diff 0.9.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +41 -6
  4. data/Rakefile +0 -1
  5. data/VERSION +1 -1
  6. data/lib/pretty_diff.rb +5 -6
  7. data/lib/pretty_diff/abstract_generator.rb +51 -0
  8. data/lib/pretty_diff/basic_generator.rb +72 -0
  9. data/lib/pretty_diff/chunk.rb +24 -41
  10. data/lib/pretty_diff/diff.rb +75 -41
  11. data/lib/pretty_diff/encoding.rb +30 -0
  12. data/lib/pretty_diff/line.rb +30 -51
  13. data/lib/pretty_diff/line_numbers.rb +47 -67
  14. data/pretty_diff.gemspec +33 -18
  15. data/test/abstract_generator_test.rb +65 -0
  16. data/test/basic_generator_test.rb +29 -0
  17. data/test/chunk_test.rb +13 -30
  18. data/test/diff_test.rb +50 -19
  19. data/test/encoding_test.rb +35 -0
  20. data/test/fixtures/blank.diff +0 -0
  21. data/test/fixtures/blank.diff.html +1 -0
  22. data/test/fixtures/chinese-gbk-crlf +5 -0
  23. data/test/fixtures/cp1251.diff +13 -0
  24. data/test/fixtures/csharp.diff +12 -0
  25. data/test/fixtures/csharp.diff.html +19 -0
  26. data/test/{data → fixtures}/first.diff +0 -0
  27. data/test/fixtures/first.diff.html +85 -0
  28. data/test/{data → fixtures}/second.diff +0 -0
  29. data/test/fixtures/single_line.diff +7 -0
  30. data/test/fixtures/single_line.diff.html +4 -0
  31. data/test/fixtures/text.diff +8 -0
  32. data/test/fixtures/text.diff.html +13 -0
  33. data/test/fixtures/windows-cp1251-lf +6 -0
  34. data/test/helper.rb +10 -8
  35. data/test/line_numbers_test.rb +25 -0
  36. data/test/line_test.rb +26 -37
  37. metadata +81 -75
  38. data/lib/pretty_diff/html_generators/chunk_generator.rb +0 -41
  39. data/lib/pretty_diff/html_generators/diff_generator.rb +0 -13
  40. data/lib/pretty_diff/html_generators/line_generator.rb +0 -45
  41. data/lib/pretty_diff/html_generators/line_numbers_generator.rb +0 -36
  42. data/test/html_generator_test.rb +0 -39
@@ -0,0 +1,30 @@
1
+ require 'charlock_holmes'
2
+
3
+ module PrettyDiff
4
+ module Encoding
5
+ extend self
6
+
7
+ def enforce(encoding, text)
8
+ result = text
9
+ if (detected = detect(result)) && detected.downcase != encoding.downcase
10
+ result = convert(result, detected, encoding)
11
+ end
12
+ if RUBY_VERSION >= "2.0.0"
13
+ result.force_encoding(encoding)
14
+ else
15
+ result
16
+ end
17
+ end
18
+
19
+ def detect(str)
20
+ if detected = CharlockHolmes::EncodingDetector.detect(str)
21
+ detected[:encoding]
22
+ end
23
+ end
24
+
25
+ def convert(str, from, to)
26
+ CharlockHolmes::Converter.convert(str, from, to)
27
+ end
28
+
29
+ end
30
+ end
@@ -1,60 +1,39 @@
1
- #
2
- # Represent a single line of the diff.
3
- #
4
- class PrettyDiff::Line #:nodoc:
5
-
6
- attr_reader :diff, :input
7
-
8
- def initialize(diff, input)
9
- @diff = diff
10
- @input = input
11
- end
12
-
13
- # Generate HTML presentation for a Line. Return a string.
14
- def to_html
15
- generator.generate
16
- end
17
-
18
- # Prepare Line contents for injection into HTML structure.
19
- def format
20
- input.gsub("\t", ' ')
21
- end
22
-
23
- # Unified Diff sometimes emit a special line at the end of the file
24
- # that we should not display in the output.
25
- # Return true or false.
26
- def ignore?
27
- input =~ /\/
28
- end
1
+ module PrettyDiff
2
+ class Line
3
+ attr_reader :chunk, :contents
4
+ attr_accessor :left_number, :right_number
5
+
6
+ def initialize(chunk, contents)
7
+ @chunk = chunk
8
+ @contents = contents
9
+ end
29
10
 
30
- # Return status of the Line. Can be :added, :deleted or :not_modified.
31
- def status
32
- case input
33
- when /^\+/
34
- :added
35
- when /^\-/
36
- :deleted
37
- else
38
- :not_modified
11
+ def ignored?
12
+ contents =~ /\/
39
13
  end
40
- end
41
14
 
42
- def added?
43
- status == :added
44
- end
15
+ def status
16
+ case contents
17
+ when /^\+/
18
+ :added
19
+ when /^\-/
20
+ :deleted
21
+ else
22
+ :not_modified
23
+ end
24
+ end
45
25
 
46
- def deleted?
47
- status == :deleted
48
- end
26
+ def added?
27
+ status == :added
28
+ end
49
29
 
50
- def not_modified?
51
- status == :not_modified
52
- end
30
+ def deleted?
31
+ status == :deleted
32
+ end
53
33
 
54
- private
34
+ def not_modified?
35
+ status == :not_modified
36
+ end
55
37
 
56
- def generator
57
- @_generator ||= PrettyDiff::LineGenerator.new(self)
58
38
  end
59
-
60
39
  end
@@ -1,84 +1,64 @@
1
- #
2
- # Represent 2 columns of numbers that will be displayed
3
- # on the left of the HTML presentation.
4
- #
5
- class PrettyDiff::LineNumbers #:nodoc:
1
+ module PrettyDiff
2
+ class LineNumbers
6
3
 
7
- attr_reader :diff, :meta_info
4
+ attr_reader :diff, :meta_info
8
5
 
9
- def initialize(diff, meta)
10
- @diff = diff
11
- @meta_info = meta
12
- end
13
-
14
- # Increase either left column of numbers, right or both of them; depending on the Line status.
15
- def act_on_line(line)
16
- if line.added?
17
- increase_right
18
- elsif line.deleted?
19
- increase_left
20
- else
21
- increase_both
6
+ def initialize(diff, meta)
7
+ @diff = diff
8
+ @meta_info = meta
22
9
  end
23
- end
24
10
 
25
- # Generate HTML presentation for a both line numbers columns. Return a string.
26
- def to_html
27
- generator.generate
28
- end
11
+ def act_on_line(line)
12
+ if line.added?
13
+ increase_right
14
+ elsif line.deleted?
15
+ increase_left
16
+ else
17
+ increase_both
18
+ end
19
+ end
29
20
 
30
- def left_column
31
- @left_column ||= []
32
- end
21
+ def left_column
22
+ @left_column ||= []
23
+ end
33
24
 
34
- def right_column
35
- @right_column ||= []
36
- end
25
+ def right_column
26
+ @right_column ||= []
27
+ end
37
28
 
38
- private
29
+ private
39
30
 
40
- def generator
41
- @_generator ||= PrettyDiff::LineNumbersGenerator.new(self)
42
- end
31
+ def scan_meta(target)
32
+ meta_info.scan(target).flatten.first
33
+ end
43
34
 
44
- # Search for information about line numbers changes provided by unified diff format.
45
- def scan_meta(target)
46
- meta_info.scan(target).flatten.first
47
- end
35
+ def left_starts_at
36
+ scan_meta(/^@@ -(\d+)/).to_i
37
+ end
48
38
 
49
- # Return starting number for the left column according to unified diff information.
50
- def left_starts_at
51
- scan_meta(/^@@ -(\d+),/).to_i
52
- end
39
+ def right_starts_at
40
+ scan_meta(/\+(\d+)(?:,\d+)? @@/).to_i
41
+ end
53
42
 
54
- # Return starting number for the right column according to unified diff information.
55
- def right_starts_at
56
- scan_meta(/\+(\d+),\d+ @@$/).to_i
57
- end
43
+ def increase_left
44
+ left_column << increase_or_start(:left)
45
+ right_column << nil
46
+ end
58
47
 
59
- # Increase left column line number by one.
60
- def increase_left
61
- left_column << increase_or_start(:left)
62
- right_column << nil
63
- end
48
+ def increase_right
49
+ left_column << nil
50
+ right_column << increase_or_start(:right)
51
+ end
64
52
 
65
- # Increase right column line number by one.
66
- def increase_right
67
- left_column << nil
68
- right_column << increase_or_start(:right)
69
- end
53
+ def increase_both
54
+ left_column << increase_or_start(:left)
55
+ right_column << increase_or_start(:right)
56
+ end
70
57
 
71
- # Increase both columns line numbers by one.
72
- def increase_both
73
- left_column << increase_or_start(:left)
74
- right_column << increase_or_start(:right)
75
- end
58
+ def increase_or_start(which)
59
+ previous = send("#{which}_column").reverse.find{|e| !e.nil?}
60
+ if previous then previous + 1 else send("#{which}_starts_at") end
61
+ end
76
62
 
77
- # Either increasing existing line number by one or using the initial number provided by
78
- # unified diff format.
79
- def increase_or_start(which)
80
- previous = send("#{which}_column").reverse.find{|e| !e.nil?}
81
- if previous then previous + 1 else send("#{which}_starts_at") end
82
63
  end
83
-
84
64
  end
data/pretty_diff.gemspec CHANGED
@@ -4,16 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{pretty_diff}
8
- s.version = "0.9.3"
7
+ s.name = "pretty_diff"
8
+ s.version = "2.0.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{2013-04-05}
13
- s.description = %q{PrettyDiff is a highly customizable library for creating fully featured HTML
14
- listings out of unified diff format strings.
15
- Include copy/paste-safe line numbers and built-in syntax highlighting.}
16
- s.email = %q{ilya.sabanin@gmail.com}
12
+ s.date = "2014-02-18"
13
+ s.description = "PrettyDiff is a highly customizable library for creating fully featured HTML\n listings out of unified diff format strings.\n Include copy/paste-safe line numbers and built-in syntax highlighting."
14
+ s.email = "ilya.sabanin@gmail.com"
17
15
  s.extra_rdoc_files = [
18
16
  "LICENSE",
19
17
  "README.rdoc"
@@ -27,39 +25,56 @@ Gem::Specification.new do |s|
27
25
  "Rakefile",
28
26
  "VERSION",
29
27
  "lib/pretty_diff.rb",
28
+ "lib/pretty_diff/abstract_generator.rb",
29
+ "lib/pretty_diff/basic_generator.rb",
30
30
  "lib/pretty_diff/chunk.rb",
31
31
  "lib/pretty_diff/diff.rb",
32
- "lib/pretty_diff/html_generators/chunk_generator.rb",
33
- "lib/pretty_diff/html_generators/diff_generator.rb",
34
- "lib/pretty_diff/html_generators/line_generator.rb",
35
- "lib/pretty_diff/html_generators/line_numbers_generator.rb",
32
+ "lib/pretty_diff/encoding.rb",
36
33
  "lib/pretty_diff/line.rb",
37
34
  "lib/pretty_diff/line_numbers.rb",
38
35
  "pretty_diff.gemspec",
36
+ "test/abstract_generator_test.rb",
37
+ "test/basic_generator_test.rb",
39
38
  "test/chunk_test.rb",
40
- "test/data/first.diff",
41
- "test/data/second.diff",
42
39
  "test/diff_test.rb",
40
+ "test/encoding_test.rb",
41
+ "test/fixtures/blank.diff",
42
+ "test/fixtures/blank.diff.html",
43
+ "test/fixtures/chinese-gbk-crlf",
44
+ "test/fixtures/cp1251.diff",
45
+ "test/fixtures/csharp.diff",
46
+ "test/fixtures/csharp.diff.html",
47
+ "test/fixtures/first.diff",
48
+ "test/fixtures/first.diff.html",
49
+ "test/fixtures/second.diff",
50
+ "test/fixtures/single_line.diff",
51
+ "test/fixtures/single_line.diff.html",
52
+ "test/fixtures/text.diff",
53
+ "test/fixtures/text.diff.html",
54
+ "test/fixtures/windows-cp1251-lf",
43
55
  "test/helper.rb",
44
- "test/html_generator_test.rb",
56
+ "test/line_numbers_test.rb",
45
57
  "test/line_test.rb"
46
58
  ]
47
- s.homepage = %q{http://github.com/isabanin/pretty_diff}
59
+ s.homepage = "http://github.com/isabanin/pretty_diff"
48
60
  s.require_paths = ["lib"]
49
- s.rubygems_version = %q{1.6.2}
50
- s.summary = %q{Library for converting unified diff format into HTML listings.}
61
+ s.rubygems_version = "2.0.3"
62
+ s.summary = "Library for converting unified diff format into HTML listings."
51
63
 
52
64
  if s.respond_to? :specification_version then
53
- s.specification_version = 3
65
+ s.specification_version = 4
54
66
 
55
67
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
+ s.add_runtime_dependency(%q<charlock_holmes>, ["~> 0.6"])
56
69
  s.add_development_dependency(%q<jeweler>, [">= 0"])
57
70
  s.add_development_dependency(%q<builder>, [">= 0"])
58
71
  else
72
+ s.add_dependency(%q<charlock_holmes>, ["~> 0.6"])
59
73
  s.add_dependency(%q<jeweler>, [">= 0"])
60
74
  s.add_dependency(%q<builder>, [">= 0"])
61
75
  end
62
76
  else
77
+ s.add_dependency(%q<charlock_holmes>, ["~> 0.6"])
63
78
  s.add_dependency(%q<jeweler>, [">= 0"])
64
79
  s.add_dependency(%q<builder>, [">= 0"])
65
80
  end
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class AbstractGeneratorTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @diff = new_diff(fixture('first.diff'))
7
+ end
8
+
9
+ def test_target_accessor
10
+ generator = gen.new(@diff)
11
+ assert generator.respond_to?(:diff)
12
+ assert generator.instance_variables.include?(:@diff)
13
+ end
14
+
15
+ def test_stubbed_generate
16
+ assert_raises RuntimeError do
17
+ gen.new(@diff).generate
18
+ end
19
+ end
20
+
21
+ def test_tag_with_options
22
+ assert_equal '<img class="test" />',
23
+ gen.new(@diff).send(:tag, :img, :class => 'test')
24
+ end
25
+
26
+ def test_tag_without_options
27
+ assert_equal '<img />',
28
+ gen.new(@diff).send(:tag, :img)
29
+ end
30
+
31
+ def test_open_tag
32
+ assert_equal '<strong>',
33
+ gen.new(@diff).send(:tag, :strong, nil, true)
34
+ end
35
+
36
+ def test_content_tag_with_options
37
+ assert_equal '<div class="test">a</div>',
38
+ gen.new(@diff).send(:content_tag, :div, :class => 'test'){'a'}
39
+ end
40
+
41
+ def test_content_tag_without_options
42
+ assert_equal '<div>a</div>',
43
+ gen.new(@diff).send(:content_tag, :div){'a'}
44
+ end
45
+
46
+ def test_h
47
+ assert_equal '&lt;strong&gt;vasya&lt;/strong&gt;',
48
+ gen.new(@diff).send(:h, '<strong>vasya</strong>')
49
+ end
50
+
51
+ def test_class_to_target_name
52
+ assert_equal 'diff',
53
+ gen.new(@diff).send(:class_to_target_name, 'DiffGenerator')
54
+
55
+ assert_equal 'line_numbers',
56
+ gen.new(@diff).send(:class_to_target_name, 'LineNumbersGen')
57
+ end
58
+
59
+ private
60
+
61
+ def gen
62
+ PrettyDiff::AbstractGenerator
63
+ end
64
+
65
+ end
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+
3
+ class BasicGeneratorTest < MiniTest::Unit::TestCase
4
+
5
+ def test_generated_html
6
+ diff = new_diff(fixture('first.diff'), :generator => PrettyDiff::BasicGenerator)
7
+ assert_equal fixture('first.diff.html'), diff.to_html
8
+ end
9
+
10
+ def test_more_generated_html
11
+ diff = new_diff(fixture('text.diff'), :generator => PrettyDiff::BasicGenerator)
12
+ assert_equal fixture('text.diff.html'), diff.to_html
13
+ end
14
+
15
+ def test_another_generated_html
16
+ diff = new_diff(fixture('csharp.diff'), :generator => PrettyDiff::BasicGenerator)
17
+ assert_equal fixture('csharp.diff.html'), diff.to_html
18
+ end
19
+
20
+ def test_generate_html_for_blank
21
+ diff = new_diff(fixture('blank.diff'), :generator => PrettyDiff::BasicGenerator)
22
+ assert_equal fixture('blank.diff.html'), diff.to_html
23
+ end
24
+
25
+ def test_generate_html_for_single_line_diffs
26
+ diff = new_diff(fixture('single_line.diff'), :generator => PrettyDiff::BasicGenerator)
27
+ assert_equal fixture('single_line.diff.html'), diff.to_html
28
+ end
29
+ end
data/test/chunk_test.rb CHANGED
@@ -1,37 +1,20 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
2
 
3
- # <link rel="stylesheet" href="../stylesheets/codecolorer.css" type="text/css" media="screen, projection" />
4
- # <link rel="stylesheet" href="../stylesheets/modalbox.css" type="text/css" media="screen, projection" />
5
- # <link rel="stylesheet" href="../stylesheets/print.css" type="text/css" media="print" />
6
- #+ <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
7
- #+ <link rel="apple-touch-icon" href="../images/apple-touch-icon.png"/>
8
- # <script type="text/javascript" src="../javascripts/prototype.js" charset="utf-8"></script>
9
- # <script type="text/javascript" src="../javascripts/scriptaculous.js" charset="utf-8"></script>
10
- # <script type="text/javascript" src="../javascripts/application.js" charset="utf-8"></script>
3
+ class ChunkTest < MiniTest::Unit::TestCase
11
4
 
12
- class ChunkTest < Test::Unit::TestCase
13
- context "Diff Chunk" do
14
- setup do
15
- @diff = PrettyDiff::Diff.new read_diff('second.diff')
16
- @chunk = @diff.send(:chunks).first
17
- @chunk.send(:find_lines!)
18
- end
19
-
20
- should "generate HTML without errors" do
21
- assert @chunk.to_html
22
- end
23
-
24
- should "find correct amount of left line numbers" do
25
- assert @chunk.send(:line_numbers).send(:left_column).compact.size == 6
26
- end
27
-
28
- should "find correct amount of right line numbers" do
29
- assert @chunk.send(:line_numbers).send(:right_column).compact.size == 8
30
- end
5
+ def setup
6
+ @diff = new_diff(fixture('first.diff'))
7
+ @chunk = @diff.chunks.last
8
+ end
31
9
 
32
- should "find correct amount of lines" do
33
- assert @chunk.lines.size == 8
10
+ def test_lines
11
+ assert_equal 20, @chunk.lines.size
12
+ @chunk.lines.each do |l|
13
+ assert l.kind_of?(PrettyDiff::Line)
34
14
  end
15
+ end
35
16
 
17
+ def test_line_numbers
18
+ assert @chunk.line_numbers.kind_of?(PrettyDiff::LineNumbers)
36
19
  end
37
20
  end