1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
# frozen_string_literal: true
module Temple
module Filters
# Try to encode input string
#
# @api public
class Encoding < Parser
define_options encoding: 'utf-8'
def call(s)
if options[:encoding] && s.respond_to?(:encoding)
old_enc = s.encoding
s = +s
s.force_encoding(options[:encoding])
# Fall back to old encoding if new encoding is invalid
unless s.valid_encoding?
s.force_encoding(old_enc)
s.force_encoding(::Encoding::BINARY) unless s.valid_encoding?
end
end
s
end
end
end
end
|