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.

151 lines
4.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, attribute = :text, paragraphize = true)
  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.public_send(attribute)
  16. return '' if raw_content.blank?
  17. return reformat(raw_content) unless status.local?
  18. linkable_accounts = status.mentions.map(&:account)
  19. linkable_accounts << status.account
  20. html = raw_content
  21. html = "RT @#{prepend_reblog} #{html}" if prepend_reblog
  22. html = encode_and_link_urls(html, linkable_accounts)
  23. html = simple_format(html, {}, sanitize: false) if paragraphize
  24. html = html.delete("\n")
  25. html.html_safe # rubocop:disable Rails/OutputSafety
  26. end
  27. def reformat(html)
  28. sanitize(html, Sanitize::Config::MASTODON_STRICT).html_safe # rubocop:disable Rails/OutputSafety
  29. end
  30. def plaintext(status)
  31. return status.text if status.local?
  32. strip_tags(status.text)
  33. end
  34. def simplified_format(account)
  35. return reformat(account.note) unless account.local?
  36. html = encode_and_link_urls(account.note)
  37. html = simple_format(html, {}, sanitize: false)
  38. html = html.delete("\n")
  39. html.html_safe # rubocop:disable Rails/OutputSafety
  40. end
  41. def sanitize(html, config)
  42. Sanitize.fragment(html, config)
  43. end
  44. private
  45. def encode(html)
  46. HTMLEntities.new.encode(html)
  47. end
  48. def encode_and_link_urls(html, accounts = nil)
  49. entities = Extractor.extract_entities_with_indices(html, extract_url_without_protocol: false)
  50. rewrite(html.dup, entities) do |entity|
  51. if entity[:url]
  52. link_to_url(entity)
  53. elsif entity[:hashtag]
  54. link_to_hashtag(entity)
  55. elsif entity[:screen_name]
  56. link_to_mention(entity, accounts)
  57. end
  58. end
  59. end
  60. def rewrite(text, entities)
  61. chars = text.to_s.to_char_a
  62. # Sort by start index
  63. entities = entities.sort_by do |entity|
  64. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  65. indices.first
  66. end
  67. result = []
  68. last_index = entities.reduce(0) do |index, entity|
  69. indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
  70. result << encode(chars[index...indices.first].join)
  71. result << yield(entity)
  72. indices.last
  73. end
  74. result << encode(chars[last_index..-1].join)
  75. result.flatten.join
  76. end
  77. def link_to_url(entity)
  78. normalized_url = Addressable::URI.parse(entity[:url]).normalize
  79. html_attrs = { target: '_blank', rel: 'nofollow noopener' }
  80. Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), normalized_url, html_attrs)
  81. rescue Addressable::URI::InvalidURIError
  82. encode(entity[:url])
  83. end
  84. def link_to_mention(entity, linkable_accounts)
  85. acct = entity[:screen_name]
  86. return link_to_account(acct) unless linkable_accounts
  87. account = linkable_accounts.find { |item| TagManager.instance.same_acct?(item.acct, acct) }
  88. account ? mention_html(account) : "@#{acct}"
  89. end
  90. def link_to_account(acct)
  91. username, domain = acct.split('@')
  92. domain = nil if TagManager.instance.local_domain?(domain)
  93. account = Account.find_remote(username, domain)
  94. account ? mention_html(account) : "@#{acct}"
  95. end
  96. def link_to_hashtag(entity)
  97. hashtag_html(entity[:hashtag])
  98. end
  99. def link_html(url)
  100. url = Addressable::URI.parse(url).display_uri.to_s
  101. prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
  102. text = url[prefix.length, 30]
  103. suffix = url[prefix.length + 30..-1]
  104. cutoff = url[prefix.length..-1].length > 30
  105. "<span class=\"invisible\">#{prefix}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{text}</span><span class=\"invisible\">#{suffix}</span>"
  106. end
  107. def hashtag_html(tag)
  108. "<a href=\"#{tag_url(tag.downcase)}\" class=\"mention hashtag\" rel=\"tag\">#<span>#{tag}</span></a>"
  109. end
  110. def mention_html(account)
  111. "<span class=\"h-card\"><a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@<span>#{account.username}</span></a></span>"
  112. end
  113. end