def encode(source, *instructions)
string = source.to_s.dup
if (instructions.empty?)
instructions = [:basic]
elsif (unknown_instructions = instructions - INSTRUCTIONS) != []
raise InstructionError,
"unknown encode_entities command(s): #{unknown_instructions.inspect}"
end
basic_entity_encoder =
if instructions.include?(:basic) || instructions.include?(:named)
:encode_named
elsif instructions.include?(:decimal)
:encode_decimal
else instructions.include?(:hexadecimal)
:encode_hexadecimal
end
string.gsub!(basic_entity_regexp){ __send__(basic_entity_encoder, $&) }
extended_entity_encoders = []
if instructions.include?(:named)
extended_entity_encoders << :encode_named
end
if instructions.include?(:decimal)
extended_entity_encoders << :encode_decimal
elsif instructions.include?(:hexadecimal)
extended_entity_encoders << :encode_hexadecimal
end
unless extended_entity_encoders.empty?
string.gsub!(extended_entity_regexp){
encode_extended(extended_entity_encoders, $&)
}
end
return string
end