Escape HTML with Eclipse Monkey

Posting code on my blog became tedious.
I always had to go though and replace every < and > with > and < or some parts of the code would simply disappear! Furthermore closing square brackets seem to change normal quotes into their italic counterparts, so ] has to be replaced with &#93; too.

Eclipse Monkey to the rescue!
Since i do not want to mess up my code inside of Eclipse, i let the monkey only play with my Clipboard.

After the script was finished, it had to ‘escape’ itself to reach the public 😀

installing eclipse monkey
create a scripts folder in your project root and paste it into some escape.js, after this it should show up under ‘scripts > HTML’.

/*
 * Menu: HTML > basic html escape
 * Kudos: Michael Grosser
 * License: WTF
 */

function main() {
	systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
	contents = systemClipboard.getContents(null);
	msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
	msg = msg.replace('&','XXplaceHolDerXX');
	msg = msg.replace('XXplaceHolDerXX','&amp;');

	msg = msg.replace('<','&lt;');
	msg = msg.replace('>','&gt;');
	msg = msg.replace(']','&#93;');
	newContents  = new Packages.java.awt.datatransfer.StringSelection(msg);
	systemClipboard.setContents(newContents, newContents);
}

Stupid WordPress keeps ‘correcting’ my > to &gt; … :/

Leave a comment