html_aware_formatter.rb 677 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # frozen_string_literal: true
  2. class HtmlAwareFormatter
  3. attr_reader :text, :local, :options
  4. alias local? local
  5. # @param [String] text
  6. # @param [Boolean] local
  7. # @param [Hash] options
  8. def initialize(text, local, options = {})
  9. @text = text
  10. @local = local
  11. @options = options
  12. end
  13. def to_s
  14. return ''.html_safe if text.blank?
  15. if local?
  16. linkify
  17. else
  18. reformat.html_safe # rubocop:disable Rails/OutputSafety
  19. end
  20. rescue ArgumentError
  21. ''.html_safe
  22. end
  23. private
  24. def reformat
  25. Sanitize.fragment(text, Sanitize::Config::MASTODON_STRICT)
  26. end
  27. def linkify
  28. TextFormatter.new(text, options).to_s
  29. end
  30. end