diffed 0.0.8 → 0.0.9
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/lib/diffed.rb +2 -83
- data/lib/diffed/version.rb +1 -1
- data/lib/formatters/html.rb +52 -0
- data/lib/models/file.rb +3 -0
- data/lib/models/line.rb +31 -2
- data/lib/models/section.rb +25 -1
- metadata +8 -7
data/lib/diffed.rb
CHANGED
@@ -12,97 +12,16 @@ module Diffed
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class Diff
|
15
|
+
include Html
|
15
16
|
attr_accessor :sections
|
16
17
|
|
17
18
|
def initialize(lines)
|
18
19
|
parse(lines)
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
-
html = make_table_tag(use_inline_styles)
|
23
|
-
|
24
|
-
@sections.each do |section|
|
25
|
-
html << format_section_header_row(section.header, use_inline_styles)
|
26
|
-
section.lines.each_with_index do |line, i|
|
27
|
-
html << format_code_line(line, use_inline_styles)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
html << "</table>"
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
def format_code_line(line, use_inline_styles)
|
36
|
-
row = OutputRow.new(:code_line =>line)
|
37
|
-
|
38
|
-
if use_inline_styles
|
39
|
-
format_styled_row code_line_style(line), '#000', row
|
40
|
-
else
|
41
|
-
format_classed_row line.type.to_s, row
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def code_line_style(line)
|
46
|
-
case line.type
|
47
|
-
when :left
|
48
|
-
'#FDD'
|
49
|
-
when :right
|
50
|
-
'#DFD'
|
51
|
-
when :both
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def format_section_header_row(header, use_inline_styles)
|
57
|
-
row = OutputRow.new(:left => "...", :right => "...", :text => header)
|
58
|
-
|
59
|
-
if use_inline_styles
|
60
|
-
format_styled_row '#F0F0FF', '#888', row
|
61
|
-
else
|
62
|
-
format_classed_row 'section', row
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def make_table_tag(inline_styles)
|
67
|
-
if inline_styles
|
68
|
-
table_attributes = %Q{cellpadding="5" style="border-collapse: collapse; border: 1px solid \#CCC; font-family: Consolas, courier, monospace; font-size: 13px; color: #888"}
|
69
|
-
else
|
70
|
-
table_attributes = %Q{class="coloured-diff"}
|
71
|
-
end
|
72
|
-
|
73
|
-
%Q{<table #{table_attributes}>\n}
|
74
|
-
end
|
75
|
-
|
76
|
-
def format_styled_row(bg_color, text_color, row)
|
77
|
-
row_style = bg_color.nil? ? "" : %Q{ style="background-color: #{bg_color}"}
|
78
|
-
text_color = text_color
|
79
|
-
|
80
|
-
%Q{<tr#{row_style}><td style="border-left: 1px solid \#CCC">#{row.left}</td><td style="border-left: 1px solid \#CCC">#{row.right}</td><td style="border-left: 1px solid \#CCC; border-right: 1px solid \#CCC; color: #{text_color}"><pre>#{row.text}</pre></td></tr>\n}
|
81
|
-
end
|
82
|
-
|
83
|
-
def format_classed_row(css_class, row)
|
84
|
-
%Q{<tr class="#{css_class}"><td>#{row.left}</td><td>#{row.left}</td><td><pre>#{row.text}</pre></td></tr>\n}
|
85
|
-
end
|
86
|
-
|
22
|
+
private
|
87
23
|
def parse(lines)
|
88
24
|
@sections = UnifiedDiffParser.new(lines).parse!.sections
|
89
25
|
end
|
90
|
-
|
91
|
-
class OutputRow
|
92
|
-
attr_reader :left, :right
|
93
|
-
|
94
|
-
def initialize(params = {})
|
95
|
-
if params[:code_line].nil?
|
96
|
-
@left, @right, @text = params[:left], params[:right], params[:text]
|
97
|
-
else
|
98
|
-
line = params[:code_line]
|
99
|
-
@left, @right, @text = line.left_line_num, line.right_line_num, line.text
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def text
|
104
|
-
EscapeUtils.escape_html(@text, false)
|
105
|
-
end
|
106
|
-
end
|
107
26
|
end
|
108
27
|
end
|
data/lib/diffed/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Diffed
|
2
|
+
module Html
|
3
|
+
def as_html_table(use_inline_styles=true)
|
4
|
+
html = make_table_tag(use_inline_styles)
|
5
|
+
|
6
|
+
sections.each do |section|
|
7
|
+
html << section.as_html_rows(use_inline_styles)
|
8
|
+
end
|
9
|
+
|
10
|
+
html << "</table>"
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def format_styled_row(bg_color, text_color, row)
|
15
|
+
row_style = bg_color.nil? ? "" : %Q{ style="background-color: #{bg_color}"}
|
16
|
+
text_color = text_color
|
17
|
+
|
18
|
+
%Q{<tr#{row_style}><td style="border-left: 1px solid \#CCC">#{row.left}</td><td style="border-left: 1px solid \#CCC">#{row.right}</td><td style="border-left: 1px solid \#CCC; border-right: 1px solid \#CCC; color: #{text_color}"><pre>#{row.text}</pre></td></tr>\n}
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_classed_row(css_class, row)
|
22
|
+
%Q{<tr class="#{css_class}"><td>#{row.left}</td><td>#{row.left}</td><td><pre>#{row.text}</pre></td></tr>\n}
|
23
|
+
end
|
24
|
+
|
25
|
+
def make_table_tag(inline_styles)
|
26
|
+
if inline_styles
|
27
|
+
table_attributes = %Q{cellpadding="5" style="border-collapse: collapse; border: 1px solid \#CCC; font-family: Consolas, courier, monospace; font-size: 13px; color: #888"}
|
28
|
+
else
|
29
|
+
table_attributes = %Q{class="coloured-diff"}
|
30
|
+
end
|
31
|
+
|
32
|
+
%Q{<table #{table_attributes}>\n}
|
33
|
+
end
|
34
|
+
|
35
|
+
class OutputRow
|
36
|
+
attr_reader :left, :right
|
37
|
+
|
38
|
+
def initialize(params = {})
|
39
|
+
if params[:code_line].nil?
|
40
|
+
@left, @right, @text = params[:left], params[:right], params[:text]
|
41
|
+
else
|
42
|
+
line = params[:code_line]
|
43
|
+
@left, @right, @text = line.left_line_num, line.right_line_num, line.text
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def text
|
48
|
+
EscapeUtils.escape_html(@text, false)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/models/file.rb
CHANGED
data/lib/models/line.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
require 'formatters/html'
|
2
|
+
|
1
3
|
module Diffed
|
2
4
|
class Line
|
5
|
+
include Html
|
3
6
|
attr_reader :type, :text, :left_line_num, :right_line_num, :no_newline
|
4
7
|
|
5
8
|
def initialize(type, text, left_line_num, right_line_num)
|
@@ -17,6 +20,32 @@ module Diffed
|
|
17
20
|
def no_newline= bool
|
18
21
|
# mutability like this kind of sucks, but this one's a pain to avoid.
|
19
22
|
@no_newline = bool
|
20
|
-
end
|
21
|
-
|
23
|
+
end
|
24
|
+
|
25
|
+
def as_html_row(use_inline_styles)
|
26
|
+
format_code_line(use_inline_styles)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def format_code_line(use_inline_styles)
|
31
|
+
row = OutputRow.new(:code_line => self)
|
32
|
+
|
33
|
+
if use_inline_styles
|
34
|
+
format_styled_row code_line_style, '#000', row
|
35
|
+
else
|
36
|
+
format_classed_row type.to_s, row
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def code_line_style
|
41
|
+
case type
|
42
|
+
when :left
|
43
|
+
'#FDD'
|
44
|
+
when :right
|
45
|
+
'#DFD'
|
46
|
+
when :both
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
22
51
|
end
|
data/lib/models/section.rb
CHANGED
@@ -1,9 +1,33 @@
|
|
1
|
+
require 'formatters/html'
|
2
|
+
|
1
3
|
module Diffed
|
2
4
|
class Section
|
5
|
+
include Html
|
3
6
|
attr_reader :header, :lines
|
4
7
|
|
5
8
|
def initialize(header, lines)
|
6
9
|
@header, @lines = header, lines
|
7
|
-
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def as_html_rows(use_inline_styles)
|
13
|
+
html = format_section_header_row(use_inline_styles)
|
14
|
+
lines.each { |line| html << line.as_html_row(use_inline_styles) }
|
15
|
+
html
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def format_section_header_row(use_inline_styles)
|
20
|
+
row = OutputRow.new(:left => "...", :right => "...", :text => header)
|
21
|
+
|
22
|
+
if use_inline_styles
|
23
|
+
format_styled_row '#F0F0FF', '#888', row
|
24
|
+
else
|
25
|
+
format_classed_row 'section', row
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def sections
|
30
|
+
[self]
|
31
|
+
end
|
8
32
|
end
|
9
33
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diffed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-08-01 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: escape_utils
|
16
|
-
requirement: &
|
16
|
+
requirement: &2164278420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2164278420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &2164278000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2164278000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2164277580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2164277580
|
47
47
|
description: ! 'This is a library for creating HTML from a unified diff string, built
|
48
48
|
specifically for the diff section output by "perforce describe -du" or "git show
|
49
49
|
[commit SHA]", but with an eye towards solving a more general problem. It supports
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- diffed.gemspec
|
66
66
|
- lib/diffed.rb
|
67
67
|
- lib/diffed/version.rb
|
68
|
+
- lib/formatters/html.rb
|
68
69
|
- lib/models/file.rb
|
69
70
|
- lib/models/line.rb
|
70
71
|
- lib/models/section.rb
|