formatting_helper.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. module FormattingHelper
  3. def html_aware_format(text, local, options = {})
  4. HtmlAwareFormatter.new(text, local, options).to_s
  5. end
  6. def linkify(text, options = {})
  7. TextFormatter.new(text, options).to_s
  8. end
  9. def extract_status_plain_text(status)
  10. PlainTextFormatter.new(status.text, status.local?).to_s
  11. end
  12. module_function :extract_status_plain_text
  13. def status_content_format(status)
  14. html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
  15. end
  16. def rss_status_content_format(status)
  17. html = status_content_format(status)
  18. before_html = begin
  19. if status.spoiler_text?
  20. tag.p do
  21. tag.strong do
  22. I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale)
  23. end
  24. status.spoiler_text
  25. end + tag.hr
  26. end
  27. end
  28. after_html = begin
  29. if status.preloadable_poll
  30. tag.p do
  31. safe_join(
  32. status.preloadable_poll.options.map do |o|
  33. tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', o, disabled: true)
  34. end,
  35. tag.br
  36. )
  37. end
  38. end
  39. end
  40. prerender_custom_emojis(
  41. safe_join([before_html, html, after_html]),
  42. status.emojis,
  43. style: 'width: 1.1em; height: 1.1em; object-fit: contain; vertical-align: middle; margin: -.2ex .15em .2ex'
  44. ).to_str
  45. end
  46. def account_bio_format(account)
  47. html_aware_format(account.note, account.local?)
  48. end
  49. def account_field_value_format(field, with_rel_me: true)
  50. html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false)
  51. end
  52. end