My code is simple:
"<3>".encode:)xml => :text)
I am completely new to RGSS and ruby, so I may simply be misusing the #encode method of String. However, whenever I try to invoke #encode with the hash option ":xml => :text", it throws a "Encoding::ConverterNotFoundError" with the text "code converter not found (xml_text)".
This leads me to believe this converter option is invalid in RGSS, and that the converter has not been implemented; So, I would like to do this myself.
I figured simply inheriting from Encoding::Converter would allow me to manage this option, but I just can't seem to get a working example - because the initialize parameters require two different encodings (source and destination); which also causes the ConverterNotFoundError to be thrown if you pass the options. This time, with "UTF-8" to "UTF-8" not found.
class XmlTextEncodingConverter < Encoding::Converter XmlCharRegexp = /[<|>|\&|\"]/ XmlCodes = { '<' => "lt;", '>' => "gt;", '&' => "&", '"' => """ } alias :xml_text_initialize :initialize def initialize(options) xml_text_initialize("UTF-8", "UTF-8") @options = options end alias :xml_text_convert :convert def convert(text) result = xml_text_convert(text) case @options[:xml] when :text result = result.gsub(XmlCharRegexp) { |m| XmlCodes["#{x}"] } end end endAll-in-all, I can't seem to find ANY documentation in RUBY about extending Encoding::Converter to create custom encodings, or find any core "require" commands to allow "xml_text" or "xml_attr" to become valid options.
Any advice on how to resolve the issue of encoding/decoding xml special characters in strings would be tremendously appreciated.
[Ace] RGSS3 String.encode
● ARCHIVED · READ-ONLY
-
-
It most likely isn't (legally) possible to make your first code example work. Why not simply write a new method that escapes those characters instead? Either as an instance method of String, or as a singleton method of a new or existing module (called XML, for example).
You're also using alias wrong. Look up the super keyword. -
Thanks for the alias tip - I completely overlooked that when copy-pasting the class together.
I pieced the class attempt together from methods that I did develop to do the character replace; I was just hoping to find a way to extend String#encode to work with the arguments, without adding anymore complexity.
Do you happen to know of any way to extend an Encoding::Converter to do this? I can't even get a basic one to work properly. -
How is encode defined?
-
I'm not quite sure how it is defined, other than the limited definition available in RUBY 1.9.3 doc:
static VALUEstr_encode(int argc, VALUE *argv, VALUE str){ VALUE newstr = str; int encidx = str_transcode(argc, argv, &newstr); return encoded_dup(newstr, str, encidx);} I am having trouble reconstructing the c code for encoded_dup in the source. The rabbit hole goes deep. -
As I've said, I doubt it's (legally) possible for you to make your first code example work.
Of course, you can still use Encoding::Converter for the following part (that your code above didn't do):
But in that case, you should use it in an entire different way. You can find a good description here.If the value is :text #encode replaces undefined characters with their (upper-case hexadecimal) numeric character references.
By the way, if you want to read the Ruby source code (on the web), this site is great for that, even though the version used by RGSS3 (1.9.2-p0) isn't available on it. You can also just download it. -
Also, take note that AFAIK, not all methods of Ruby is available on RGSS.