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.

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