[Cleanup] Remove duplicate Diff::LCS code

The wiki page presenter wasn't used, and other_names from wiki page versions
is also unused.
That makes the remaing two implementations functionally the same.
This commit is contained in:
Earlopain 2022-11-26 20:19:23 +01:00
parent 7a028cf003
commit ec776aa0c9
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
9 changed files with 55 additions and 185 deletions

View File

@ -8,7 +8,7 @@ class EditHistoriesController < ApplicationController
end
def show
@edits = EditHistoryDecorator.decorate_collection(EditHistory.includes(:user).where('versionable_id = ? AND versionable_type = ?', params[:id], params[:type]).order(:id))
@edits = EditHistory.includes(:user).where('versionable_id = ? AND versionable_type = ?', params[:id], params[:type]).order(:id)
respond_with(@edits)
end
end

View File

@ -1,58 +0,0 @@
class EditHistoryDecorator < ApplicationDecorator
def self.collection_decorator_class
PaginatedDecorator
end
delegate_all
def diff(other)
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
thisarr = other.body.scan(pattern)
otharr = object.body.scan(pattern)
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = ->(str) {str.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')}
output = thisarr
output.each {|q| q.replace(escape_html[q])}
diffs.reverse_each do |hunk|
newchange = hunk.max {|a, b| a.old_position <=> b.old_position}
newstart = newchange.old_position
oldstart = hunk.min {|a, b| a.old_position <=> b.old_position}.old_position
if newchange.action == '+'
output.insert(newstart, '</ins>')
end
hunk.reverse_each do |chg|
case chg.action
when '-'
oldstart = chg.old_position
output[chg.old_position] = '<br>' if chg.old_element.match(/^\r?\n$/)
when '+'
if chg.new_element.match(/^\r?\n$/)
output.insert(chg.old_position, '<br>')
else
output.insert(chg.old_position, "#{escape_html[chg.new_element]}")
end
end
end
if newchange.action == '+'
output.insert(newstart, '<ins>')
end
if hunk[0].action == '-'
output.insert((newstart == oldstart || newchange.action != '+') ? newstart + 1 : newstart, '</del>')
output.insert(oldstart, '<del>')
end
end
output.join.gsub(/\r?\n/, '<br>').html_safe
end
end

View File

@ -0,0 +1,51 @@
module TextHelper
def lcs_diff(this, that)
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
thisarr = this.scan(pattern)
otharr = that.scan(pattern)
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = ->(str) { str.gsub(/&/, "&amp;").gsub(/</, "&lt;").gsub(/>/, "&gt;") }
output = thisarr
output.each {|q| q.replace(escape_html[q])}
diffs.reverse_each do |hunk|
newchange = hunk.max { |a, b| a.old_position <=> b.old_position }
newstart = newchange.old_position
oldstart = hunk.min { |a, b| a.old_position <=> b.old_position }.old_position
if newchange.action == "+"
output.insert(newstart, "</ins>")
end
hunk.reverse_each do |chg|
case chg.action
when "-"
oldstart = chg.old_position
output[chg.old_position] = "<br>" if chg.old_element.match(/^\r?\n$/)
when "+"
if chg.new_element.match(/^\r?\n$/)
output.insert(chg.old_position, "<br>")
else
output.insert(chg.old_position, escape_html[chg.new_element].to_s)
end
end
end
if newchange.action == "+"
output.insert(newstart, "<ins>")
end
if hunk[0].action == "-"
output.insert(newstart == oldstart || newchange.action != "+" ? newstart + 1 : newstart, "</del>")
output.insert(oldstart, "<del>")
end
end
output.join.gsub(/\r?\n/, "<br>").html_safe
end
end

View File

@ -1,57 +0,0 @@
module WikiPageVersionsHelper
def wiki_page_diff(thispage, otherpage)
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
other_names_pattern = Regexp.new('\S+|\s+')
thisarr = thispage.body.scan(pattern)
otharr = otherpage.body.scan(pattern)
if thispage.other_names.present? || otherpage.other_names.present?
thisarr = "#{thispage.other_names}\n\n".scan(other_names_pattern) + thisarr
otharr = "#{otherpage.other_names}\n\n".scan(other_names_pattern) + otharr
end
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = ->(str) {str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
output = thisarr
output.each { |q| q.replace(escape_html[q]) }
diffs.reverse_each do |hunk|
newchange = hunk.max{|a,b| a.old_position <=> b.old_position}
newstart = newchange.old_position
oldstart = hunk.min{|a,b| a.old_position <=> b.old_position}.old_position
if newchange.action == '+'
output.insert(newstart, '</ins>')
end
hunk.reverse_each do |chg|
case chg.action
when '-'
oldstart = chg.old_position
output[chg.old_position] = '<br>' if chg.old_element.match(/^\r?\n$/)
when '+'
if chg.new_element.match(/^\r?\n$/)
output.insert(chg.old_position, '<br>')
else
output.insert(chg.old_position, "#{escape_html[chg.new_element]}")
end
end
end
if newchange.action == '+'
output.insert(newstart, '<ins>')
end
if hunk[0].action == '-'
output.insert((newstart == oldstart || newchange.action != '+') ? newstart+1 : newstart, '</del>')
output.insert(oldstart, '<del>')
end
end
output.join.gsub(/\r?\n/, '<br>').html_safe
end
end

View File

@ -228,10 +228,6 @@ class WikiPage < ApplicationRecord
@post_set ||= PostSets::Post.new(title, 1, 4)
end
def presenter
@presenter ||= WikiPagePresenter.new(self)
end
def tags
body.scan(/\[\[(.+?)\]\]/).flatten.map do |match|
if match =~ /^(.+?)\|(.+)/

View File

@ -1,62 +0,0 @@
class WikiPagePresenter
attr_reader :wiki_page
def initialize(wiki_page)
@wiki_page = wiki_page
end
def excerpt
wiki_page.body
end
# Produce a formatted page that shows the difference between two versions of a page.
def diff(other_version)
pattern = Regexp.new('(?:<.+?>)|(?:[0-9_A-Za-z\x80-\xff]+[\x09\x20]?)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
thisarr = self.body.scan(pattern)
otharr = other_version.body.scan(pattern)
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = ->(str) {str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
output = thisarr;
output.each { |q| q.replace(CGI.escape_html(q)) }
diffs.reverse_each do |hunk|
newchange = hunk.max{|a,b| a.old_position <=> b.old_position}
newstart = newchange.old_position
oldstart = hunk.min{|a,b| a.old_position <=> b.old_position}.old_position
if newchange.action == '+'
output.insert(newstart, "</ins>")
end
hunk.reverse_each do |chg|
case chg.action
when '-'
oldstart = chg.old_position
output[chg.old_position] = "" if chg.old_element.match(/^\r?\n$/)
when '+'
if chg.new_element.match(/^\r?\n$/)
output.insert(chg.old_position, "[nl]")
else
output.insert(chg.old_position, "#{escape_html[chg.new_element]}")
end
end
end
if newchange.action == '+'
output.insert(newstart, "<ins>")
end
if hunk[0].action == '-'
output.insert((newstart == oldstart || newchange.action != '+') ? newstart+1 : newstart, "</del>")
output.insert(oldstart, "<del>")
end
end
output.join.gsub(/\r?\n/, "[nl]")
end
end

View File

@ -13,7 +13,7 @@
<div class="content">
<div class="body">
<% if edit.version > 1 %>
<%= edit.diff(@edits[idx-1]) %>
<%= lcs_diff(@edits[idx-1].body, edit.body) %>
<% else %>
<%= edit.body %>
<% end %>

View File

@ -6,7 +6,7 @@
<p>Showing differences between <%= compact_time @thispage.updated_at %> (<%= link_to_user @thispage.updater %>) and <%= compact_time @otherpage.updated_at %> (<%= link_to_user @otherpage.updater %>)</p>
<div>
<%= wiki_page_diff(@thispage, @otherpage) %>
<%= lcs_diff(@thispage.body, @otherpage.body) %>
</div>
<% else %>
<p>The artist requested removal of this page.</p>

View File

@ -40,7 +40,7 @@
<% end %>
<% content_for(:html_header) do %>
<meta name="description" content="<%= strip_dtext(@wiki_page.presenter.excerpt) %>"></meta>
<meta name="description" content="<%= strip_dtext(@wiki_page.body) %>">
<% end %>
<%= render "secondary_links" %>