bangtags: support namespacing, args, text tf + add replace commands
parent
e42f09c53d
commit
d0631f446c
|
@ -578,42 +578,90 @@ class Status < ApplicationRecord
|
||||||
return if text&.nil?
|
return if text&.nil?
|
||||||
return unless '#!'.in?(text)
|
return unless '#!'.in?(text)
|
||||||
chunks = []
|
chunks = []
|
||||||
text.split(/(#!(?:[\w:-]+|{[\w:-]+}))/).each do |chunk|
|
tf_command = nil
|
||||||
|
|
||||||
|
text.split(/(#!(?:[\w:-]+|{.*?}))/).each do |chunk|
|
||||||
if chunk.start_with?("#!")
|
if chunk.start_with?("#!")
|
||||||
chunk.sub!(/{(.*)}$/, '\1')
|
chunk.sub!(/{(.*)}$/, '\1')
|
||||||
case chunk[2..-1].downcase
|
command = chunk[2..-1].split(':')
|
||||||
|
next if command.blank?
|
||||||
|
|
||||||
|
case command[0]
|
||||||
|
when 'tf'
|
||||||
|
tf_command = command[1..-1]
|
||||||
|
when 'end', 'stop'
|
||||||
|
tf_command = nil
|
||||||
|
when 'char'
|
||||||
|
charmap = {
|
||||||
|
'zws': "\u200c"
|
||||||
|
}
|
||||||
|
command[1..-1].each do |c|
|
||||||
|
next if c.nil?
|
||||||
|
if c.in?(charmap)
|
||||||
|
chunks << charmap[command[1]]
|
||||||
|
elsif (/^\h{1,5}$/ =~ c) && c.to_i(16) > 0
|
||||||
|
begin
|
||||||
|
chunks << [c.to_i(16)].pack('U*')
|
||||||
|
rescue
|
||||||
|
chunks << '?'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
when 'permalink'
|
when 'permalink'
|
||||||
chunks << TagManager.instance.url_for(self)
|
chunks << TagManager.instance.url_for(self)
|
||||||
when 'cloudroot'
|
when 'cloudroot'
|
||||||
chunks << "https://monsterpit.cloud/~/#{account.username}"
|
chunks << "https://monsterpit.cloud/~/#{account.username}"
|
||||||
when 'blogroot'
|
when 'blogroot'
|
||||||
chunks << "https://monsterpit.blog/~/#{account.username}"
|
chunks << "https://monsterpit.blog/~/#{account.username}"
|
||||||
when 'ping:admins'
|
when 'ping'
|
||||||
mentions = User.admins.map { |u| "@#{u.account.username}" }
|
case command[1]
|
||||||
mentions.sort!
|
when 'admins'
|
||||||
chunks << mentions.join(' ')
|
mentions = User.admins.map { |u| "@#{u.account.username}" }
|
||||||
when 'ping:mods'
|
mentions.sort!
|
||||||
mentions = User.moderators.map { |u| "@#{u.account.username}" }
|
chunks << mentions.join(' ')
|
||||||
mentions.sort!
|
when 'mods'
|
||||||
chunks << mentions.join(' ')
|
mentions = User.moderators.map { |u| "@#{u.account.username}" }
|
||||||
when 'ping:staff'
|
mentions.sort!
|
||||||
mentions = User.admins.map { |u| "@#{u.account.username}" }
|
chunks << mentions.join(' ')
|
||||||
mentions += User.moderators.map { |u| "@#{u.account.username}" }
|
when 'staff'
|
||||||
mentions.uniq!
|
mentions = User.admins.map { |u| "@#{u.account.username}" }
|
||||||
mentions.sort!
|
mentions += User.moderators.map { |u| "@#{u.account.username}" }
|
||||||
chunks << mentions.join(' ')
|
mentions.uniq!
|
||||||
when 'thread:reall'
|
mentions.sort!
|
||||||
if conversation_id.present?
|
|
||||||
mention_ids = Status.where(conversation_id: conversation_id).flat_map { |s| s.mentions.pluck(:account_id) }
|
|
||||||
mention_ids.uniq!
|
|
||||||
mentions = Account.where(id: mention_ids).map { |a| "@#{a.username}" }
|
|
||||||
chunks << mentions.join(' ')
|
chunks << mentions.join(' ')
|
||||||
end
|
end
|
||||||
when 'char:zws'
|
when 'thread'
|
||||||
chunks << "\u200c"
|
case command[1]
|
||||||
|
when 'reall'
|
||||||
|
if conversation_id.present?
|
||||||
|
mention_ids = Status.where(conversation_id: conversation_id).flat_map { |s| s.mentions.pluck(:account_id) }
|
||||||
|
mention_ids.uniq!
|
||||||
|
mentions = Account.where(id: mention_ids).map { |a| "@#{a.username}" }
|
||||||
|
chunks << mentions.join(' ')
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
chunks << chunk
|
chunks << chunk
|
||||||
end
|
end
|
||||||
|
elsif tf_command.present?
|
||||||
|
case tf_command[0]
|
||||||
|
when 'replace', 'sub', 's'
|
||||||
|
tf_command[1..-1].in_groups_of(2) do |args|
|
||||||
|
if args.all?
|
||||||
|
chunks << chunk.sub(*args)
|
||||||
|
else
|
||||||
|
chunks << chunk
|
||||||
|
end
|
||||||
|
end
|
||||||
|
when 'replaceall', 'gsub', 'gs'
|
||||||
|
tf_command[1..-1].in_groups_of(2) do |args|
|
||||||
|
if args.all?
|
||||||
|
chunks << chunk.gsub(*args)
|
||||||
|
else
|
||||||
|
chunks << chunk
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
chunks << chunk
|
chunks << chunk
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue