闭社主体 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.

101 lines
2.9 KiB

8 years ago
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class Formatter
  4. include Singleton
  5. include RoutingHelper
  6. include ActionView::Helpers::TextHelper
  7. include ActionView::Helpers::SanitizeHelper
  8. def format(status)
  9. return reformat(status.content) unless status.local?
  10. html = status.text
  11. html = encode(html)
  12. html = simple_format(html, {}, sanitize: false)
  13. html = html.delete("\n")
  14. html = link_urls(html)
  15. html = link_mentions(html, status.mentions)
  16. html = link_hashtags(html)
  17. html.html_safe # rubocop:disable Rails/OutputSafety
  18. end
  19. def reformat(html)
  20. sanitize(html, tags: %w(a br p span), attributes: %w(href rel class))
  21. end
  22. def plaintext(status)
  23. return status.text if status.local?
  24. strip_tags(status.text)
  25. end
  26. def simplified_format(account)
  27. return reformat(account.note) unless account.local?
  28. html = encode(account.note)
  29. html = link_urls(html)
  30. html = link_accounts(html)
  31. html = link_hashtags(html)
  32. html.html_safe # rubocop:disable Rails/OutputSafety
  33. end
  34. private
  35. def encode(html)
  36. HTMLEntities.new.encode(html)
  37. end
  38. def link_urls(html)
  39. Twitter::Autolink.auto_link_urls(html, url_target: '_blank',
  40. link_attribute_block: lambda { |_, a| a[:rel] << ' noopener' },
  41. link_text_block: lambda { |_, text| link_html(text) })
  42. end
  43. def link_mentions(html, mentions)
  44. html.gsub(Account::MENTION_RE) do |match|
  45. acct = Account::MENTION_RE.match(match)[1]
  46. mention = mentions.find { |item| TagManager.instance.same_acct?(item.account.acct, acct) }
  47. mention.nil? ? match : mention_html(match, mention.account)
  48. end
  49. end
  50. def link_accounts(html)
  51. html.gsub(Account::MENTION_RE) do |match|
  52. acct = Account::MENTION_RE.match(match)[1]
  53. username, domain = acct.split('@')
  54. domain = nil if TagManager.instance.local_domain?(domain)
  55. account = Account.find_remote(username, domain)
  56. account.nil? ? match : mention_html(match, account)
  57. end
  58. end
  59. def link_hashtags(html)
  60. html.gsub(Tag::HASHTAG_RE) do |match|
  61. hashtag_html(match)
  62. end
  63. end
  64. def link_html(url)
  65. prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
  66. text = url[prefix.length, 30]
  67. suffix = url[prefix.length + 30..-1]
  68. cutoff = url[prefix.length..-1].length > 30
  69. "<span class=\"invisible\">#{prefix}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{text}</span><span class=\"invisible\">#{suffix}</span>"
  70. end
  71. def hashtag_html(match)
  72. prefix, affix = match.split('#')
  73. "#{prefix}<a href=\"#{tag_url(affix.downcase)}\" class=\"mention hashtag\">#<span>#{affix}</span></a>"
  74. end
  75. def mention_html(match, account)
  76. "#{match.split('@').first}<span class=\"h-card\"><a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@<span>#{account.username}</span></a></span>"
  77. end
  78. end