1
0

text_formatter.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. private
  40. def rewrite
  41. entities.sort_by! do |entity|
  42. entity[:indices].first
  43. end
  44. result = ''.dup
  45. last_index = entities.reduce(0) do |index, entity|
  46. indices = entity[:indices]
  47. result << h(text[index...indices.first])
  48. result << yield(entity)
  49. indices.last
  50. end
  51. result << h(text[last_index..-1])
  52. result
  53. end
  54. def link_to_url(entity)
  55. url = Addressable::URI.parse(entity[:url]).to_s
  56. rel = with_rel_me? ? (DEFAULT_REL + %w(me)) : DEFAULT_REL
  57. prefix = url.match(URL_PREFIX_REGEX).to_s
  58. display_url = url[prefix.length, 30]
  59. suffix = url[prefix.length + 30..-1]
  60. cutoff = url[prefix.length..-1].length > 30
  61. <<~HTML.squish
  62. <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>
  63. HTML
  64. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  65. h(entity[:url])
  66. end
  67. def link_to_hashtag(entity)
  68. hashtag = entity[:hashtag]
  69. url = tag_url(hashtag)
  70. <<~HTML.squish
  71. <a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a>
  72. HTML
  73. end
  74. def link_to_mention(entity)
  75. username, domain = entity[:screen_name].split('@')
  76. domain = nil if local_domain?(domain)
  77. account = nil
  78. if preloaded_accounts?
  79. same_username_hits = 0
  80. preloaded_accounts.each do |other_account|
  81. same_username = other_account.username.casecmp(username).zero?
  82. same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero?
  83. if same_username && !same_domain
  84. same_username_hits += 1
  85. elsif same_username && same_domain
  86. account = other_account
  87. end
  88. end
  89. else
  90. account = entity_cache.mention(username, domain)
  91. end
  92. return "@#{h(entity[:screen_name])}" if account.nil?
  93. url = ActivityPub::TagManager.instance.url_for(account)
  94. display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
  95. <<~HTML.squish
  96. <span class="h-card"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
  97. HTML
  98. end
  99. def entity_cache
  100. @entity_cache ||= EntityCache.instance
  101. end
  102. def tag_manager
  103. @tag_manager ||= TagManager.instance
  104. end
  105. delegate :local_domain?, to: :tag_manager
  106. def multiline?
  107. options[:multiline]
  108. end
  109. def with_domains?
  110. options[:with_domains]
  111. end
  112. def with_rel_me?
  113. options[:with_rel_me]
  114. end
  115. def preloaded_accounts
  116. options[:preloaded_accounts]
  117. end
  118. def preloaded_accounts?
  119. preloaded_accounts.present?
  120. end
  121. end