2022-03-26 01:53:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class HtmlAwareFormatter
|
2023-01-20 12:57:32 +00:00
|
|
|
STATUS_MIME_TYPES = %w(text/plain text/markdown text/html).freeze
|
|
|
|
|
2022-03-26 01:53:34 +00:00
|
|
|
attr_reader :text, :local, :options
|
|
|
|
|
|
|
|
alias local? local
|
|
|
|
|
|
|
|
# @param [String] text
|
|
|
|
# @param [Boolean] local
|
|
|
|
# @param [Hash] options
|
|
|
|
def initialize(text, local, options = {})
|
|
|
|
@text = text
|
|
|
|
@local = local
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return ''.html_safe if text.blank?
|
|
|
|
|
|
|
|
if local?
|
|
|
|
linkify
|
|
|
|
else
|
|
|
|
reformat.html_safe # rubocop:disable Rails/OutputSafety
|
|
|
|
end
|
|
|
|
rescue ArgumentError
|
|
|
|
''.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reformat
|
|
|
|
Sanitize.fragment(text, Sanitize::Config::MASTODON_STRICT)
|
|
|
|
end
|
|
|
|
|
|
|
|
def linkify
|
2022-03-28 19:51:51 +01:00
|
|
|
if %w(text/markdown text/html).include?(@options[:content_type])
|
|
|
|
AdvancedTextFormatter.new(text, options).to_s
|
|
|
|
else
|
|
|
|
TextFormatter.new(text, options).to_s
|
|
|
|
end
|
2022-03-26 01:53:34 +00:00
|
|
|
end
|
|
|
|
end
|