logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

formatter.rb (7014B)


  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. return '' if raw_content.blank?
  17. unless status.local?
  18. html = reformat(raw_content)
  19. html = encode_custom_emojis(html, status.emojis) if options[:custom_emojify]
  20. return html.html_safe # rubocop:disable Rails/OutputSafety
  21. end
  22. linkable_accounts = status.mentions.map(&:account)
  23. linkable_accounts << status.account
  24. html = raw_content
  25. html = "RT @#{prepend_reblog} #{html}" if prepend_reblog
  26. html = encode_and_link_urls(html, linkable_accounts)
  27. html = encode_custom_emojis(html, status.emojis) if options[:custom_emojify]
  28. html = simple_format(html, {}, sanitize: false)
  29. html = html.delete("\n")
  30. html.html_safe # rubocop:disable Rails/OutputSafety
  31. end
  32. def reformat(html)
  33. sanitize(html, Sanitize::Config::MASTODON_STRICT)
  34. end
  35. def plaintext(status)
  36. return status.text if status.local?
  37. text = status.text.gsub(/(<br \/>|<br>|<\/p>)+/) { |match| "#{match}\n" }
  38. strip_tags(text)
  39. end
  40. def simplified_format(account, **options)
  41. html = if account.local?
  42. linkify(account.note)
  43. else
  44. reformat(account.note)
  45. end
  46. html = encode_custom_emojis(html, CustomEmoji.from_text(account.note, account.domain)) if options[:custom_emojify]
  47. html.html_safe # rubocop:disable Rails/OutputSafety
  48. end
  49. def sanitize(html, config)
  50. Sanitize.fragment(html, config)
  51. end
  52. def format_spoiler(status)
  53. html = encode(status.spoiler_text)
  54. html = encode_custom_emojis(html, status.emojis)
  55. html.html_safe # rubocop:disable Rails/OutputSafety
  56. end
  57. def format_field(account, str)
  58. return reformat(str).html_safe unless account.local? # rubocop:disable Rails/OutputSafety
  59. encode_and_link_urls(str, me: true).html_safe # rubocop:disable Rails/OutputSafety
  60. end
  61. def linkify(text)
  62. html = encode_and_link_urls(text)
  63. html = simple_format(html, {}, sanitize: false)
  64. html = html.delete("\n")
  65. html.html_safe # rubocop:disable Rails/OutputSafety
  66. end
  67. private
  68. def encode(html)
  69. HTMLEntities.new.encode(html)
  70. end
  71. def encode_and_link_urls(html, accounts = nil, options = {})
  72. entities = Extractor.extract_entities_with_indices(html, extract_url_without_protocol: false)
  73. if accounts.is_a?(Hash)
  74. options = accounts
  75. accounts = nil
  76. end
  77. rewrite(html.dup, entities) do |entity|
  78. if entity[:url]
  79. link_to_url(entity, options)
  80. elsif entity[:hashtag]
  81. link_to_hashtag(entity)
  82. elsif entity[:screen_name]
  83. link_to_mention(entity, accounts)
  84. end
  85. end
  86. end
  87. def count_tag_nesting(tag)
  88. if tag[1] == '/' then -1
  89. elsif tag[-2] == '/' then 0
  90. else 1
  91. end
  92. end
  93. def encode_custom_emojis(html, emojis)
  94. return html if emojis.empty?
  95. emoji_map = emojis.map { |e| [e.shortcode, full_asset_url(e.image.url(:static))] }.to_h
  96. i = -1
  97. tag_open_index = nil
  98. inside_shortname = false
  99. shortname_start_index = -1
  100. invisible_depth = 0
  101. while i + 1 < html.size
  102. i += 1
  103. if invisible_depth.zero? && inside_shortname && html[i] == ':'
  104. shortcode = html[shortname_start_index + 1..i - 1]
  105. emoji = emoji_map[shortcode]
  106. if emoji
  107. replacement = "<img draggable=\"false\" class=\"emojione\" alt=\":#{shortcode}:\" title=\":#{shortcode}:\" src=\"#{emoji}\" />"
  108. before_html = shortname_start_index.positive? ? html[0..shortname_start_index - 1] : ''
  109. html = before_html + replacement + html[i + 1..-1]
  110. i += replacement.size - (shortcode.size + 2) - 1
  111. else
  112. i -= 1
  113. end
  114. inside_shortname = false
  115. elsif tag_open_index && html[i] == '>'
  116. tag = html[tag_open_index..i]
  117. tag_open_index = nil
  118. if invisible_depth.positive?
  119. invisible_depth += count_tag_nesting(tag)
  120. elsif tag == '<span class="invisible">'
  121. invisible_depth = 1
  122. end
  123. elsif html[i] == '<'
  124. tag_open_index = i
  125. inside_shortname = false
  126. elsif !tag_open_index && html[i] == ':'
  127. inside_shortname = true
  128. shortname_start_index = i
  129. end
  130. end
  131. html
  132. end
  133. def rewrite(text, entities)
  134. chars = text.to_s.to_char_a
  135. # Sort by start index
  136. entities = entities.sort_by do |entity|
  137. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  138. indices.first
  139. end
  140. result = []
  141. last_index = entities.reduce(0) do |index, entity|
  142. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  143. result << encode(chars[index...indices.first].join)
  144. result << yield(entity)
  145. indices.last
  146. end
  147. result << encode(chars[last_index..-1].join)
  148. result.flatten.join
  149. end
  150. def link_to_url(entity, options = {})
  151. url = Addressable::URI.parse(entity[:url])
  152. html_attrs = { target: '_blank', rel: 'nofollow noopener' }
  153. html_attrs[:rel] = "me #{html_attrs[:rel]}" if options[:me]
  154. Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), url, html_attrs)
  155. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  156. encode(entity[:url])
  157. end
  158. def link_to_mention(entity, linkable_accounts)
  159. acct = entity[:screen_name]
  160. return link_to_account(acct) unless linkable_accounts
  161. account = linkable_accounts.find { |item| TagManager.instance.same_acct?(item.acct, acct) }
  162. account ? mention_html(account) : "@#{acct}"
  163. end
  164. def link_to_account(acct)
  165. username, domain = acct.split('@')
  166. domain = nil if TagManager.instance.local_domain?(domain)
  167. account = Account.find_remote(username, domain)
  168. account ? mention_html(account) : "@#{acct}"
  169. end
  170. def link_to_hashtag(entity)
  171. hashtag_html(entity[:hashtag])
  172. end
  173. def link_html(url)
  174. url = Addressable::URI.parse(url).to_s
  175. prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
  176. text = url[prefix.length, 30]
  177. suffix = url[prefix.length + 30..-1]
  178. cutoff = url[prefix.length..-1].length > 30
  179. "<span class=\"invisible\">#{encode(prefix)}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{encode(text)}</span><span class=\"invisible\">#{encode(suffix)}</span>"
  180. end
  181. def hashtag_html(tag)
  182. "<a href=\"#{tag_url(tag.downcase)}\" class=\"mention hashtag\" rel=\"tag\">#<span>#{tag}</span></a>"
  183. end
  184. def mention_html(account)
  185. "<span class=\"h-card\"><a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@<span>#{account.username}</span></a></span>"
  186. end
  187. end