You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
10 KiB

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