This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-03-25 18:53:34 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class HtmlAwareFormatter
|
2023-01-20 04:57:32 -08:00
|
|
|
STATUS_MIME_TYPES = %w(text/plain text/markdown text/html).freeze
|
|
|
|
|
2022-03-25 18:53:34 -07: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 11:51:51 -07: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-25 18:53:34 -07:00
|
|
|
end
|
|
|
|
end
|