formatter.rb 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.poll
  17. raw_content = raw_content + "\n\n" + status.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_display_name(account, **options)
  57. html = encode(account.display_name.presence || account.username)
  58. html = encode_custom_emojis(html, account.emojis, options[:autoplay]) if options[:custom_emojify]
  59. html.html_safe # rubocop:disable Rails/OutputSafety
  60. end
  61. def format_field(account, str, **options)
  62. return reformat(str).html_safe unless account.local? # rubocop:disable Rails/OutputSafety
  63. html = encode_and_link_urls(str, me: true)
  64. html = encode_custom_emojis(html, account.emojis, options[:autoplay]) if options[:custom_emojify]
  65. html.html_safe # rubocop:disable Rails/OutputSafety
  66. end
  67. def linkify(text)
  68. html = encode_and_link_urls(text)
  69. html = simple_format(html, {}, sanitize: false)
  70. html = html.delete("\n")
  71. html.html_safe # rubocop:disable Rails/OutputSafety
  72. end
  73. private
  74. def html_entities
  75. @html_entities ||= HTMLEntities.new
  76. end
  77. def encode(html)
  78. html_entities.encode(html)
  79. end
  80. def encode_and_link_urls(html, accounts = nil, options = {})
  81. entities = utf8_friendly_extractor(html, extract_url_without_protocol: false)
  82. if accounts.is_a?(Hash)
  83. options = accounts
  84. accounts = nil
  85. end
  86. rewrite(html.dup, entities) do |entity|
  87. if entity[:url]
  88. link_to_url(entity, options)
  89. elsif entity[:hashtag]
  90. link_to_hashtag(entity)
  91. elsif entity[:screen_name]
  92. link_to_mention(entity, accounts)
  93. end
  94. end
  95. end
  96. def count_tag_nesting(tag)
  97. if tag[1] == '/' then -1
  98. elsif tag[-2] == '/' then 0
  99. else 1
  100. end
  101. end
  102. def encode_custom_emojis(html, emojis, animate = false)
  103. return html if emojis.empty?
  104. emoji_map = if animate
  105. emojis.each_with_object({}) { |e, h| h[e.shortcode] = full_asset_url(e.image.url) }
  106. else
  107. emojis.each_with_object({}) { |e, h| h[e.shortcode] = full_asset_url(e.image.url(:static)) }
  108. end
  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. replacement = "<img draggable=\"false\" class=\"emojione\" alt=\":#{encode(shortcode)}:\" title=\":#{encode(shortcode)}:\" src=\"#{encode(emoji)}\" />"
  121. before_html = shortname_start_index.positive? ? html[0..shortname_start_index - 1] : ''
  122. html = before_html + replacement + html[i + 1..-1]
  123. i += replacement.size - (shortcode.size + 2) - 1
  124. else
  125. i -= 1
  126. end
  127. inside_shortname = false
  128. elsif tag_open_index && html[i] == '>'
  129. tag = html[tag_open_index..i]
  130. tag_open_index = nil
  131. if invisible_depth.positive?
  132. invisible_depth += count_tag_nesting(tag)
  133. elsif tag == '<span class="invisible">'
  134. invisible_depth = 1
  135. end
  136. elsif html[i] == '<'
  137. tag_open_index = i
  138. inside_shortname = false
  139. elsif !tag_open_index && html[i] == ':'
  140. inside_shortname = true
  141. shortname_start_index = i
  142. end
  143. end
  144. html
  145. end
  146. def rewrite(text, entities)
  147. chars = text.to_s.to_char_a
  148. # Sort by start index
  149. entities = entities.sort_by do |entity|
  150. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  151. indices.first
  152. end
  153. result = []
  154. last_index = entities.reduce(0) do |index, entity|
  155. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  156. result << encode(chars[index...indices.first].join)
  157. result << yield(entity)
  158. indices.last
  159. end
  160. result << encode(chars[last_index..-1].join)
  161. result.flatten.join
  162. end
  163. UNICODE_ESCAPE_BLACKLIST_RE = /\p{Z}|\p{P}/
  164. def utf8_friendly_extractor(text, options = {})
  165. old_to_new_index = [0]
  166. escaped = text.chars.map do |c|
  167. output = begin
  168. if c.ord.to_s(16).length > 2 && UNICODE_ESCAPE_BLACKLIST_RE.match(c).nil?
  169. CGI.escape(c)
  170. else
  171. c
  172. end
  173. end
  174. old_to_new_index << old_to_new_index.last + output.length
  175. output
  176. end.join
  177. # Note: I couldn't obtain list_slug with @user/list-name format
  178. # for mention so this requires additional check
  179. special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
  180. # exactly one of :url, :hashtag, :screen_name, :cashtag keys is present
  181. key = (extract.keys & [:url, :hashtag, :screen_name, :cashtag]).first
  182. new_indices = [
  183. old_to_new_index.find_index(extract[:indices].first),
  184. old_to_new_index.find_index(extract[:indices].last),
  185. ]
  186. has_prefix_char = [:hashtag, :screen_name, :cashtag].include?(key)
  187. value_indices = [
  188. new_indices.first + (has_prefix_char ? 1 : 0), # account for #, @ or $
  189. new_indices.last - 1,
  190. ]
  191. next extract.merge(
  192. :indices => new_indices,
  193. key => text[value_indices.first..value_indices.last]
  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.downcase))}\" 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(TagManager.instance.url_for(account))}\" class=\"u-url mention\">@<span>#{encode(account.username)}</span></a></span>"
  235. end
  236. end