text_formatter.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # frozen_string_literal: true
  2. class TextFormatter
  3. include ActionView::Helpers::TextHelper
  4. include ERB::Util
  5. include RoutingHelper
  6. URL_PREFIX_REGEX = /\A(https?:\/\/(www\.)?|xmpp:)/.freeze
  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 = rewrite do |entity|
  28. if entity[:url]
  29. link_to_url(entity)
  30. elsif entity[:hashtag]
  31. link_to_hashtag(entity)
  32. elsif entity[:screen_name]
  33. link_to_mention(entity)
  34. end
  35. end
  36. html = simple_format(html, {}, sanitize: false).delete("\n") if multiline?
  37. html.html_safe # rubocop:disable Rails/OutputSafety
  38. end
  39. class << self
  40. include ERB::Util
  41. def shortened_link(url, rel_me: false)
  42. url = Addressable::URI.parse(url).to_s
  43. rel = rel_me ? (DEFAULT_REL + %w(me)) : DEFAULT_REL
  44. prefix = url.match(URL_PREFIX_REGEX).to_s
  45. display_url = url[prefix.length, 30]
  46. suffix = url[prefix.length + 30..-1]
  47. cutoff = url[prefix.length..-1].length > 30
  48. <<~HTML.squish.html_safe # rubocop:disable Rails/OutputSafety
  49. <a href="#{h(url)}" target="_blank" rel="#{rel.join(' ')}"><span class="invisible">#{h(prefix)}</span><span class="#{cutoff ? 'ellipsis' : ''}">#{h(display_url)}</span><span class="invisible">#{h(suffix)}</span></a>
  50. HTML
  51. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  52. h(url)
  53. end
  54. end
  55. private
  56. def rewrite
  57. entities.sort_by! do |entity|
  58. entity[:indices].first
  59. end
  60. result = ''.dup
  61. last_index = entities.reduce(0) do |index, entity|
  62. indices = entity[:indices]
  63. result << h(text[index...indices.first])
  64. result << yield(entity)
  65. indices.last
  66. end
  67. result << h(text[last_index..-1])
  68. result
  69. end
  70. def link_to_url(entity)
  71. TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?)
  72. end
  73. def link_to_hashtag(entity)
  74. hashtag = entity[:hashtag]
  75. url = tag_url(hashtag)
  76. <<~HTML.squish
  77. <a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a>
  78. HTML
  79. end
  80. def link_to_mention(entity)
  81. username, domain = entity[:screen_name].split('@')
  82. domain = nil if local_domain?(domain)
  83. account = nil
  84. if preloaded_accounts?
  85. same_username_hits = 0
  86. preloaded_accounts.each do |other_account|
  87. same_username = other_account.username.casecmp(username).zero?
  88. same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero?
  89. if same_username && !same_domain
  90. same_username_hits += 1
  91. elsif same_username && same_domain
  92. account = other_account
  93. end
  94. end
  95. else
  96. account = entity_cache.mention(username, domain)
  97. end
  98. return "@#{h(entity[:screen_name])}" if account.nil?
  99. url = ActivityPub::TagManager.instance.url_for(account)
  100. display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
  101. <<~HTML.squish
  102. <span class="h-card"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
  103. HTML
  104. end
  105. def entity_cache
  106. @entity_cache ||= EntityCache.instance
  107. end
  108. def tag_manager
  109. @tag_manager ||= TagManager.instance
  110. end
  111. delegate :local_domain?, to: :tag_manager
  112. def multiline?
  113. options[:multiline]
  114. end
  115. def with_domains?
  116. options[:with_domains]
  117. end
  118. def with_rel_me?
  119. options[:with_rel_me]
  120. end
  121. def preloaded_accounts
  122. options[:preloaded_accounts]
  123. end
  124. def preloaded_accounts?
  125. preloaded_accounts.present?
  126. end
  127. end