Merge pull request #2998 from evazion/fix-quote-stripping
Fix [quote] stripping for comments, forum post replies.
This commit is contained in:
@@ -18,22 +18,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Danbooru.Comment.quote_message = function(data) {
|
|
||||||
var blocks = data["body"].match(/\[\/?quote\]|.|\n|\r/gm);
|
|
||||||
var n = 0;
|
|
||||||
var stripped_body = "";
|
|
||||||
$.each(blocks, function(i, block) {
|
|
||||||
if (block === "[quote]") {
|
|
||||||
n += 1;
|
|
||||||
} else if (block == "[/quote]") {
|
|
||||||
n -= 1;
|
|
||||||
} else if (n === 0) {
|
|
||||||
stripped_body += block;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return "[quote]\n" + data["creator_name"] + " said:\n\n" + stripped_body + "\n[/quote]\n\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
Danbooru.Comment.quote = function(e) {
|
Danbooru.Comment.quote = function(e) {
|
||||||
$.get(
|
$.get(
|
||||||
"/comments/" + $(e.target).data('comment-id') + ".json",
|
"/comments/" + $(e.target).data('comment-id') + ".json",
|
||||||
@@ -41,7 +25,7 @@
|
|||||||
var $link = $(e.target);
|
var $link = $(e.target);
|
||||||
var $div = $link.closest("div.comments-for-post").find(".new-comment");
|
var $div = $link.closest("div.comments-for-post").find(".new-comment");
|
||||||
var $textarea = $div.find("textarea");
|
var $textarea = $div.find("textarea");
|
||||||
var msg = Danbooru.Comment.quote_message(data);
|
var msg = data["quoted_response"];
|
||||||
if ($textarea.val().length > 0) {
|
if ($textarea.val().length > 0) {
|
||||||
msg = $textarea.val() + "\n\n" + msg;
|
msg = $textarea.val() + "\n\n" + msg;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class CommentsController < ApplicationController
|
|||||||
|
|
||||||
def show
|
def show
|
||||||
@comment = Comment.find(params[:id])
|
@comment = Comment.find(params[:id])
|
||||||
respond_with(@comment)
|
respond_with(@comment, methods: [:quoted_response])
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|||||||
@@ -12,11 +12,22 @@ class DText
|
|||||||
CGI.escapeHTML(string)
|
CGI.escapeHTML(string)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.quote(message, creator_name)
|
||||||
|
stripped_body = DText.strip_blocks(message, "quote")
|
||||||
|
"[quote]\n#{creator_name} said:\n\n#{stripped_body}\n[/quote]\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
def self.strip_blocks(string, tag)
|
def self.strip_blocks(string, tag)
|
||||||
blocks = string.scan(/\[\/?#{tag}\]|.+?(?=\[\/?#{tag}\]|$)/m)
|
|
||||||
n = 0
|
n = 0
|
||||||
stripped = ""
|
stripped = ""
|
||||||
blocks.each do |block|
|
string = string.dup
|
||||||
|
|
||||||
|
string.gsub!(/\s*\[#{tag}\](?!\])\s*/mi, "\n\n[#{tag}]\n\n")
|
||||||
|
string.gsub!(/\s*\[\/#{tag}\]\s*/mi, "\n\n[/#{tag}]\n\n")
|
||||||
|
string.gsub!(/(?:\r?\n){3,}/, "\n\n")
|
||||||
|
string.strip!
|
||||||
|
|
||||||
|
string.split(/\n{2}/).each do |block|
|
||||||
case block
|
case block
|
||||||
when "[#{tag}]"
|
when "[#{tag}]"
|
||||||
n += 1
|
n += 1
|
||||||
@@ -26,7 +37,7 @@ class DText
|
|||||||
|
|
||||||
else
|
else
|
||||||
if n == 0
|
if n == 0
|
||||||
stripped += block
|
stripped << "#{block}\n\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,31 +16,10 @@ module Mentionable
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def strip_quote_blocks(str)
|
|
||||||
stripped = ""
|
|
||||||
str.gsub!(/\s*\[quote\](?!\])\s*/m, "\n\n[quote]\n\n")
|
|
||||||
str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n")
|
|
||||||
str.gsub!(/(?:\r?\n){3,}/, "\n\n")
|
|
||||||
str.strip!
|
|
||||||
nest = 0
|
|
||||||
str.split(/\n{2}/).each do |block|
|
|
||||||
if block == "[quote]"
|
|
||||||
nest += 1
|
|
||||||
|
|
||||||
elsif block == "[/quote]"
|
|
||||||
nest -= 1
|
|
||||||
|
|
||||||
elsif nest == 0
|
|
||||||
stripped << "#{block}\n"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
stripped
|
|
||||||
end
|
|
||||||
|
|
||||||
def queue_mention_messages
|
def queue_mention_messages
|
||||||
from_id = read_attribute(self.class.mentionable_option(:user_field))
|
from_id = read_attribute(self.class.mentionable_option(:user_field))
|
||||||
text = strip_quote_blocks(read_attribute(self.class.mentionable_option(:message_field)))
|
text = read_attribute(self.class.mentionable_option(:message_field))
|
||||||
|
text = DText.strip_blocks(text, "quote")
|
||||||
|
|
||||||
names = text.scan(DText::MENTION_REGEXP).map do |mention|
|
names = text.scan(DText::MENTION_REGEXP).map do |mention|
|
||||||
mention.gsub!(/(?:^\s*@)|(?:[:;,.!?\)\]<>]$)/, "")
|
mention.gsub!(/(?:^\s*@)|(?:[:;,.!?\)\]<>]$)/, "")
|
||||||
|
|||||||
@@ -249,6 +249,10 @@ class Comment < ActiveRecord::Base
|
|||||||
def undelete!
|
def undelete!
|
||||||
update({ :is_deleted => false }, :as => CurrentUser.role)
|
update({ :is_deleted => false }, :as => CurrentUser.role)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def quoted_response
|
||||||
|
DText.quote(body, creator_name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Comment.connection.extend(PostgresExtensions)
|
Comment.connection.extend(PostgresExtensions)
|
||||||
|
|||||||
@@ -234,8 +234,7 @@ class ForumPost < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def quoted_response
|
def quoted_response
|
||||||
stripped_body = DText.strip_blocks(body, "quote")
|
DText.quote(body, creator_name)
|
||||||
"[quote]\n#{creator_name} said:\n\n#{stripped_body}\n[/quote]\n\n"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def forum_topic_page
|
def forum_topic_page
|
||||||
|
|||||||
@@ -243,6 +243,33 @@ class CommentTest < ActiveSupport::TestCase
|
|||||||
assert_equal([comment], post.comments.visible(user))
|
assert_equal([comment], post.comments.visible(user))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "that is quoted" do
|
||||||
|
should "strip [quote] tags correctly" do
|
||||||
|
comment = FactoryGirl.create(:comment, body: <<-EOS.strip_heredoc)
|
||||||
|
paragraph one
|
||||||
|
|
||||||
|
[quote]
|
||||||
|
somebody said:
|
||||||
|
|
||||||
|
blah blah blah
|
||||||
|
[/QUOTE]
|
||||||
|
|
||||||
|
paragraph two
|
||||||
|
EOS
|
||||||
|
|
||||||
|
assert_equal(<<-EOS.strip_heredoc, comment.quoted_response)
|
||||||
|
[quote]
|
||||||
|
#{comment.creator_name} said:
|
||||||
|
|
||||||
|
paragraph one
|
||||||
|
|
||||||
|
paragraph two
|
||||||
|
[/quote]
|
||||||
|
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user