discourse-diff 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4cb06363e0625bb3f299bafa5a129a87e1e38db
4
+ data.tar.gz: 21805df17067b7bf27874310c954ad5ebd7e0878
5
+ SHA512:
6
+ metadata.gz: bb46193929e2bd974daea80e492e53061ca821920285a800442b5add7e52877016793be8472396bf7200c0b5bcf090cb8a76923a6dc934421646cce7ef2bd0d7
7
+ data.tar.gz: 1db2a9d41c5d4642f1c36fc08b45579308296405e4eec72064886b93123a5792b79ba86cbc618d6a5e724c03ff2165a365148fb8c5bc26ce4daab72e725e2cee
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at james.kiesel@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'nokogiri'
6
+
7
+ # Specify your gem's dependencies in discourse-diff.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ discourse-diff (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ mini_portile2 (2.3.0)
11
+ nokogiri (1.8.2)
12
+ mini_portile2 (~> 2.3.0)
13
+ rake (10.5.0)
14
+ rspec (3.0.0)
15
+ rspec-core (~> 3.0.0)
16
+ rspec-expectations (~> 3.0.0)
17
+ rspec-mocks (~> 3.0.0)
18
+ rspec-core (3.0.4)
19
+ rspec-support (~> 3.0.0)
20
+ rspec-expectations (3.0.4)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.0.0)
23
+ rspec-mocks (3.0.4)
24
+ rspec-support (~> 3.0.0)
25
+ rspec-support (3.0.4)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ discourse-diff!
33
+ nokogiri
34
+ rake (~> 10.0)
35
+ rspec (~> 3.0.0)
36
+
37
+ BUNDLED WITH
38
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 James Kiesel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Discourse::Diff
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/discourse/diff`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'discourse-diff'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install discourse-diff
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/discourse-diff. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Discourse::Diff project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/discourse-diff/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "discourse/diff"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "discourse/diff"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "discourse-diff"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Discourse", "James Kiesel"]
9
+ spec.email = ["contact@discourse.org", "james.kiesel@gmail.com"]
10
+
11
+ spec.summary = %q{Discourse Diff provides inline html diffing for markdown blobs}
12
+ spec.description = %q{This library has been extracted from [Discourse core](https://www.github.com/discourse/discourse)}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.16"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "rspec", ">= 3.0.0"
21
+ end
@@ -0,0 +1,275 @@
1
+ require "discourse/onpdiff"
2
+ require "nokogiri"
3
+
4
+ module Discourse
5
+ class Diff
6
+ MAX_DIFFERENCE = 200
7
+
8
+ def initialize(before, after)
9
+ @before = before
10
+ @after = after
11
+ before_html = tokenize_html_blocks(@before)
12
+ after_html = tokenize_html_blocks(@after)
13
+ before_markdown = tokenize_line(CGI::escapeHTML(@before))
14
+ after_markdown = tokenize_line(CGI::escapeHTML(@after))
15
+
16
+ @block_by_block_diff = ONPDiff.new(before_html, after_html).diff
17
+ @line_by_line_diff = ONPDiff.new(before_markdown, after_markdown).short_diff
18
+ end
19
+
20
+ def inline_html
21
+ i = 0
22
+ inline = []
23
+ while i < @block_by_block_diff.length
24
+ op_code = @block_by_block_diff[i][1]
25
+ if op_code == :common then inline << @block_by_block_diff[i][0]
26
+ else
27
+ if op_code == :delete
28
+ opposite_op_code = :add
29
+ klass = "del"
30
+ first = i
31
+ second = i + 1
32
+ else
33
+ opposite_op_code = :delete
34
+ klass = "ins"
35
+ first = i + 1
36
+ second = i
37
+ end
38
+
39
+ if i + 1 < @block_by_block_diff.length && @block_by_block_diff[i + 1][1] == opposite_op_code
40
+ diff = ONPDiff.new(tokenize_html(@block_by_block_diff[first][0]), tokenize_html(@block_by_block_diff[second][0])).diff
41
+ inline << generate_inline_html(diff)
42
+ i += 1
43
+ else
44
+ inline << add_class_or_wrap_in_tags(@block_by_block_diff[i][0], klass)
45
+ end
46
+ end
47
+ i += 1
48
+ end
49
+
50
+ "<div class=\"inline-diff\">#{inline.join}</div>"
51
+ end
52
+
53
+ def side_by_side_html
54
+ i = 0
55
+ left, right = [], []
56
+ while i < @block_by_block_diff.length
57
+ op_code = @block_by_block_diff[i][1]
58
+ if op_code == :common
59
+ left << @block_by_block_diff[i][0]
60
+ right << @block_by_block_diff[i][0]
61
+ else
62
+ if op_code == :delete
63
+ opposite_op_code = :add
64
+ side = left
65
+ klass = "del"
66
+ first = i
67
+ second = i + 1
68
+ else
69
+ opposite_op_code = :delete
70
+ side = right
71
+ klass = "ins"
72
+ first = i + 1
73
+ second = i
74
+ end
75
+
76
+ if i + 1 < @block_by_block_diff.length && @block_by_block_diff[i + 1][1] == opposite_op_code
77
+ diff = ONPDiff.new(tokenize_html(@block_by_block_diff[first][0]), tokenize_html(@block_by_block_diff[second][0])).diff
78
+ deleted, inserted = generate_side_by_side_html(diff)
79
+ left << deleted
80
+ right << inserted
81
+ i += 1
82
+ else
83
+ side << add_class_or_wrap_in_tags(@block_by_block_diff[i][0], klass)
84
+ end
85
+ end
86
+ i += 1
87
+ end
88
+
89
+ "<div class=\"revision-content\">#{left.join}</div><div class=\"revision-content\">#{right.join}</div>"
90
+ end
91
+
92
+ def side_by_side_markdown
93
+ i = 0
94
+ table = ["<table class=\"markdown\">"]
95
+ while i < @line_by_line_diff.length
96
+ table << "<tr>"
97
+ op_code = @line_by_line_diff[i][1]
98
+ if op_code == :common
99
+ table << "<td>#{@line_by_line_diff[i][0]}</td>"
100
+ table << "<td>#{@line_by_line_diff[i][0]}</td>"
101
+ else
102
+ if op_code == :delete
103
+ opposite_op_code = :add
104
+ first = i
105
+ second = i + 1
106
+ else
107
+ opposite_op_code = :delete
108
+ first = i + 1
109
+ second = i
110
+ end
111
+
112
+ if i + 1 < @line_by_line_diff.length && @line_by_line_diff[i + 1][1] == opposite_op_code
113
+ before_tokens, after_tokens = tokenize_markdown(@line_by_line_diff[first][0]), tokenize_markdown(@line_by_line_diff[second][0])
114
+ if (before_tokens.length - after_tokens.length).abs > MAX_DIFFERENCE
115
+ before_tokens, after_tokens = tokenize_line(@line_by_line_diff[first][0]), tokenize_line(@line_by_line_diff[second][0])
116
+ end
117
+ diff = ONPDiff.new(before_tokens, after_tokens).short_diff
118
+ deleted, inserted = generate_side_by_side_markdown(diff)
119
+ table << "<td class=\"diff-del\">#{deleted.join}</td>"
120
+ table << "<td class=\"diff-ins\">#{inserted.join}</td>"
121
+ i += 1
122
+ else
123
+ if op_code == :delete
124
+ table << "<td class=\"diff-del\">#{@line_by_line_diff[i][0]}</td>"
125
+ table << "<td></td>"
126
+ else
127
+ table << "<td></td>"
128
+ table << "<td class=\"diff-ins\">#{@line_by_line_diff[i][0]}</td>"
129
+ end
130
+ end
131
+ end
132
+ table << "</tr>"
133
+ i += 1
134
+ end
135
+ table << "</table>"
136
+
137
+ table.join
138
+ end
139
+
140
+ private
141
+
142
+ def tokenize_line(text)
143
+ text.scan(/[^\r\n]+[\r\n]*/)
144
+ end
145
+
146
+ def tokenize_markdown(text)
147
+ t, tokens = [], []
148
+ i = 0
149
+ while i < text.length
150
+ if text[i] =~ /\w/
151
+ t << text[i]
152
+ elsif text[i] =~ /[ \t]/ && t.join =~ /^\w+$/
153
+ begin
154
+ t << text[i]
155
+ i += 1
156
+ end while i < text.length && text[i] =~ /[ \t]/
157
+ i -= 1
158
+ tokens << t.join
159
+ t = []
160
+ else
161
+ tokens << t.join if t.length > 0
162
+ tokens << text[i]
163
+ t = []
164
+ end
165
+ i += 1
166
+ end
167
+ tokens << t.join if t.length > 0
168
+ tokens
169
+ end
170
+
171
+ def tokenize_html_blocks(html)
172
+ Nokogiri::HTML.fragment(html).search("./*").map(&:to_html)
173
+ end
174
+
175
+ def tokenize_html(html)
176
+ HtmlTokenizer.tokenize(html)
177
+ end
178
+
179
+ def add_class_or_wrap_in_tags(html_or_text, klass)
180
+ index_of_next_chevron = html_or_text.index(">")
181
+ if html_or_text.length > 0 && html_or_text[0] == '<' && index_of_next_chevron
182
+ index_of_class = html_or_text.index("class=")
183
+ if index_of_class.nil? || index_of_class > index_of_next_chevron
184
+ # we do not have a class for the current tag
185
+ # add it right before the ">"
186
+ html_or_text.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
187
+ else
188
+ # we have a class, insert it at the beginning if not already present
189
+ classes = html_or_text[/class=(["'])([^\1]*)\1/, 2]
190
+ if classes.include?("diff-#{klass}")
191
+ html_or_text
192
+ else
193
+ html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
194
+ end
195
+ end
196
+ else
197
+ "<#{klass}>#{html_or_text}</#{klass}>"
198
+ end
199
+ end
200
+
201
+ def generate_inline_html(diff)
202
+ inline = []
203
+ diff.each do |d|
204
+ case d[1]
205
+ when :common then inline << d[0]
206
+ when :delete then inline << add_class_or_wrap_in_tags(d[0], "del")
207
+ when :add then inline << add_class_or_wrap_in_tags(d[0], "ins")
208
+ end
209
+ end
210
+ inline
211
+ end
212
+
213
+ def generate_side_by_side_html(diff)
214
+ deleted, inserted = [], []
215
+ diff.each do |d|
216
+ case d[1]
217
+ when :common
218
+ deleted << d[0]
219
+ inserted << d[0]
220
+ when :delete then deleted << add_class_or_wrap_in_tags(d[0], "del")
221
+ when :add then inserted << add_class_or_wrap_in_tags(d[0], "ins")
222
+ end
223
+ end
224
+ [deleted, inserted]
225
+ end
226
+
227
+ def generate_side_by_side_markdown(diff)
228
+ deleted, inserted = [], []
229
+ diff.each do |d|
230
+ case d[1]
231
+ when :common
232
+ deleted << d[0]
233
+ inserted << d[0]
234
+ when :delete then deleted << "<del>#{d[0]}</del>"
235
+ when :add then inserted << "<ins>#{d[0]}</ins>"
236
+ end
237
+ end
238
+ [deleted, inserted]
239
+ end
240
+
241
+ class HtmlTokenizer < Nokogiri::XML::SAX::Document
242
+
243
+ attr_accessor :tokens
244
+
245
+ def initialize
246
+ @tokens = []
247
+ end
248
+
249
+ def self.tokenize(html)
250
+ me = new
251
+ parser = Nokogiri::HTML::SAX::Parser.new(me)
252
+ parser.parse("<html><body>#{html}</body></html>")
253
+ me.tokens
254
+ end
255
+
256
+ USELESS_TAGS = %w{html body}
257
+ def start_element(name, attributes = [])
258
+ return if USELESS_TAGS.include?(name)
259
+ attrs = attributes.map { |a| " #{a[0]}=\"#{a[1]}\"" }.join
260
+ @tokens << "<#{name}#{attrs}>"
261
+ end
262
+
263
+ AUTOCLOSING_TAGS = %w{area base br col embed hr img input meta}
264
+ def end_element(name)
265
+ return if USELESS_TAGS.include?(name) || AUTOCLOSING_TAGS.include?(name)
266
+ @tokens << "</#{name}>"
267
+ end
268
+
269
+ def characters(string)
270
+ @tokens.concat string.scan(/\W|\w+[ \t]*/).map { |x| CGI::escapeHTML(x) }
271
+ end
272
+
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,153 @@
1
+ # Use "An O(NP) Sequence Comparison Algorithm" as described by Sun Wu, Udi Manber and Gene Myers
2
+ # in http://www.itu.dk/stud/speciale/bepjea/xwebtex/litt/an-onp-sequence-comparison-algorithm.pdf
3
+ class ONPDiff
4
+
5
+ def initialize(a, b)
6
+ @a, @b = a, b
7
+ @m, @n = a.length, b.length
8
+ @backtrack = []
9
+ if @reverse = @m > @n
10
+ @a, @b = @b, @a
11
+ @m, @n = @n, @m
12
+ end
13
+ @offset = @m + 1
14
+ @delta = @n - @m
15
+ end
16
+
17
+ def diff
18
+ @diff ||= build_edit_script(compose)
19
+ end
20
+
21
+ def short_diff
22
+ @short_diff ||= build_short_edit_script(compose)
23
+ end
24
+
25
+ private
26
+
27
+ def compose
28
+ return @shortest_path if @shortest_path
29
+
30
+ size = @m + @n + 3
31
+ fp = Array.new(size) { |i| -1 }
32
+ @path = Array.new(size) { |i| -1 }
33
+ p = -1
34
+
35
+ begin
36
+ p += 1
37
+
38
+ k = -p
39
+ while k <= @delta - 1
40
+ fp[k + @offset] = snake(k, fp[k - 1 + @offset] + 1, fp[k + 1 + @offset])
41
+ k += 1
42
+ end
43
+
44
+ k = @delta + p
45
+ while k >= @delta + 1
46
+ fp[k + @offset] = snake(k, fp[k - 1 + @offset] + 1, fp[k + 1 + @offset])
47
+ k -= 1
48
+ end
49
+
50
+ fp[@delta + @offset] = snake(@delta, fp[@delta - 1 + @offset] + 1, fp[@delta + 1 + @offset])
51
+
52
+ end until fp[@delta + @offset] == @n
53
+
54
+ r = @path[@delta + @offset]
55
+
56
+ @shortest_path = []
57
+ while r != -1
58
+ @shortest_path << [@backtrack[r][0], @backtrack[r][1]]
59
+ r = @backtrack[r][2]
60
+ end
61
+
62
+ @shortest_path
63
+ end
64
+
65
+ def snake(k, p, pp)
66
+ r = p > pp ? @path[k - 1 + @offset] : @path[k + 1 + @offset]
67
+ y = [p, pp].max
68
+ x = y - k
69
+
70
+ while x < @m && y < @n && @a[x] == @b[y]
71
+ x += 1
72
+ y += 1
73
+ end
74
+
75
+ @path[k + @offset] = @backtrack.length
76
+ @backtrack << [x, y, r]
77
+
78
+ y
79
+ end
80
+
81
+ def build_edit_script(shortest_path)
82
+ ses = []
83
+ x, y = 1, 1
84
+ px, py = 0, 0
85
+ i = shortest_path.length - 1
86
+ while i >= 0
87
+ while px < shortest_path[i][0] || py < shortest_path[i][1]
88
+ if shortest_path[i][1] - shortest_path[i][0] > py - px
89
+ t = @reverse ? :delete : :add
90
+ ses << [@b[py], t]
91
+ y += 1
92
+ py += 1
93
+ elsif shortest_path[i][1] - shortest_path[i][0] < py - px
94
+ t = @reverse ? :add : :delete
95
+ ses << [@a[px], t]
96
+ x += 1
97
+ px += 1
98
+ else
99
+ ses << [@a[px], :common]
100
+ x += 1
101
+ y += 1
102
+ px += 1
103
+ py += 1
104
+ end
105
+ end
106
+ i -= 1
107
+ end
108
+ ses
109
+ end
110
+
111
+ def build_short_edit_script(shortest_path)
112
+ ses = []
113
+ x, y = 1, 1
114
+ px, py = 0, 0
115
+ i = shortest_path.length - 1
116
+ while i >= 0
117
+ while px < shortest_path[i][0] || py < shortest_path[i][1]
118
+ if shortest_path[i][1] - shortest_path[i][0] > py - px
119
+ t = @reverse ? :delete : :add
120
+ if ses.length > 0 && ses[-1][1] == t
121
+ ses[-1][0] << @b[py]
122
+ else
123
+ ses << [@b[py], t]
124
+ end
125
+ y += 1
126
+ py += 1
127
+ elsif shortest_path[i][1] - shortest_path[i][0] < py - px
128
+ t = @reverse ? :add : :delete
129
+ if ses.length > 0 && ses[-1][1] == t
130
+ ses[-1][0] << @a[px]
131
+ else
132
+ ses << [@a[px], t]
133
+ end
134
+ x += 1
135
+ px += 1
136
+ else
137
+ if ses.length > 0 && ses[-1][1] == :common
138
+ ses[-1][0] << @a[px]
139
+ else
140
+ ses << [@a[px], :common]
141
+ end
142
+ x += 1
143
+ y += 1
144
+ px += 1
145
+ py += 1
146
+ end
147
+ end
148
+ i -= 1
149
+ end
150
+ ses
151
+ end
152
+
153
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discourse-diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Discourse
8
+ - James Kiesel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 3.0.0
56
+ description: This library has been extracted from [Discourse core](https://www.github.com/discourse/discourse)
57
+ email:
58
+ - contact@discourse.org
59
+ - james.kiesel@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - discourse-diff.gemspec
75
+ - lib/discourse/diff.rb
76
+ - lib/discourse/onpdiff.rb
77
+ homepage:
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Discourse Diff provides inline html diffing for markdown blobs
101
+ test_files: []