2022-03-26 01:53:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TextFormatter
|
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ERB::Util
|
|
|
|
include RoutingHelper
|
|
|
|
|
2023-06-06 12:50:51 +00:00
|
|
|
URL_PREFIX_REGEX = %r{\A(https?://(www\.)?|xmpp:)}
|
2022-03-26 01:53:34 +00:00
|
|
|
|
|
|
|
DEFAULT_REL = %w(nofollow noopener noreferrer).freeze
|
|
|
|
|
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
multiline: true,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
attr_reader :text, :options
|
|
|
|
|
|
|
|
# @param [String] text
|
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Boolean] :multiline
|
|
|
|
# @option options [Boolean] :with_domains
|
|
|
|
# @option options [Boolean] :with_rel_me
|
|
|
|
# @option options [Array<Account>] :preloaded_accounts
|
|
|
|
def initialize(text, options = {})
|
|
|
|
@text = text
|
|
|
|
@options = DEFAULT_OPTIONS.merge(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def entities
|
|
|
|
@entities ||= Extractor.extract_entities_with_indices(text, extract_url_without_protocol: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return ''.html_safe if text.blank?
|
|
|
|
|
|
|
|
html = rewrite do |entity|
|
|
|
|
if entity[:url]
|
|
|
|
link_to_url(entity)
|
|
|
|
elsif entity[:hashtag]
|
|
|
|
link_to_hashtag(entity)
|
|
|
|
elsif entity[:screen_name]
|
|
|
|
link_to_mention(entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
html = simple_format(html, {}, sanitize: false).delete("\n") if multiline?
|
|
|
|
|
|
|
|
html.html_safe # rubocop:disable Rails/OutputSafety
|
|
|
|
end
|
|
|
|
|
2023-07-06 13:06:49 +00:00
|
|
|
class << self
|
|
|
|
include ERB::Util
|
|
|
|
|
|
|
|
def shortened_link(url, rel_me: false)
|
|
|
|
url = Addressable::URI.parse(url).to_s
|
|
|
|
rel = rel_me ? (DEFAULT_REL + %w(me)) : DEFAULT_REL
|
|
|
|
|
|
|
|
prefix = url.match(URL_PREFIX_REGEX).to_s
|
|
|
|
display_url = url[prefix.length, 30]
|
2023-07-12 08:03:06 +00:00
|
|
|
suffix = url[prefix.length + 30..]
|
|
|
|
cutoff = url[prefix.length..].length > 30
|
2023-07-06 13:06:49 +00:00
|
|
|
|
2023-07-07 16:10:17 +00:00
|
|
|
<<~HTML.squish.html_safe # rubocop:disable Rails/OutputSafety
|
2023-07-06 13:06:49 +00:00
|
|
|
<a href="#{h(url)}" target="_blank" rel="#{rel.join(' ')}" translate="no"><span class="invisible">#{h(prefix)}</span><span class="#{cutoff ? 'ellipsis' : ''}">#{h(display_url)}</span><span class="invisible">#{h(suffix)}</span></a>
|
|
|
|
HTML
|
|
|
|
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
|
|
|
h(url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-26 01:53:34 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def rewrite
|
|
|
|
entities.sort_by! do |entity|
|
|
|
|
entity[:indices].first
|
|
|
|
end
|
|
|
|
|
|
|
|
result = ''.dup
|
|
|
|
|
|
|
|
last_index = entities.reduce(0) do |index, entity|
|
|
|
|
indices = entity[:indices]
|
|
|
|
result << h(text[index...indices.first])
|
|
|
|
result << yield(entity)
|
|
|
|
indices.last
|
|
|
|
end
|
|
|
|
|
2023-07-12 08:03:06 +00:00
|
|
|
result << h(text[last_index..])
|
2022-03-26 01:53:34 +00:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_url(entity)
|
2023-07-06 13:06:49 +00:00
|
|
|
TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?)
|
2022-03-26 01:53:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_hashtag(entity)
|
|
|
|
hashtag = entity[:hashtag]
|
|
|
|
url = tag_url(hashtag)
|
|
|
|
|
|
|
|
<<~HTML.squish
|
|
|
|
<a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_mention(entity)
|
|
|
|
username, domain = entity[:screen_name].split('@')
|
|
|
|
domain = nil if local_domain?(domain)
|
|
|
|
account = nil
|
|
|
|
|
|
|
|
if preloaded_accounts?
|
|
|
|
same_username_hits = 0
|
|
|
|
|
|
|
|
preloaded_accounts.each do |other_account|
|
|
|
|
same_username = other_account.username.casecmp(username).zero?
|
|
|
|
same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero?
|
|
|
|
|
|
|
|
if same_username && !same_domain
|
|
|
|
same_username_hits += 1
|
|
|
|
elsif same_username && same_domain
|
|
|
|
account = other_account
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
account = entity_cache.mention(username, domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
return "@#{h(entity[:screen_name])}" if account.nil?
|
|
|
|
|
|
|
|
url = ActivityPub::TagManager.instance.url_for(account)
|
|
|
|
display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
|
|
|
|
|
|
|
|
<<~HTML.squish
|
2023-06-20 16:10:19 +00:00
|
|
|
<span class="h-card" translate="no"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
|
2022-03-26 01:53:34 +00:00
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
def entity_cache
|
|
|
|
@entity_cache ||= EntityCache.instance
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_manager
|
|
|
|
@tag_manager ||= TagManager.instance
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :local_domain?, to: :tag_manager
|
|
|
|
|
|
|
|
def multiline?
|
|
|
|
options[:multiline]
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_domains?
|
|
|
|
options[:with_domains]
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_rel_me?
|
|
|
|
options[:with_rel_me]
|
|
|
|
end
|
|
|
|
|
|
|
|
def preloaded_accounts
|
|
|
|
options[:preloaded_accounts]
|
|
|
|
end
|
|
|
|
|
|
|
|
def preloaded_accounts?
|
|
|
|
preloaded_accounts.present?
|
|
|
|
end
|
|
|
|
end
|