forked from e621ng/e621ng
[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:
parent
7a028cf003
commit
ec776aa0c9
@ -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
|
||||
|
@ -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(/&/, '&').gsub(/</, '<').gsub(/>/, '>')}
|
||||
|
||||
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
|
51
app/helpers/text_helper.rb
Normal file
51
app/helpers/text_helper.rb
Normal 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(/&/, "&").gsub(/</, "<").gsub(/>/, ">") }
|
||||
|
||||
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
|
@ -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(/&/,'&').gsub(/</,'<').gsub(/>/,'>')}
|
||||
|
||||
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
|
@ -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 =~ /^(.+?)\|(.+)/
|
||||
|
@ -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(/&/,'&').gsub(/</,'<').gsub(/>/,'>')}
|
||||
|
||||
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
|
@ -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 %>
|
||||
|
@ -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>
|
||||
|
@ -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" %>
|
||||
|
Loading…
Reference in New Issue
Block a user