dtext: refactor parsing of embedded BUR pseudo tags.

This commit is contained in:
evazion
2020-02-22 01:37:12 -06:00
parent ca1492c8ca
commit c1f7b76bdb
5 changed files with 13 additions and 44 deletions

View File

@@ -7,7 +7,7 @@ class DText
def self.format_text(text, data: nil, **options) def self.format_text(text, data: nil, **options)
return nil if text.nil? return nil if text.nil?
data = preprocess([text]) if data.nil? data = preprocess([text]) if data.nil?
text = parse_embedded_tag_request_text(text) text = parse_embedded_tag_request(text)
html = DTextRagel.parse(text, **options) html = DTextRagel.parse(text, **options)
html = postprocess(html, *data) html = postprocess(html, *data)
html html
@@ -16,7 +16,7 @@ class DText
end end
def self.preprocess(dtext_messages) def self.preprocess(dtext_messages)
dtext_messages = dtext_messages.map { |message| parse_embedded_tag_request_text(message) } dtext_messages = dtext_messages.map { |message| parse_embedded_tag_request(message) }
names = dtext_messages.map { |message| parse_wiki_titles(message) }.flatten.uniq names = dtext_messages.map { |message| parse_wiki_titles(message) }.flatten.uniq
wiki_pages = WikiPage.where(title: names) wiki_pages = WikiPage.where(title: names)
tags = Tag.where(name: names) tags = Tag.where(name: names)
@@ -74,19 +74,20 @@ class DText
"[quote]\n#{creator_name} said:\n\n#{stripped_body}\n[/quote]\n\n" "[quote]\n#{creator_name} said:\n\n#{stripped_body}\n[/quote]\n\n"
end end
def self.parse_embedded_tag_request_text(text) def self.parse_embedded_tag_request(text)
[TagAlias, TagImplication, BulkUpdateRequest].each do |tag_request| text = parse_embedded_tag_request_type(text, TagAlias, /\[ta:(?<id>\d+)\]/m)
text = text.gsub(tag_request.embedded_pattern) do |match| text = parse_embedded_tag_request_type(text, TagImplication, /\[ti:(?<id>\d+)\]/m)
obj = tag_request.find($~[:id]) text = parse_embedded_tag_request_type(text, BulkUpdateRequest, /\[bur:(?<id>\d+)\]/m)
tag_request_message(obj) || match
rescue ActiveRecord::RecordNotFound
match
end
end
text text
end end
def self.parse_embedded_tag_request_type(text, tag_request, pattern)
text.gsub(pattern) do |match|
obj = tag_request.find_by_id($~[:id])
tag_request_message(obj) || match
end
end
def self.tag_request_message(obj) def self.tag_request_message(obj)
if obj.is_a?(TagRelationship) if obj.is_a?(TagRelationship)
if obj.is_approved? if obj.is_approved?

View File

@@ -137,14 +137,6 @@ class BulkUpdateRequest < ApplicationRecord
include ApprovalMethods include ApprovalMethods
include ValidationMethods include ValidationMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[bur:(?<id>\d+)\]/m
end
end
end
def editable?(user) def editable?(user)
user_id == user.id || user.is_builder? user_id == user.id || user.is_builder?
end end

View File

@@ -33,14 +33,6 @@ class TagAlias < TagRelationship
include ApprovalMethods include ApprovalMethods
include ForumMethods include ForumMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[ta:(?<id>\d+)\]/m
end
end
end
def self.to_aliased(names) def self.to_aliased(names)
names = Array(names).map(&:to_s) names = Array(names).map(&:to_s)
return [] if names.empty? return [] if names.empty?

View File

@@ -191,14 +191,6 @@ class TagImplication < TagRelationship
include ValidationMethods include ValidationMethods
include ApprovalMethods include ApprovalMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[ti:(?<id>\d+)\]/m
end
end
end
def reload(options = {}) def reload(options = {})
flush_cache flush_cache
super super

View File

@@ -177,14 +177,6 @@ class TagRelationship < ApplicationRecord
end end
end end
concerning :EmbeddedText do
class_methods do
def embedded_pattern
raise NotImplementedError
end
end
end
def antecedent_and_consequent_are_different def antecedent_and_consequent_are_different
if antecedent_name == consequent_name if antecedent_name == consequent_name
errors[:base] << "Cannot alias or implicate a tag to itself" errors[:base] << "Cannot alias or implicate a tag to itself"