formatting_helper.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 url_for_preview_card(preview_card)
  10. preview_card.url
  11. end
  12. def extract_status_plain_text(status)
  13. PlainTextFormatter.new(status.text, status.local?).to_s
  14. end
  15. module_function :extract_status_plain_text
  16. def status_content_format(status)
  17. html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
  18. end
  19. def rss_status_content_format(status)
  20. html = status_content_format(status)
  21. before_html = if status.spoiler_text?
  22. tag.p do
  23. tag.strong do
  24. I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale)
  25. end
  26. status.spoiler_text
  27. end + tag.hr
  28. end
  29. after_html = 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. prerender_custom_emojis(
  40. safe_join([before_html, html, after_html]),
  41. status.emojis,
  42. style: 'width: 1.1em; height: 1.1em; object-fit: contain; vertical-align: middle; margin: -.2ex .15em .2ex'
  43. ).to_str
  44. end
  45. def account_bio_format(account)
  46. html_aware_format(account.note, account.local?)
  47. end
  48. def account_field_value_format(field, with_rel_me: true)
  49. if field.verified? && !field.account.local?
  50. TextFormatter.shortened_link(field.value_for_verification)
  51. else
  52. html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false)
  53. end
  54. end
  55. end