闭社主体 forked from https://github.com/tootsuite/mastodon
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.

121 lines
3.5 KiB

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