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.

150 lines
4.1 KiB

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