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.

303 lines
9.2 KiB

7 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. 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 = emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] }
  110. i = -1
  111. tag_open_index = nil
  112. inside_shortname = false
  113. shortname_start_index = -1
  114. invisible_depth = 0
  115. while i + 1 < html.size
  116. i += 1
  117. if invisible_depth.zero? && inside_shortname && html[i] == ':'
  118. shortcode = html[shortname_start_index + 1..i - 1]
  119. emoji = emoji_map[shortcode]
  120. if emoji
  121. original_url, static_url = emoji
  122. replacement = begin
  123. if animate
  124. "<img draggable=\"false\" class=\"emojione\" alt=\":#{encode(shortcode)}:\" title=\":#{encode(shortcode)}:\" src=\"#{encode(original_url)}\" />"
  125. else
  126. "<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}\" />"
  127. end
  128. end
  129. before_html = shortname_start_index.positive? ? html[0..shortname_start_index - 1] : ''
  130. html = before_html + replacement + html[i + 1..-1]
  131. i += replacement.size - (shortcode.size + 2) - 1
  132. else
  133. i -= 1
  134. end
  135. inside_shortname = false
  136. elsif tag_open_index && html[i] == '>'
  137. tag = html[tag_open_index..i]
  138. tag_open_index = nil
  139. if invisible_depth.positive?
  140. invisible_depth += count_tag_nesting(tag)
  141. elsif tag == '<span class="invisible">'
  142. invisible_depth = 1
  143. end
  144. elsif html[i] == '<'
  145. tag_open_index = i
  146. inside_shortname = false
  147. elsif !tag_open_index && html[i] == ':'
  148. inside_shortname = true
  149. shortname_start_index = i
  150. end
  151. end
  152. html
  153. end
  154. def rewrite(text, entities)
  155. text = text.to_s
  156. # Sort by start index
  157. entities = entities.sort_by do |entity|
  158. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  159. indices.first
  160. end
  161. result = []
  162. last_index = entities.reduce(0) do |index, entity|
  163. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  164. result << encode(text[index...indices.first])
  165. result << yield(entity)
  166. indices.last
  167. end
  168. result << encode(text[last_index..-1])
  169. result.flatten.join
  170. end
  171. UNICODE_ESCAPE_BLACKLIST_RE = /\p{Z}|\p{P}/
  172. def utf8_friendly_extractor(text, options = {})
  173. old_to_new_index = [0]
  174. escaped = text.chars.map do |c|
  175. output = begin
  176. if c.ord.to_s(16).length > 2 && UNICODE_ESCAPE_BLACKLIST_RE.match(c).nil?
  177. CGI.escape(c)
  178. else
  179. c
  180. end
  181. end
  182. old_to_new_index << old_to_new_index.last + output.length
  183. output
  184. end.join
  185. # Note: I couldn't obtain list_slug with @user/list-name format
  186. # for mention so this requires additional check
  187. special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
  188. new_indices = [
  189. old_to_new_index.find_index(extract[:indices].first),
  190. old_to_new_index.find_index(extract[:indices].last),
  191. ]
  192. next extract.merge(
  193. indices: new_indices,
  194. url: text[new_indices.first..new_indices.last - 1]
  195. )
  196. end
  197. standard = Extractor.extract_entities_with_indices(text, options)
  198. Extractor.remove_overlapping_entities(special + standard)
  199. end
  200. def link_to_url(entity, options = {})
  201. url = Addressable::URI.parse(entity[:url])
  202. html_attrs = { target: '_blank', rel: 'nofollow noopener' }
  203. html_attrs[:rel] = "me #{html_attrs[:rel]}" if options[:me]
  204. Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), url, html_attrs)
  205. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  206. encode(entity[:url])
  207. end
  208. def link_to_mention(entity, linkable_accounts)
  209. acct = entity[:screen_name]
  210. return link_to_account(acct) unless linkable_accounts
  211. account = linkable_accounts.find { |item| TagManager.instance.same_acct?(item.acct, acct) }
  212. account ? mention_html(account) : "@#{encode(acct)}"
  213. end
  214. def link_to_account(acct)
  215. username, domain = acct.split('@')
  216. domain = nil if TagManager.instance.local_domain?(domain)
  217. account = EntityCache.instance.mention(username, domain)
  218. account ? mention_html(account) : "@#{encode(acct)}"
  219. end
  220. def link_to_hashtag(entity)
  221. hashtag_html(entity[:hashtag])
  222. end
  223. def link_html(url)
  224. url = Addressable::URI.parse(url).to_s
  225. prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
  226. text = url[prefix.length, 30]
  227. suffix = url[prefix.length + 30..-1]
  228. cutoff = url[prefix.length..-1].length > 30
  229. "<span class=\"invisible\">#{encode(prefix)}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{encode(text)}</span><span class=\"invisible\">#{encode(suffix)}</span>"
  230. end
  231. def hashtag_html(tag)
  232. "<a href=\"#{encode(tag_url(tag.downcase))}\" class=\"mention hashtag\" rel=\"tag\">#<span>#{encode(tag)}</span></a>"
  233. end
  234. def mention_html(account)
  235. "<span class=\"h-card\"><a href=\"#{encode(ActivityPub::TagManager.instance.url_for(account))}\" class=\"u-url mention\">@<span>#{encode(account.username)}</span></a></span>"
  236. end
  237. end