formatter.rb 9.2 KB

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