2010 January 30th
Once I needed a very simple and fast HTML parser written in actionscript – unlike John Resig I didn’t want to pretty print or fix broken markup; all I needed to is the structure and the attributes – and since it had to be run many times in a second, I tried not to use regexp. I know, it’s silly – but it works.
Now I “ported” that to javascript and eventually to a jEdit macro – why is this good? Because unlike a full blown SAX parser, this tool will not choke on php tags, so I use it to select the innerHTML or outerHTML in a text/html/php file – or just to find a matching tag. Unfortunately right now it parses the full file, so it’s a bit slow, but I may fix that sometime in the future.
Furthmore since this is javascript, it would be very easy to port it to editors with a javascript macro engine (Notepad++ has a rough one, and we also have Aptana, but I find Aptana’s – pretty much discontinued – scriptmonkey buggy and unreliable).
Download the jEdit javascript macro from here
2010 January 14th
Sometimes I have to debug or check a json response from the server: I just paste the response to jEdit and reformat it with a macro (yes, I know about the online json beautifier and I think we did have a firebug extension for this, but still, it’s fun to see how flexible javascript macros are):
Download the json2.js from json.org, place it into your lib directory (see: startup.js), then use this ridiculously simple macro to reformat a valid javascript object (in buffer or selection):
(function(){
if (buffer.isReadOnly())
return;
var text = "",
selections = textArea.getSelection(),
bufferSelected = false,
obj;
if (selections.length == 1) {
text = textArea.getSelectedText(selections[0]);
} else {
text = textArea.getText();
bufferSelected = true;
}
obj = eval("(" + text + ")");
include(MACROLIBPATH + "json.org/json2.js");
textArea[bufferSelected ? "setText" : "setSelectedText"](JSON.stringify(obj, null, '\t'));
})();
You can download this and some of my other macros from here.
2009 December 30th
Message from the future:
there's no easy way - but hey, let's rejoice, since noone cares :D
Right now I don’t know which one sucks more: doing complex beanshell macros in a text editor or doing a proper plugin with an ant deploy script and a reloader (helper) plugin. So, here comes another crappy solution: writing jEdit macros in pure java, with real content assist and code completion.
- in the plugin manager install the JavaMacros plugin
- in Eclipse create a jEdit_Java_Macros java project
- Project Properties: Java Build Path: Libraries: Add External Jars —-» add jedit.jar and JavaMacros.jar
- create package: macros.CompiledMacros
- create a CompiledMacros subfolder in your personal macros dir
- create a class (HelloWorld for example); at the interfaces section add “MacroClass”; you can add a constructor or a main method, but it doesn’t make a difference
- now, oh my, you would think about setting the bin path to the Macros folder in the settings directory (Default output folder), but don’t do that – that will just mess up your macros folder, instead use this jEdit macro to automatically copy, reload and execute the compiled class file:
macroName = "CompiledMacros/HelloWorld";
from = "Q:/dev/workspace-java-jedit/jEdit_Java_Macros/bin/macros/";
to = "Q:/.jedit/macros/";
void reloadClassMacro(macroName, from, to) {
import java.io.*;
from += macroName + ".class";
to += macroName + ".class";
in = new FileInputStream(from);
out = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close();
out.close();
Macros.loadMacros();
macro = Macros.getMacro(macroName);
macro.invoke(view);
}
reloadClassMacro(macroName, from, to);
return;
Now assign this macro to key in jEdit and it will automatically rerun the class macro. Since class macros are tied to a given location (meaning that if you copy the class file to another directory or you rename it it will not work) this method is useful for developing macros; in the end one might want to copy the final code into a bsh file (with cleaning out the class wrapper of course).
Finally, if you happen to be on Windows you can use this autohotkey script to “call” the macro from within Eclipse (I call the AHK script with Win+F12 (#F12) and I assigned the classloader to F12 in jEdit so that’s why I forward F12 there):
#F12::
Process, Exist, eclipse.exe
If ErrorLevel
{
WinActivate, ahk_pid %ErrorLevel%
Send ^s
}
IfWinExist jEdit
{
WinActivate
Send {F12}
}
return
EDIT: no, I was wrong, this sucks too; I can’t add internal classes, like eventlisteners… thank you very much, jEdit.
2009 December 28th
Message from the future:
too bad I'm just plain tired to code at home nowdays; guess I should drink COla, Hell, RedBull and other assorted liquified shit
I have tried many and did not like either one; enough said. Finally I decided to write one for ourselves: first I wanted to do it in full java, but right now I’m hesitating -
- I need multiple users (so that we can upload from my machine or the laptop in the same time)
- proper database is a must (I’m tired of hashmap dumps, xml files and half baked lucene caches)
- simple gui (I don’t want a command line tool while swing may be too steep for me)
Either way, on the client side I must have a crawler; right now
the crawler is finished and does what I need: it reads directories and saves them as xml files. Can read mp3, zip, 7zip, rar and DOS descript.ion files (which is supported by Total Commander for example). It mainly uses
XML Directory Listing,
jID3,
J7Zip and
JUnRar (these are Apache/LGPL/free libraries) – I had been thinking about the exif info and the avi metadata, but the exif info is way too complex to be fully stored (and I just don’t need it) while the avi metadata is a total pain in the back in java (JMF player realization, broken/legacy libs etc).
I’ll upload the source to Google Code sooner or later, til then the binary is here:
http://www.rosamez.com/download/xml-dir-listing-ex.7z
(this is only the crawler; it’s a command line application! Example usage:
java -jar xml-dir-listing-ex.jar -o c:\test.xml -m -a -dion c:\temp)
2009 December 27th
Message from the future:
not amused
Zen Coding didn’t excite me too much since I have some pretty similar macros/superabbrevs myself, but its dombuilder is quite nice I must admit. I’m not really interested in writing a fully working jEdit connector for all the ZC macros, but I did convert the expander to jEdit; download here.
Macro is in javascript, requires the JavaScriptShell plugin.