Home Forums General XMetaL Discussion Indicating invisible Unicode characters in XMetaL Reply To: Indicating invisible Unicode characters in XMetaL

Derek Read

Reply to: Indicating invisible Unicode characters in XMetaL

With the current features available you can come close I think, but only if we change the markup slightly (and then reverse those changes before saving).

I've been experimenting with some script that would convert your characters to PIs. I would expect this to run just after a document is opened. A similar script would need to be created that would reverse that process during save.

Here's what I have so far. I've chosen to insert the PI with just a target (no data) in order to keep them short. They also happen to look sort of like what you proposed.

I see a few benefits to using PIs:
1) This should work for any DTD. No need to change the DTD to keep the document valid.
2) Users can easily insert a PI using standard XMetaL features and type in the hex number (in the right format). Then, if it is in your list of “recognized chars” (the array) it could be handled automatically.
3) PIs can be styled using CSS to make them stand out from the rest of your content if necessary. So you could make them white on blue if you want.

[code]//XMetaL Script Language JScript:
//VERY experimental: use this as a *demo* to build into your own customizations

//Declare a range we can use to manipulate the doc
var rng     = ActiveDocument.Range;

Put hex values for the characters we want to convert into PIs into an array
Don't put any markup characters or the standard whitespace characters.
This script is not designed to handle those and it will fail (and there is no need that I can see).
var chars   = [“00A0″,”200B”,”2060″];

Put the content of the entire document into a string (starting at the root
element because that is the only thing we are interested in).
JScript string manip is very fast so I chose to use it rather than the Find.Execute XMetaL
API or similar which would be a lot slower using an API would probably allow us to keep
the user's selection but I think this script would probably run at document open anyway.
var docXML = rng.TextWithRM;

//replace the characters in the string with PIs using the values declared in the array
for(i=0;i //Create regex
var uniChar = “\u” + chars;
var re_char = new RegExp(uniChar, “g”);

/*Create the text for the PI
Note: the plus symbol is illegal in a PI target. You might use something else here
instead of U+ or what I have (nothing) or leave it as is
var piTxt = “ + “?>”;

//Do the replacement for this particular character.
//Note that “g” in the regex means “global” so all will be replaced.
docXML = docXML.replace(re_char,piTxt);
//Replace the document with our altered version
rng.Text = docXML;

We've replaced the whole document so the user's original selection
doesn't exist anymore and can't be restored so put it at the start of the document.
That won't be an issue if this runs at document open.