1
0

formatting_helper.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. module FormattingHelper
  3. SYNDICATED_EMOJI_STYLES = <<~CSS.squish
  4. height: 1.1em;
  5. margin: -.2ex .15em .2ex;
  6. object-fit: contain;
  7. vertical-align: middle;
  8. width: 1.1em;
  9. CSS
  10. def html_aware_format(text, local, options = {})
  11. HtmlAwareFormatter.new(text, local, options).to_s
  12. end
  13. def linkify(text, options = {})
  14. TextFormatter.new(text, options).to_s
  15. end
  16. def url_for_preview_card(preview_card)
  17. preview_card.url
  18. end
  19. def extract_status_plain_text(status)
  20. PlainTextFormatter.new(status.text, status.local?).to_s
  21. end
  22. module_function :extract_status_plain_text
  23. def status_content_format(status)
  24. MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
  25. span.add_attributes(
  26. 'app.formatter.content.type' => 'status',
  27. 'app.formatter.content.origin' => status.local? ? 'local' : 'remote'
  28. )
  29. html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
  30. end
  31. end
  32. def rss_status_content_format(status)
  33. prerender_custom_emojis(
  34. wrapped_status_content_format(status),
  35. status.emojis,
  36. style: SYNDICATED_EMOJI_STYLES
  37. ).to_str
  38. end
  39. def account_bio_format(account)
  40. MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
  41. span.add_attributes(
  42. 'app.formatter.content.type' => 'account_bio',
  43. 'app.formatter.content.origin' => account.local? ? 'local' : 'remote'
  44. )
  45. html_aware_format(account.note, account.local?)
  46. end
  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. private
  56. def wrapped_status_content_format(status)
  57. safe_join [
  58. rss_content_preroll(status),
  59. status_content_format(status),
  60. rss_content_postroll(status),
  61. ]
  62. end
  63. def rss_content_preroll(status)
  64. if status.spoiler_text?
  65. safe_join [
  66. tag.p { spoiler_with_warning(status) },
  67. tag.hr,
  68. ]
  69. end
  70. end
  71. def spoiler_with_warning(status)
  72. safe_join [
  73. tag.strong { I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) },
  74. status.spoiler_text,
  75. ]
  76. end
  77. def rss_content_postroll(status)
  78. if status.preloadable_poll
  79. tag.p do
  80. poll_option_tags(status)
  81. end
  82. end
  83. end
  84. def poll_option_tags(status)
  85. safe_join(
  86. status.preloadable_poll.options.map do |option|
  87. tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', option, disabled: true)
  88. end,
  89. tag.br
  90. )
  91. end
  92. end