markdiff 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb6772c25f4c97ca2a3db7f2ccf4400e14bbaa7e
4
- data.tar.gz: 4c09fe6b043e5eb4476d57c81cf2dac97c29edfb
3
+ metadata.gz: 57c81d2928ea333fb0183a412d0577140b5f5215
4
+ data.tar.gz: 80e6b8f960bf5b360d65b053f1628b56fa6dbe56
5
5
  SHA512:
6
- metadata.gz: 253e048d69981d9d3ca09c2bf0473524699d1f226ce144d309c18d04377cc7810919c3033f3d879f30eb77681914c1d2ce03e6acd4ab56a0e732530eec5c1b3e
7
- data.tar.gz: d1483244668b83c844b071fe2ccf610a214705a272d42ec26fd6f4c90c396caca1bd778be76f8b7d3059055963e17a0a98af0b8412b7fc13b79e5b2915fafaac
6
+ metadata.gz: 07028cae95a84c32e239c5788acb9a121c4049cb6f87a8ce95302080f8993bd5fe79f57c36e79f8f2d7d62eff3330de9d6ce098a96994a77cf90c5cc6ce57ae5
7
+ data.tar.gz: 31e0e2023ee38182991c981ceb888837b702ce85f16bff101824b9def7d4fa3c46bed9fb678c9f9719f241b052e3b0cdfc3fffe472942eccbe77a75dd398a691
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.4.0
2
+ - Chanage .changed specs
3
+ - Fix bugs on text diff
4
+
1
5
  ## 0.3.0
2
6
  - Support partial text diff
3
7
 
data/README.md CHANGED
@@ -7,7 +7,12 @@ require "markdiff"
7
7
 
8
8
  differ = Markdiff::Differ.new
9
9
  node = differ.render("<p>a</p>", "<p>b</p>")
