text_formatter.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # frozen_string_literal: true
  2. class TextFormatter
  3. include ActionView::Helpers::TextHelper
  4. include ERB::Util
  5. include RoutingHelper
  6. URL_PREFIX_REGEX = %r{\A(https?://(www\.)?|xmpp:)}
  7. DEFAULT_REL = %w(nofollow noopener noreferrer).freeze
  8. DEFAULT_OPTIONS = {
  9. multiline: true,
  10. }.freeze
  11. attr_reader :text, :options
  12. # @param [String] text
  13. # @param [Hash] options
  14. # @option options [Boolean] :multiline
  15. # @option options [Boolean] :with_domains
  16. # @option options [Boolean] :with_rel_me
  17. # @option options [Array<Account>] :preloaded_accounts
  18. def initialize(text, options = {})
  19. @text = text
  20. @options = DEFAULT_OPTIONS.merge(options)
  21. end
  22. def entities
  23. @entities ||= Extractor.extract_entities_with_indices(text, extract_url_without_protocol: false)
  24. end
  25. def to_s
  26. return ''.html_safe if text.blank?
  27. html = nil
  28. MastodonOTELTracer.in_span('TextFormatter#to_s extract_and_rewrite') do
  29. html = rewrite do |entity|
  30. if entity[:url]
  31. link_to_url(entity)
  32. elsif entity[:hashtag]
  33. link_to_hashtag(entity)
  34. elsif entity[:screen_name]
  35. link_to_mention(entity)
  36. end
  37. end
  38. end
  39. if multiline?
  40. MastodonOTELTracer.in_span('TextFormatter#to_s simple_format') do
  41. html = simple_format(html, {}, sanitize: false).delete("\n")
  42. end
  43. end
  44. html.html_safe # rubocop:disable Rails/OutputSafety
  45. end
  46. class << self
  47. include ERB::Util
  48. include ActionView::Helpers::TagHelper
  49. def shortened_link(url, rel_me: false)
  50. url = Addressable::URI.parse(url).to_s
  51. rel = rel_me ? (DEFAULT_REL + %w(me)) : DEFAULT_REL
  52. prefix = url.match(URL_PREFIX_REGEX).to_s
  53. display_url = url[prefix.length, 30]
  54. suffix = url[prefix.length + 30..]
  55. cutoff = url[prefix.length..].length > 30
  56. tag.a href: url, target: '_blank', rel: rel.join(' '), translate: 'no' do
  57. tag.span(prefix, class: 'invisible') +
  58. tag.span(display_url, class: (cutoff ? 'ellipsis' : '')) +
  59. tag.span(suffix, class: 'invisible')
  60. end
  61. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  62. h(url)
  63. end
  64. end
  65. private
  66. def rewrite
  67. entities.sort_by! do |entity|
  68. entity[:indices].first
  69. end
  70. result = +''
  71. last_index = entities.reduce(0) do |index, entity|
  72. indices = entity[:indices]
  73. result << h(text[index...indices.first])
  74. result << yield(entity)
  75. indices.last
  76. end
  77. result << h(text[last_index..])
  78. result
  79. end
  80. def link_to_url(entity)
  81. MastodonOTELTracer.in_span('TextFormatter#link_to_url') do
  82. TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?)
  83. end
  84. end
  85. def link_to_hashtag(entity)
  86. MastodonOTELTracer.in_span('TextFormatter#link_to_hashtag') do
  87. hashtag = entity[:hashtag]
  88. url = tag_url(hashtag)
  89. <<~HTML.squish
  90. <a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a>
  91. HTML
  92. end
  93. end
  94. def link_to_mention(entity)
  95. MastodonOTELTracer.in_span('TextFormatter#link_to_mention') do
  96. username, domain = entity[:screen_name].split('@')
  97. domain = nil if local_domain?(domain)
  98. account = nil
  99. if preloaded_accounts?
  100. same_username_hits = 0
  101. preloaded_accounts.each do |other_account|
  102. same_username = other_account.username.casecmp(username).zero?
  103. same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero?
  104. if same_username && !same_domain
  105. same_username_hits += 1
  106. elsif same_username && same_domain
  107. account = other_account
  108. end
  109. end
  110. else
  111. account = entity_cache.mention(username, domain)
  112. end
  113. return "@#{h(entity[:screen_name])}" if account.nil?
  114. url = ActivityPub::TagManager.instance.url_for(account)
  115. display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
  116. <<~HTML.squish
  117. <span class="h-card" translate="no"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
  118. HTML
  119. end
  120. end
  121. def entity_cache
  122. @entity_cache ||= EntityCache.instance
  123. end
  124. def tag_manager
  125. @tag_manager ||= TagManager.instance
  126. end
  127. delegate :local_domain?, to: :tag_manager
  128. def multiline?
  129. options[:multiline]
  130. end
  131. def with_domains?
  132. options[:with_domains]
  133. end
  134. def with_rel_me?
  135. options[:with_rel_me]
  136. end
  137. def preloaded_accounts
  138. options[:preloaded_accounts]
  139. end
  140. def preloaded_accounts?
  141. preloaded_accounts.present?
  142. end
  143. end