General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Indicating invisible Unicode characters in XMetaL

  • tahnee_m

    Indicating invisible Unicode characters in XMetaL

    Participants 3
    Replies 4
    Last Activity 9 years, 7 months ago

    I am currently brainstorming ideas to customize XMetaL to somehow indicate the presence of an invisible Unicode character, such as the zero-width space (U+200B), the non-breaking space (U+00A0), and the word joiner (U+2060). The first two are already in our special characters palette, but 2060 would be a new addition. I'd really appreciate any guidance or ideas any of you have to offer!

    I was thinking (ideally) something in the Tags On or Normal view that would show the Unicode value in a shaded box, but I’m open to other solutions. It would be nice to indicate which invisible Unicode character we’re using.

    This is what I am hoping to achieve:
    [IMG]http://i50.tinypic.com/353ds01.jpg[/img]

    Reply

    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.
    */
    rng.SelectNodeContents(ActiveDocument.DocumentElement);
    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.
    */
    rng.MoveToDocumentStart();
    rng.Select();[/code]

    Reply

    Derek Read

    Reply to: Indicating invisible Unicode characters in XMetaL

    Here's another version of a script that ultimately does the same thing as the previous one.

    The benefit to this one is that the user's selection is not lost. The drawback with it being slow isn't as bad as I thought. You need to have an extreme case for it to be noticable and I'm assuming these chars are probably fairly rarely used even for you (?)

    Your users should be able to run this one (along with the reversing version which I have not written) any time and not lose their current selection.

    [code]//XMetaL Script Language JScript:
    //Same script but using Find.Execute instead of JScript string manipulations.

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

    //Put hex values representing the chars we want to convert to PIs into an array
    var chars   = [“2060″,”200B”,”00A0″];

    //Using rng.Delete() causes the document to be updated.
    //Turn off the rendering engine until after this loop to reduce flicker and speed things up.
    ActiveDocument.FormattingUpdating = false;

    //Loop once for each character in the array
    for(i=0;i //Move the range to the start of the doc so we will find all chars
    rng.MoveToDocumentStart();
    //convert the value from the array to a character
    var char = String.fromCharCode(parseInt(chars,16));
    //Keep going as long as we can find the char
    while(rng.Find.Execute(char)){
    //Next line is needed because inserting a PI doesn't replace the current selection (like other insertion actions)
    //Delete the char
    rng.Delete();
    //Insert a PI
    rng.InsertProcessingInstruction();
    //Give the PI a target that matches the character's Unicode code point value for easy identification
    rng.TypeText(“U”+chars
    );
    }
    }
    //Turn the rendering engine back on.
    ActiveDocument.FormattingUpdating = true;[/code]

    Reply

    tahnee_m

    Reply to: Indicating invisible Unicode characters in XMetaL

    This was extremely helpful, thank you! If I were giving my users the option to switch between viewing and hiding this new feature using two different macros, would it be easier to hide the PIs instead of reversing the process each time? Reversing would then only take place during check in.

    Reply

    Derek Read

    Reply to: Indicating invisible Unicode characters in XMetaL

    In both cases the whole document would need to be completely re-rendered so I'm not sure there is much speed savings there. You might find that changing the CSS to hide PIs might be slightly faster because you aren't changing the markup in that case but I think the majority of any time in a script would be due to the re-rendering of the document.

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support