12.12.06

Rapid Extension Prototyping with Plain Old Webserver

Posted in POW at 2:09 pm by David Kellogg

Recently I used SJS to prototype internationalization code. Here’s how it works.

I found what appeared to be a word separator at XUL Planet http://www.xulplanet.com/references/xpcomref/ifaces/nsISemanticUnitScanner.html

The real problem I have is that I need to separate English or German words. That’s easy enough, but what about Chinese? I need a general-purpose separator that can tokenize (in URL-encoded speak) ‘%E6%9C%88′ or ‘月’. That’s the Chinese word for month. Imagine three of these characters in a row. Where does one character end and the other begin? The difficulties mount. So it’s time to try some XPCOM in Firefox using the Plain Old Webserver.

Here is i18n.sjs:


try {
var scanner = Components.classes["@mozilla.org/intl/semanticunitscanner;1"]
.createInstance(Components.interfaces.nsISemanticUnitScanner);
var text = decodeURI("%E6%9C%88%E6%9C%88");
//var text = "An apple a day";
scanner.start(null);
var begin = new Object();
var end = new Object();
var next_available = scanner.next(text, text.length, 0, false, begin, end);
var sub = text.substring(0,end.value);
alert("first word is "+sub);
} catch(e) {
alert(e);
}
?>

I just drop this code into the ‘htdocs’ directory, and wammo, I have my result. The first word break is at position 1 for Chinese. The English word break is at 2 if you uncomment it.

4 Comments »

  1. augustusgloo said,

    January 21, 2007 at 9:17 am

    Hi,
    I love the webserver, but is it possible to use normal webfiles of get a SJS WYSYWYG editor? (I’m great at html but not so good at sjs)
    Thanks

  2. David Kellogg said,

    January 23, 2007 at 7:23 am

    Probably not, since SJS is a programming language, not markup. It seems you are looking for something else, like HTML.

    You are welcome to use any .html file you want with POW. You are not limited the the .sjs extension.

    Dave

  3. John Jorgensen said,

    January 24, 2007 at 8:49 am

    This is a great web server. Thanks for putting effort into it.

    I’d like to write a wiki on top of POW. It looks like I’ll need to add handlers to make and delete directories if I want to use the filesystem as my back store. How do I submit patches if I manage to make something sane?

    J*

  4. David Kellogg said,

    January 24, 2007 at 10:25 am

    I’d like to write a wiki on top of POW.

    Great! Sounds even more ambitious than writing a webserver in JS.

    Mail them to me for now dakellog_public at yahoo.com. I’ll set up somthing better in the future.

    For directories, this seemed to work.

    <?sjs
    pow_mkdir(“testing”);
    pow_delete_file(“testing”);
    ?>

    There really should be a complementary pow_rmdir with a recursive option. There’s already an undocumented, unfrozen, but working deletion command,

    function pow_delete_file (String filename, bool recursive);

    filename: a file or directory
    recursive: true for recursive, false for non-recursive

    This recursively deletes everything in a directory (or file) and below it. Again, I will replace this by pow_rmdir or pow_rm in the future.

Leave a Comment