formatter.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. require_relative './sanitize_config'
  4. class Formatter
  5. include Singleton
  6. include RoutingHelper
  7. include ActionView::Helpers::TextHelper
  8. def format(status, **options)
  9. if status.reblog?
  10. prepend_reblog = status.reblog.account.acct
  11. status = status.proper
  12. else
  13. prepend_reblog = false
  14. end
  15. raw_content = status.text
  16. if options[:inline_poll_options] && status.preloadable_poll
  17. raw_content = raw_content + "\n\n" + status.preloadable_poll.options.map { |title| "[ ] #{title}" }.join("\n")
  18. end
  19. return '' if raw_content.blank?
  20. unless status.local?
  21. html = reformat(raw_content)
  22. html = encode_custom_emojis(html, status.emojis, options[:autoplay]) if options[:custom_emojify]
  23. return html.html_safe # rubocop:disable Rails/OutputSafety
  24. end
  25. linkable_accounts = status.active_mentions.map(&:account)
  26. linkable_accounts << status.account
  27. html = raw_content
  28. html = "RT @#{prepend_reblog} #{html}" if prepend_reblog
  29. html = encode_and_link_urls(html, linkable_accounts)
  30. html = encode_custom_emojis(html, status.emojis, options[:autoplay]) if options[:custom_emojify]
  31. html = simple_format(html, {}, sanitize: false)
  32. html = html.delete("\n")
  33. html.html_safe # rubocop:disable Rails/OutputSafety
  34. end
  35. def reformat(html)
  36. sanitize(html, Sanitize::Config::MASTODON_STRICT)
  37. rescue ArgumentError
  38. ''
  39. end
  40. def plaintext(status)
  41. return status.text if status.local?
  42. text = status.text.gsub(/(<br \/>|<br>|<\/p>)+/) { |match| "#{match}\n" }
  43. strip_tags(text)
  44. end
  45. def simplified_format(account, **options)
  46. html = account.local? ? linkify(account.note) : reformat(account.note)
  47. html = encode_custom_emojis(html, account.emojis, options[:autoplay]) if options[:custom_emojify]
  48. html.html_safe # rubocop:disable Rails/OutputSafety
  49. end
  50. def sanitize(html, config)
  51. Sanitize.fragment(html, config)
  52. end
  53. def format_spoiler(status, **options)
  54. html = encode(status.spoiler_text)
  55. html = encode_custom_emojis(html, status.emojis, options[:autoplay])
  56. html.html_safe # rubocop:disable Rails/OutputSafety
  57. end
  58. def format_poll_option(status, option, **options)
  59. html = encode(option.title)
  60. html = encode_custom_emojis(html, status.emojis, options[:autoplay])
  61. html.html_safe # rubocop:disable Rails/OutputSafety
  62. end
  63. def format_display_name(account, **options)
  64. html = encode(account.display_name.presence || account.username)
  65. html = encode_custom_emojis(html, account.emojis, options[:autoplay]) if options[:custom_emojify]
  66. html.html_safe # rubocop:disable Rails/OutputSafety
  67. end
  68. def format_field(account, str, **options)
  69. html = account.local? ? encode_and_link_urls(str, me: true) : reformat(str)
  70. html = encode_custom_emojis(html, account.emojis, options[:autoplay]) if options[:custom_emojify]
  71. html.html_safe # rubocop:disable Rails/OutputSafety
  72. end
  73. def linkify(text)
  74. html = encode_and_link_urls(text)
  75. html = simple_format(html, {}, sanitize: false)
  76. html = html.delete("\n")
  77. html.html_safe # rubocop:disable Rails/OutputSafety
  78. end
  79. private
  80. def html_entities
  81. @html_entities ||= HTMLEntities.new
  82. end
  83. def encode(html)
  84. html_entities.encode(html)
  85. end
  86. def encode_and_link_urls(html, accounts = nil, options = {})
  87. entities = utf8_friendly_extractor(html, extract_url_without_protocol: false)
  88. if accounts.is_a?(Hash)
  89. options = accounts
  90. accounts = nil
  91. end
  92. rewrite(html.dup, entities) do |entity|
  93. if entity[:url]
  94. link_to_url(entity, options)
  95. elsif entity[:hashtag]
  96. link_to_hashtag(entity)
  97. elsif entity[:screen_name]
  98. link_to_mention(entity, accounts)
  99. end
  100. end
  101. end
  102. def count_tag_nesting(tag)
  103. if tag[1] == '/' then -1
  104. elsif tag[-2] == '/' then 0
  105. else 1
  106. end
  107. end
  108. def encode_custom_emojis(html, emojis, animate = false)
  109. return html if emojis.empty?
  110. emoji_map = emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] }
  111. i = -1
  112. tag_open_index = nil
  113. inside_shortname = false
  114. shortname_start_index = -1
  115. invisible_depth = 0
  116. while i + 1 < html.size
  117. i += 1
  118. if invisible_depth.zero? && inside_shortname && html[i] == ':'
  119. shortcode = html[shortname_start_index + 1..i - 1]
  120. emoji = emoji_map[shortcode]
  121. if emoji
  122. original_url, static_url = emoji
  123. replacement = begin
  124. if animate
  125. "<img draggable=\"false\" class=\"emojione\" alt=\":#{encode(shortcode)}:\" title=\":#{encode(shortcode)}:\" src=\"#{encode(original_url)}\" />"
  126. else
  127. "<img draggable=\"false\" class=\"emojione custom-emoji\" alt=\":#{encode(shortcode)}:\" title=\":#{encode(shortcode)}:\" src=\"#{encode(static_url)}\" data-original=\"#{original_url}\" data-static=\"#{static_url}\" />"
  128. end
  129. end
  130. before_html = shortname_start_index.positive? ? html[0..shortname_start_index - 1] : ''
  131. html = before_html + replacement + html[i + 1..-1]
  132. i += replacement.size - (shortcode.size + 2) - 1
  133. else
  134. i -= 1
  135. end
  136. inside_shortname = false
  137. elsif tag_open_index && html[i] == '>'
  138. tag = html[tag_open_index..i]
  139. tag_open_index = nil
  140. if invisible_depth.positive?
  141. invisible_depth += count_tag_nesting(tag)
  142. elsif tag == '<span class="invisible">'
  143. invisible_depth = 1
  144. end
  145. elsif html[i] == '<'
  146. tag_open_index = i
  147. inside_shortname = false
  148. elsif !tag_open_index && html[i] == ':'
  149. inside_shortname = true
  150. shortname_start_index = i
  151. end
  152. end
  153. html
  154. end
  155. def rewrite(text, entities)
  156. text = text.to_s
  157. # Sort by start index
  158. entities = entities.sort_by do |entity|
  159. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  160. indices.first
  161. end
  162. result = []
  163. last_index = entities.reduce(0) do |index, entity|
  164. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  165. result << encode(text[index...indices.first])
  166. result << yield(entity)
  167. indices.last
  168. end
  169. result << encode(text[last_index..-1])
  170. result.flatten.join
  171. end
  172. UNICODE_ESCAPE_BLACKLIST_RE = /\p{Z}|\p{P}/
  173. def utf8_friendly_extractor(text, options = {})
  174. old_to_new_index = [0]
  175. escaped = text.chars.map do |c|
  176. output = begin
  177. if c.ord.to_s(16).length > 2 && UNICODE_ESCAPE_BLACKLIST_RE.match(c).nil?
  178. CGI.escape(c)
  179. else
  180. c
  181. end
  182. end
  183. old_to_new_index << old_to_new_index.last + output.length
  184. output
  185. end.join
  186. # Note: I couldn't obtain list_slug with @user/list-name format
  187. # for mention so this requires additional check
  188. special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
  189. new_indices = [
  190. old_to_new_index.find_index(extract[:indices].first),
  191. old_to_new_index.find_index(extract[:indices].last),
  192. ]
  193. next extract.merge(
  194. indices: new_indices,
  195. url: text[new_indices.first..new_indices.last - 1]
  196. )
  197. end
  198. standard = Extractor.extract_entities_with_indices(text, options)
  199. extra = Extractor.extract_extra_uris_with_indices(text, options)
  200. Extractor.remove_overlapping_entities(special + standard + extra)
  201. end
  202. def link_to_url(entity, options = {})
  203. url = Addressable::URI.parse(entity[:url])
  204. html_attrs = { target: '_blank', rel: 'nofollow noopener noreferrer' }
  205. html_attrs[:rel] = "me #{html_attrs[:rel]}" if options[:me]
  206. Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), url, html_attrs)
  207. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  208. encode(entity[:url])
  209. end
  210. def link_to_mention(entity, linkable_accounts)
  211. acct = entity[:screen_name]
  212. return link_to_account(acct) unless linkable_accounts
  213. account = linkable_accounts.find { |item| TagManager.instance.same_acct?(item.acct, acct) }
  214. account ? mention_html(account) : "@#{encode(acct)}"
  215. end
  216. def link_to_account(acct)
  217. username, domain = acct.split('@')
  218. domain = nil if TagManager.instance.local_domain?(domain)
  219. account = EntityCache.instance.mention(username, domain)
  220. account ? mention_html(account) : "@#{encode(acct)}"
  221. end
  222. def link_to_hashtag(entity)
  223. hashtag_html(entity[:hashtag])
  224. end
  225. def link_html(url)
  226. url = Addressable::URI.parse(url).to_s
  227. prefix = url.match(/\A(https?:\/\/(www\.)?|xmpp:)/).to_s
  228. text = url[prefix.length, 30]
  229. suffix = url[prefix.length + 30..-1]
  230. cutoff = url[prefix.length..-1].length > 30
  231. "<span class=\"invisible\">#{encode(prefix)}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{encode(text)}</span><span class=\"invisible\">#{encode(suffix)}</span>"
  232. end
  233. def hashtag_html(tag)
  234. "<a href=\"#{encode(tag_url(tag))}\" class=\"mention hashtag\" rel=\"tag\">#<span>#{encode(tag)}</span></a>"
  235. end
  236. def mention_html(account)
  237. "<span class=\"h-card\"><a href=\"#{encode(ActivityPub::TagManager.instance.url_for(account))}\" class=\"u-url mention\">@<span>#{encode(account.username)}</span></a></span>"
  238. end
  239. end