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.

100 lines
2.8 KiB

7 years ago
7 years ago
7 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 = link_urls(html)
  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, tags: %w(a br p span), attributes: %w(href rel class))
  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(account.note)
  28. html = link_urls(html)
  29. html = link_accounts(html)
  30. html = link_hashtags(html)
  31. html.html_safe # rubocop:disable Rails/OutputSafety
  32. end
  33. private
  34. def encode(html)
  35. HTMLEntities.new.encode(html)
  36. end
  37. def link_urls(html)
  38. Twitter::Autolink.auto_link_urls(html, url_target: '_blank',
  39. link_attribute_block: lambda { |_, a| a[:rel] << ' noopener' },
  40. link_text_block: lambda { |_, text| link_html(text) })
  41. end
  42. def link_mentions(html, mentions)
  43. html.gsub(Account::MENTION_RE) do |match|
  44. acct = Account::MENTION_RE.match(match)[1]
  45. mention = mentions.find { |item| TagManager.instance.same_acct?(item.account.acct, acct) }
  46. mention.nil? ? match : mention_html(match, mention.account)
  47. end
  48. end
  49. def link_accounts(html)
  50. html.gsub(Account::MENTION_RE) do |match|
  51. acct = Account::MENTION_RE.match(match)[1]
  52. username, domain = acct.split('@')
  53. domain = nil if TagManager.instance.local_domain?(domain)
  54. account = Account.find_remote(username, domain)
  55. account.nil? ? match : mention_html(match, account)
  56. end
  57. end
  58. def link_hashtags(html)
  59. html.gsub(Tag::HASHTAG_RE) do |match|
  60. hashtag_html(match)
  61. end
  62. end
  63. def link_html(url)
  64. prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
  65. text = url[prefix.length, 30]
  66. suffix = url[prefix.length + 30..-1]
  67. cutoff = url[prefix.length..-1].length > 30
  68. "<span class=\"invisible\">#{prefix}</span><span class=\"#{cutoff ? 'ellipsis' : ''}\">#{text}</span><span class=\"invisible\">#{suffix}</span>"
  69. end
  70. def hashtag_html(match)
  71. prefix, affix = match.split('#')
  72. "#{prefix}<a href=\"#{tag_url(affix.downcase)}\" class=\"mention hashtag\">#<span>#{affix}</span></a>"
  73. end
  74. def mention_html(match, account)
  75. "#{match.split('@').first}<span class=\"h-card\"><a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@#{account.username}</a></span>"
  76. end
  77. end