1
0

formatter.rb 9.4 KB

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