10
- node.to_html #=> '<div class="changed"><p><del>a</del><ins>b</ins></p></div>'
10
+ node.to_html #=> '<p class="changed"><del>a</del><ins>b</ins></p>'
11
11
  ```
12
12
 
13
13
  See [spec/markdiff/differ_spec.rb](spec/markdiff/differ_spec.rb) for more examples.
14
+
15
+ ### Demo
16
+ Execute `ruby example/app.rb` to run [demo app](example/app.rb).
17
+
18
+ ![demo](images/demo.png)
data/example/app.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "markdiff"
2
+ require "redcarpet"
3
+ require "sinatra"
4
+
5
+ markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
6
+
7
+ get "/" do
8
+ if params[:before] && params[:after]
9
+ html_before = markdown.render(params[:before])
10
+ html_after = markdown.render(params[:after])
11
+ @diff = Markdiff::Differ.new.render(html_before, html_after)
12
+ end
13
+ erb :index
14
+ end
@@ -0,0 +1,99 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Markdiff</title>
6
+ <style>
7
+ a {
8
+ color: #4B81C6;
9
+ text-decoration: none;
10
+ }
11
+
12
+ a ins {
13
+ color: #4B81C6;
14
+ }
15
+
16
+ a[data-before-href] {
17
+ border-bottom: dotted 1px #999;
18
+ text-decoration: none;
19
+ }
20
+
21
+ body {
22
+ line-height: 1.7;
23
+ margin: 48px auto;
24
+ padding-left: 16px;
25
+ padding-right: 16px;
26
+ width: 860px;
27
+ }
28
+
29
+ del {
30
+ background-color: #FFEAEA;
31
+ color: #CB4000;
32
+ }
33
+
34
+ ins {
35
+ background-color: #E9FFE9;
36
+ color: green;
37
+ text-decoration: none;
38
+ }
39
+
40
+ li.added {
41
+ color: #7DCD55;
42
+ }
43
+
44
+ li.removed {
45
+ color: #CB4000;
46
+ }
47
+
48
+ section + section {
49
+ margin-top: 48px;
50
+ }
51
+
52
+ textarea {
53
+ width: 100%;
54
+ }
55
+
56
+ .diff > * {
57
+ padding-left: 32px;
58
+ }
59
+
60
+ .diff > del {
61
+ background-color: inherit;
62
+ border-left: solid 4px #CB4000;
63
+ color: inherit;
64
+ display: block;
65
+ text-decoration: none;
66
+ }
67
+
68
+ .diff > ins {
69
+ background-color: inherit;
70
+ border-left: solid 4px #7DCD55;
71
+ color: inherit;
72
+ display: block;
73
+ }
74
+
75
+ .diff > .changed {
76
+ border-left: solid 4px #FFC134;
77
+ }
78
+ </style>
79
+ </head>
80
+ <body>
81
+ <form action="/" method="get">
82
+ <section>
83
+ <h3>before</h3>
84
+ <textarea name="before" rows="8"><%= params[:before] %></textarea>
85
+ </section>
86
+ <section>
87
+ <h3>after</h3>
88
+ <textarea name="after" rows="8"><%= params[:after] %></textarea>
89
+ <input type="submit" value="OK">
90
+ </section>
91
+ <section>
92
+ <h3>diff</h3>
93
+ <div class="diff">
94
+ <%= @diff %>
95
+ </div>
96
+ </section>
97
+ </form>
98
+ </body>
99
+ </html>
data/images/demo.png ADDED
Binary file
@@ -87,13 +87,24 @@ module Markdiff
87
87
  identity_map = {}
88
88
  inverted_identity_map = {}
89
89
 
90
+ # Exactly matching with index
91
+ before_node.children.each_with_index do |before_child, before_index|
92
+ after_child = after_node.children[before_index]
93
+ if !after_child.nil? && before_child.to_html.gsub("\n", "") == after_child.to_html.gsub("\n", "")
94
+ identity_map[before_child] = after_child
95
+ inverted_identity_map[after_child] = before_child
96
+ end
97
+ end
98
+
90
99
  # Exactly matching
91
100
  before_node.children.each do |before_child|
101
+ next if identity_map[before_child]
92
102
  after_node.children.each do |after_child|
93
- if inverted_identity_map[after_child]
94
- next
95
- end
96
- if before_child.to_html.gsub("\n", "") == after_child.to_html.gsub("\n", "")
103
+ case
104
+ when identity_map[before_child]
105
+ break
106
+ when inverted_identity_map[after_child]
107
+ when before_child.to_html.gsub("\n", "") == after_child.to_html.gsub("\n", "")
97
108
  identity_map[before_child] = after_child
98
109
  inverted_identity_map[after_child] = before_child
99
110
  end
@@ -107,6 +118,8 @@ module Markdiff
107
118
  end
108
119
  after_node.children.each do |after_child|
109
120
  case
121
+ when identity_map[before_child]
122
+ break
110
123
  when inverted_identity_map[after_child]
111
124
  when before_child.text?
112
125
  if after_child.text?
@@ -189,9 +202,7 @@ module Markdiff
189
202
  # @param [Nokogiri::XML::Node] node
190
203
  def mark_top_level_node_as_changed(node)
191
204
  node = node.parent until node.parent.nil? || node.parent.fragment?
192
- unless node.name == "div" && node["class"] == "changed"
193
- node.replace(%(<div class="changed">#{node}</div>))
194
- end
205
+ node["class"] = "changed"
195
206
  end
196
207
  end
197
208
  end
@@ -20,7 +20,7 @@ module Markdiff
20
20
  if type == "-"
21
21
  before_elements[position] = "<del>#{element}</del>"
22
22
  else
23
- before_elements[position] += "<ins>#{element}</ins>"
23
+ before_elements[position] = "#{before_elements[position]}<ins>#{element}</ins>"
24
24
  end
25
25
  end
26
26
  ::Nokogiri::HTML.fragment(before_elements.join(" "))
@@ -1,3 +1,3 @@
1
1
  module Markdiff
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/markdiff.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.add_development_dependency "bundler", "~> 1.10"
17
17
  spec.add_development_dependency "pry", "0.10.3"
18
18
  spec.add_development_dependency "rake", "~> 10.0"
19
+ spec.add_development_dependency "redcarpet", "3.3.3"
19
20
  spec.add_development_dependency "rspec", "3.4.0"
20
21
  spec.add_runtime_dependency "diff-lcs"
21
22
  spec.add_runtime_dependency "nokogiri"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-08 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.3
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,9 @@ files:
108
122
  - LICENSE.txt
109
123
  - README.md
110
124
  - Rakefile
125
+ - example/app.rb
126
+ - example/views/index.erb
127
+ - images/demo.png
111
128
  - lib/markdiff.rb
112
129
  - lib/markdiff/differ.rb
113
130
  - lib/markdiff/operations/add_child_operation.rb