markdiff 0.2.1 → 0.3.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: 1701e665032bb63942b695d27e60fd525b45ee03
4
- data.tar.gz: 7e499210e87d6c72e0a72311276fc5459d7a55b0
3
+ metadata.gz: bb6772c25f4c97ca2a3db7f2ccf4400e14bbaa7e
4
+ data.tar.gz: 4c09fe6b043e5eb4476d57c81cf2dac97c29edfb
5
5
  SHA512:
6
- metadata.gz: fadc3f8eec548e6242e5a42953e2a14cca69e5c430d3915f27670a1d7d975d95fefc9d86086a80b5a0fd0787244fd964195d2f0bfdc60c265713f9e4aa686a21
7
- data.tar.gz: 4fb4ee14d8183d2d831aeb3e62b1ccae757fe2d8cbc8e7e7844c763e45b625ea75412b188bb9472091b51ec8d26b644e2832ab8278cf37f919bf73c378595d60
6
+ metadata.gz: 253e048d69981d9d3ca09c2bf0473524699d1f226ce144d309c18d04377cc7810919c3033f3d879f30eb77681914c1d2ce03e6acd4ab56a0e732530eec5c1b3e
7
+ data.tar.gz: d1483244668b83c844b071fe2ccf610a214705a272d42ec26fd6f4c90c396caca1bd778be76f8b7d3059055963e17a0a98af0b8412b7fc13b79e5b2915fafaac
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.3.0
2
+ - Support partial text diff
3
+
1
4
  ## 0.2.1
2
5
  - Support li.added and li.removed
3
6
 
@@ -4,6 +4,7 @@ require "markdiff/operations/add_data_before_href_operation"
4
4
  require "markdiff/operations/add_data_before_tag_name_operation"
5
5
  require "markdiff/operations/add_previous_sibling_operation"
6
6
  require "markdiff/operations/remove_operation"
7
+ require "markdiff/operations/text_diff_operation"
7
8
 
8
9
  module Markdiff
9
10
  class Differ
@@ -36,6 +37,11 @@ module Markdiff
36
37
  operation.target_node.replace(operation.inserted_node) if operation.target_node != operation.inserted_node
37
38
  mark_li_as_changed(operation.target_node)
38
39
  mark_top_level_node_as_changed(operation.target_node)
40
+ when ::Markdiff::Operations::TextDiffOperation
41
+ parent = operation.target_node.parent
42
+ operation.target_node.replace(operation.inserted_node)
43
+ mark_li_as_changed(parent)
44
+ mark_top_level_node_as_changed(parent)
39
45
  end
40
46
  end
41
47
  node
@@ -100,16 +106,22 @@ module Markdiff
100
106
  next
101
107
  end
102
108
  after_node.children.each do |after_child|
103
- next if inverted_identity_map[after_child]
104
- next if before_child.text?
105
- if before_child.name == after_child.name
109
+ case
110
+ when inverted_identity_map[after_child]
111
+ when before_child.text?
112
+ if after_child.text?
113
+ identity_map[before_child] = after_child
114
+ inverted_identity_map[after_child] = before_child
115
+ operations << ::Markdiff::Operations::TextDiffOperation.new(target_node: before_child, after_node: after_child)
116
+ end
117
+ when before_child.name == after_child.name
106
118
  if detect_href_difference(before_child, after_child)
107
119
  operations << ::Markdiff::Operations::AddDataBeforeHrefOperation.new(after_href: after_child["href"], target_node: before_child)
108
120
  end
109
121
  identity_map[before_child] = after_child
110
122
  inverted_identity_map[after_child] = before_child
111
123
  operations += create_patch(before_child, after_child)
112
- elsif detect_heading_level_difference(before_child, after_child)
124
+ when detect_heading_level_difference(before_child, after_child)
113
125
  operations << ::Markdiff::Operations::AddDataBeforeTagNameOperation.new(after_tag_name: after_child.name, target_node: before_child)
114
126
  identity_map[before_child] = after_child
115
127
  inverted_identity_map[after_child] = before_child
@@ -0,0 +1,30 @@
1
+ require "diff/lcs"
2
+ require "nokogiri"
3
+ require "markdiff/operations/base"
4
+
5
+ module Markdiff
6
+ module Operations
7
+ class TextDiffOperation < Base
8
+ # @param [Nokogiri::XML::Node] after_node
9
+ def initialize(after_node:, **args)
10
+ super(**args)
11
+ @after_node = after_node
12
+ end
13
+
14
+ # @return [Nokogiri::XML::Node]
15
+ def inserted_node
16
+ before_elements = target_node.to_s.split(" ")
17
+ after_elements = @after_node.to_s.split(" ")
18
+ ::Diff::LCS.diff(before_elements, after_elements).flatten.each do |operation|
19
+ type, position, element = *operation
20
+ if type == "-"
21
+ before_elements[position] = "<del>#{element}</del>"
22
+ else
23
+ before_elements[position] += "<ins>#{element}</ins>"
24
+ end
25
+ end
26
+ ::Nokogiri::HTML.fragment(before_elements.join(" "))
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Markdiff
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/markdiff.gemspec CHANGED
@@ -17,5 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.add_development_dependency "pry", "0.10.3"
18
18
  spec.add_development_dependency "rake", "~> 10.0"
19
19
  spec.add_development_dependency "rspec", "3.4.0"
20
+ spec.add_runtime_dependency "diff-lcs"
20
21
  spec.add_runtime_dependency "nokogiri"
21
22
  end
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.2.1
4
+ version: 0.3.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-07 00:00:00.000000000 Z
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: diff-lcs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: nokogiri
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +116,7 @@ files:
102
116
  - lib/markdiff/operations/add_previous_sibling_operation.rb
103
117
  - lib/markdiff/operations/base.rb
104
118
  - lib/markdiff/operations/remove_operation.rb
119
+ - lib/markdiff/operations/text_diff_operation.rb
105
120
  - lib/markdiff/version.rb
106
121
  - markdiff.gemspec
107
122
  homepage: https://github.com/r7kamura/markdiff