Autoconvert text to element

Oxygen general issues.
xsaero00
Posts: 58
Joined: Sat Aug 01, 2009 12:57 am

Autoconvert text to element

Post by xsaero00 »

I have this idea that I would like to implement using oXygen customizations. It is for a custom DocBook framework.

The user basically types something like "[1]" or "[1,3]", which represents links to reference section, and the editor will automatically detect such text and turn it into <xref linkend="bib.workscited.1" xrefstyle="bib"/> or <xref linkend="bib.workscited.1" xrefstyle="bib"/><xref linkend="bib.workscited.3" xrefstyle="bib"/>

I already figured out how to display <xref linkend="bib.workscited.1" xrefstyle="bib"/> as [1]. Now I need a simple way to add these elements. Currently I have it implemented as a dialog where user types in numbers separated by commas and elements are inserted automatically. I want to make it even easier.

If you think that making editor look at each character is too much processing power, then I can limit the code execution to only work after '[' key press. Another alternative is to make it non real time at all, but to invoke it on a user's command: A user would, optionally, highlight some text and press a button and the highlighted chunk of text or whole document will get processed.
Radu
Posts: 9446
Joined: Fri Jul 09, 2004 5:18 pm

Re: Autoconvert text to element

Post by Radu »

Hi,
If you think that making editor look at each character is too much processing power, then I can limit the code execution to only work after '[' key press.
You can set a filter on the AuthorDocumentController with an implementation like:

Code: Select all

this.authorAccess.getDocumentController().setDocumentFilter(new AuthorDocumentFilter() {
/**
* @see ro.sync.ecss.extensions.api.AuthorDocumentFilter#insertText(ro.sync.ecss.extensions.api.AuthorDocumentFilterBypass, int, java.lang.String)
*/
@Override
public void insertText(AuthorDocumentFilterBypass filterBypass, int offset, String toInsert) {
boolean defaultBehavior = true;
if(toInsert != null && "]".equals(toInsert)) {
//The user might have typed the end of the construct.
//Get some text before the caret position to see if we have a start "["
Segment seg = new Segment();
try {

//Take some text from the left of the caret, let's say 10 characters.
authorAccess.getDocumentController().getChars(offset - 10, 10, seg);
String text = seg.toString();
int indexOfBracket = text.indexOf("[");
if(indexOfBracket != -1) {
//TODO check that the construct inside the brackers is valid.
defaultBehavior = false;
//If it is valid
String insideBrackets = text.substring(indexOfBracket + 1);

//Remove what's inside
int removeOffset = offset - 10 + indexOfBracket;
authorAccess.getDocumentController().delete(removeOffset, offset - 1);
//And insert the xref
try {
authorAccess.getDocumentController().insertXMLFragment("<xref>" + insideBrackets + "</xref>", removeOffset);
} catch (AuthorOperationException e) {
e.printStackTrace();
}
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
if(defaultBehavior) {
//Delegate for default behavior.
super.insertText(filterBypass, offset, toInsert);
}
}
});
In the Docbook5ExtensionsBundle there is a method:

Code: Select all

  /**
* @see ro.sync.ecss.extensions.api.ExtensionsBundle#createAuthorExtensionStateListener()
*/
@Override
public AuthorExtensionStateListener createAuthorExtensionStateListener()
which uses the extension state listener to generate unique attributes. You can get access to the AuthorExtensionStateListener (maybe wrap it to keep the original behavior) in order to obtain the AuthorAccess when the editor page is activated.
Another alternative is to make it non real time at all, but to invoke it on a user's command: A user would, optionally, highlight some text and press a button and the highlighted chunk of text or whole document will get processed.
Yes, you can also do that, we already have some AuthorOperation implementations like :

ro.sync.ecss.extensions.commons.operations.SurroundWithFragmentOperation

You can also implement your own AuthorOperation which removes the selection and inserts your own XML fragment, like in the code sample above.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
xsaero00
Posts: 58
Joined: Sat Aug 01, 2009 12:57 am

Re: Autoconvert text to element

Post by xsaero00 »

Thank you. This seems very promising. Will try to implement it and let you know how it went.
Post Reply