Wednesday, March 04, 2009

Porting CodeMirror syntax parser to Bespin

The first screenshot (or the second, if the images are in a row) shows the current bespin syntax highlighting engine. It applies on a per-line base regular expressions to highlight the different parts. This approach is very fast, and new language color definitions can easily be added, but the engine is not aware of the internal structure of the code the user is typing in, and therefore such an engine cannot be extended to show in realtime syntax errors and provide intelligent code indentation and completion. The engine splits code into the following elements for colorizing:
  • comments
  • C-style comments
  • keywords
  • strings
  • punctuation
The second screenshot shows an early stage of a new approach I am working on, a syntax engine which uses a real tokenizer and parser (borrowed from the CodeMirror project), fully aware of the structure of the code. So far the initial coloring is fine, but to get it work continuously and smooth and with large files, there are a lot of problems and a lot of work to solve them ahead.

The second screenshot (or first, if the images are in a row) also shows that a few more Javascript code elements got identified and colored:
  • atoms
  • variables
  • variable definitions
  • local variables
  • properties
  • operators
I have chosen Javascript as the first language to port to this new syntax engine, because it is the most important one for me and maybe for web apps in general. But JS is a dynamic language and syntax analys is tricky. The good thing about CodeMirror is that beside of JS there exist already tokenizers / parsers for:
  • CSS
  • HTML mixed (with JS, CSS)
  • PHP mixed (with JS, CSS, HTML)
  • Django mixin (my proof-of-concept, never got officially released)
This new syntax engine, if I ever get it working in an acceptable way, is not supposed to replace the existing one. If desired and enabled, it will try to provide a better user experience, but will use the original syntax engine as fail back.

Saturday, February 28, 2009

Bespin - experimenting with embedded preview

While a browser based text editor or web development environment has many disadvantages compared to the desktop variant, there a also some great possibilities which haven't been explored yet with bespin. Possibilities such as having direct browser DOM access for previewing and possibly manipulating content and that is not limited to text. Images (img tags and background images) can also be edited live (with a bit of canvas magic, as e.g. in pixastic) and server round trips are only required to store the edited images. Traditional editors can only dream of this and adding this possibilities to monster IDEs makes them even more complex.
So here is my proposal (including fully functional early stage prototype) for a live preview, see the screenshots above, just clicking on the eye icon at the toolbar and at the place of the editor canvas with the HML source code, a preview panel gets blended in. Unfortunately there are a few issues. Lets take a look at the style definition of the example. It has defined properties for the "body". But we don't want to change the bespin body properties. So what I am doing to prevent a visual and usability disaster is parsing (not really in the lexical sense) the HTML template, extracting styles and injecting them as filtered CSS rules into the page when rendering the preview and removing the rules when switching off the preview.

So far this is only for HTML templates. But my idea is to have a pluggable template parser system so we can expand PHP, django or other templates. While this is not easy, I have done it before (ErlyDTL, the Django template language for Erlang) and I have used a javascript template system before: dojox.dtl - dojo django rendering. So why not use dojox.dtl for bespin template rendering ? Well, first it is specific for django, second it is text and node based (for performance reasons). That is good for an app where template parts get swapped frequently, but just for preview we don't need a node based template system, at least not for now because it adds a lot of complexity.

And one more thing to consider are the edge cases in which this approach won't work. Hopefully with enough trickery the embedded preview will work good enough in most cases. The bespin toolbar and commando line will anyway prevent a full-browser preview, unless they are blended out as well, which is currently not implemented and I am not even sure whether it would be a good idea to do so.

If you wanna contribute to the technical discussion, please user the google bespin core group.

Wednesday, February 25, 2009

Does now web development start to move into the browser ?


The browser has always been the most common tool to consume content from the internet. It also has served to create simple text based content (CMS). But now the tools start to appear for creating more complex content like web applications direclty in the browser. Let me list what I have tried out (or just heard of) recently.
  • bespin the super-cool and wicked-fast browser code editor (which since today contains a few lines of code I have contributed)
  • 280atlas, just been announced yesterday, an amazingly awesome looking visual builder for cappccino web apps, not available yet, except of the screencast.
  • Wavemaker, they have been around for a while, very enterprisy, it did not work for my out of the box when I tried a few months ago (isn't the first impression what counts most ?).
  • AmyEditor. Looks like an online version of TextMate. That is good, because TextMate looks and feels great and people just love TextMate, but is bad because I believe the browser is not the right place for embedding a desktop app alike. And when resizing the browser, that app did not resize, so that was the early ending of my exploration of that otherwise probably great app.
  • Coghead. Was a cloudbased visual app builder. They failed, are on the deadpool now. At least they could sell some of their IP to SAP. AFAIK, Coghead was not open source, halfway flash based and had a lot of proprietary things, even a proprietary programming language. Maybe that is not the way to go, unless you have more salesmen than developers in the team.
Beside of bespin these web apps all seem to look like desktop apps. I am not sure whether this is what developers will feel most comfortable with. Look at gmail, greader and other highly successful web apps. They don't look like and feel like desktop apps.

There are also cool graphics apps from aviary, for creating artwork. They are flash based and take more time to load than Pixelmator on my laptop.

Tuesday, February 24, 2009

bespin now dojo-powered

well, at least on my laptop, but I submitted a patch, so if the Mozilla guys like it, I hope it goes into trunk. The screenshot shows bespin running on the brand new Safari 4 beta.

There are still things to do related to the port, for example eliminating global variables outside of dojo and bespin namespace, dojo build profile and fixing the zillons of bugs I probably have introduced (but at least one I fixed, mousewheel works on Firefox as well)

Sunday, February 22, 2009

Porting bespin to dojo - 90 percent done

No I try to break the number one rule in software development: the last 10 percent take 90% of the total time. The dashboard works, except the command line sometimes having hiccups. The editor lets me type in text, but anything else breaks. I also replaced all third party libraries (in the external folder) with dojo equivalents. Here my experience in regard to prototype:
  • It's an absolutely fantastic framework if you wanna simple things get done quickly and you don't care about JS design philosophies (name spaces, native object extension).
  • There are a lot of utility functions for string, array and enumerable manipulation, but some of these functions are "opinionated" as all the great things from the 37systems guys. In dojo you end up cooking together your own one-line solutions for the simple stuff. It is additional work, but results in a customized and optimized solution.
  • The huge amount of string utilities suggests that prototype is targeted to developers who prefer not to deal too much with regular expressions.
So what else is missing once the bug fixing is done:
  • Name space cleanup: I used a all-lowercase namespace (e.g.: "bespin.editor"), because that is how dojo name space and file hierarchy looks like. I regret that, because it violates the bespin coding guideline and worst of all, requires a lot of slave work to update the documentation, so I will refactor to a name space mostly identical to the current one from bespin trunk (e.g.: "Bespin.Editor")
  • There are still some global variables (in the startup scripts), but before I even suggest how to change that I need some feedback from the bespin architects.
  • Testing (I introduced lots of bugs ...)
  • Build script to create minimized JS/CSS for production, maybe as paver task.
Update: I found a cheat sheet for conversion between dojo <->prototype

Thursday, February 19, 2009

Exploring bespin - and porting it to dojo

After some months absent from blogging and open source software involvement, the launch of bespin motivated me to get back to fun-hacking and blogging. Bespin is a browser based code editor which feels and smells like a real editor (vim, emacs, textmate), and not like a form on a webpage as all of the other browser based code editors (including an experimental one I hacked together myself). Internally bespin uses canvas and therefore currently only runs well on Firefox or webkit nightly. As JS lib Protoptype was choosen, but that might change in favor of dojo. So I thought I will give it a try, to port bespin to dojo and learn about the internals of bespin. And it might serve as suggestion in case bespin officially gets ported to dojo.

So where we are so far ? The screenshot at right shows that dashboard canvas rendering halfway works already, but with scaling dimensions something went wrong. Prototype makes the simple things very simple, dojo doesn't care much about the simple things, so I had to hand code a few things which had ready-to-use counterparts in prototype. But the really ugly thing about Prototype based javascript development is the global name space pollution and the extension of native objects, that is so much better handled with dojo and its class system. Of course nobody cares about those details once an app is up and running. Will finish the port and submit it to bespin bugzilla, but it might take a few days to iron out the bugs.

Sunday, September 21, 2008

Javascript performance optimization with jeene

Karl Krukov is working on Jeene, a Javascript library for partial evaluation, based on Douglas Crockford’s Pratt JS parser. Karl has just published some benchmarks (across all common browsers), where Jeene brings performance optimization of approximately 50%.

Tuesday, September 02, 2008

Thursday, August 07, 2008

Online publishing with Issuu.com

Issuu has an interesting solution for online publishing of print magazines and other publications. It is partially in flash, I usually don't like Flash, but their Flash stuff is really well done. And they take interesting approaches for the surrounding stuff of a web app as well, take a look at their documentation: http://platform.issuu.com, it's all google groups pages !

Tuesday, July 22, 2008

Perl on Google App Engine

Brad Fitzpatrick, the guy who added memchached to the Google App Engine, is starting a new project at Google: adding Perl support to the App Engine.

Monday, July 21, 2008

10gen: Cloud hosting platform offering serverside Javascript

There was some speculation that Google will offer it once they open up the App Engine Platform to more languages, there were some small-scale attempts with serverside Javascript (including the mine, with http://erlyjs.googlecode.com, which is currently receiving very little attention from my side) but now 10gen (with well known Java hacker and director of the Apache Software Foundation Geir Magnusson as CoFounder) plans to offer cloud-based hosting solution with any language running on the Java VM (at least that is what I am reading between the lines of their press release) and the funny thing is they don't start with Java itself, no they offer as first Language Javascript. The guys at 10gen also seem to like django, a quick look at one of the tutorials reveals they are providing a thing called djang10, django inspired framework for creating web applications purely in Javascript.

Saturday, June 28, 2008

Erlang Exchange 2008

The last two days I have spent at the Erlang Exchange Conference. It was great to met other Erlang hackers in person I just knew from their blog, emails or mailing list. Below the slides from my talk:

Saturday, June 14, 2008

Javascript syntax highligting for Firebug

The syntax highligting capabilities of CodeMirror, my favorite Javascript based code editor (for Web IDE-type applications) found their way into Firebug, currently available as additional extension.

Friday, June 06, 2008

Google App Engine limitations and workarounds

One of the current limitations of the Google App Engine is the lack of scheduled background processes. But early adapters are creative to circumnavigate this and other limitations. The most simple solution to this problem has William Vambenepe: he uses Google Reader to subscribe to a dummy RSS feed on his App Engine hosted test app, that results in some kind of regular polling with an interval of about 30 minutes. Of course this can't be used for any serious stuff, and the eight seconds maximum lifespan of a request also is not helping much to improve the situation. William concludes:
In the meantime, this was a fun exploration of the GAE environment. It makes it clear to me that this environment is still a toy. But a very interesting and promising one.
A more sophisticated but also much more complicated solution to the same problem has Peter Dolan: HTTPMR, a Map Reduce implementation in Python for running on HTTP based web clusters such as Google App Engine.

I am experimenting myself with a completely rewritten, not yet released version of ErlyComet, for adding a RESTful, highly scalable Comet layer with transactional cache in front of Google App Engine (or any other service which provides easy, cheap, reliable and scalable solution for persistence and shortlived HTTP requests)

Sunday, June 01, 2008

Malicious Flash fullscreen mode

Go there an try it yourself. It doesn't do any real harm, but it can scare people, especially those not really familiar with the latest capabilities of the flash player, such as full screen mode. While this example is just fun and actually the first example of this kind I have seen, I guess the same principles could also be used for real malicious stuff (e.g.: a new kind of phishing).

Thursday, May 29, 2008

IETester - makes web page testing on IE less painful

Previously I was switching between several virtually installed Windows XP instances on Parallels (for Mac OS X), just for testing a web page on different versions of Internet Explorer. Now I have installed IETester, a free Windows tool, which allows to run simultaneously (and switch between) the following IE versions:
  • IE 5.5
  • IE 6
  • IE 7
  • IE 8 beta 1

Monday, May 26, 2008

Notapad - the start of a dojo based WebIDE

Niccola Rizzo has been working for a couple of months now on the CodeTextArea dojo widget for in-browser Javascript code editing and he has just launched a public demo and test site named notaPad.

The editor provides Javascript syntax highlighting and additional functionalities with the following key bindings:

CTRL + space - autocomplete
CTRL + l - GOTO LINE
CTRL + b - GOTO MATCHING BRACKET
CTRL + c - COPY
CTRL + x - CUT
CTRL + v - PASTE
right click on the row numbers - BOOKMARKS

Sunday, May 25, 2008

blogger.com not updating homepages

Shame on you, Google ! The last few posts of this blog did only update my blog homepage after about 24 hours. I guess updating the homepage is not part of the transaction when a new post gets stored somewhere on BigTable. So if you wanna see the actual homepage of this blog instead of an out-of-date cached version, type in:

http://rsaccon.com/index.html (instead of http://rsaccon.com)

Anyway, real men build their own blog engine, instead of complaining. Right now new blog engines are popping up like mushrooms, as result of some 60000 developers messing around with Google App Engine. Unfortunately those real men are facing real problems when trying to completely import old blogs into their custom blog engine, at least that is what happened to me when I tried some years ago with a custom Rails blog engine. So for now let's hope the blogger.com issues are just temporary ...

Update: It's getting worse. Right after posting, http://rsaccon.com/index.html also continued to serve the outdated cached version.

Update II: A couple of hours later, at least the .../index.html home page got updated.

Saturday, May 24, 2008

js2-mode - the second most important Javascript tool

I have been using js2-mode (an Emacs Javascript mode) for a couple of weeks now. I started using it out of curiosity, at the time I gave up on TextMate as all-purpose editor and turned back to Emacs, mainly because Emacs has so much better Erlang support. Now I consider js2-mode as the second most important Javascript tool (nothing can beat Firebug of course, and Firebug 1.2B1 on Firefox 3RC1 really rocks !). It fits perfectly into my work flow, you don't have to learn anything specifically to use it, it shows on-the-fly any syntax errors, global variables (which usually are unintentional), does a great job of line indenting, but because line indenting is often a question of personal style, js2-mode offers several possibilities you can cycle through via TAB. And in case you don't like how js2-mode works out-of-the-box, there are tons of customization options.

The only thing I am missing: I would like to use js2-mode as well as minor mode in HTML, but that doesn't seem to work yet.

Update: After writing this post, I just discovered another recent article about js2-mode with focus on syntax highlighting.

Friday, May 23, 2008

Dojo Widget for in-browser editor CodeMirror

CodeMirror, a very impressive in-browser code editor for Javascript, XML/HTML or CSS (or any language, you just have to plug in a your own parser) made some nice progress in the last months. CodeMirror has no dependency on other frameworks or libraries. If you want to use it in a dojo environment as a dojo compatible widget, here I am gonna share here a little tutorial how to write such a thing:

First download CodeMirror and transform CodeMirror.js (the main file which loads the other files into an iframe), into something like this:

dojo.provide("mystuff.widget.CodeMirror");
dojo.require("dijit._Widget");


dojo.declare("mystuff.widget.CodeMirror", dijit._Widget, {

initialized: false,

// currently supported: 'xml' (HTML), 'js' or 'css'
type: 'xml',

options: {
stylesheet: "",
path: "/static/codemirror/js/",
parserfiles: [],
basefiles: ["codemirror_iframe.js"],
linesPerPass: 15,
passDelay: 200,
continuousScanning: false,
saveFunction: function() {
console.log('save');
},
content: " ",
undoDepth: 20,
undoDelay: 800,
disableSpellcheck: true,
textWrapping: true,
readOnly: false,
width: "100%",
height: "100%",
parserConfig: null
},

postMixInProperties: function() {
this.options.stylesheet = "/static/codemirror/css/" + this.type + "colors.css";
this.options.parserfiles = ["parse" + this.type + ".js"];
},

postCreate: function() {
this.inherited(arguments);
},

startup: function() {
if (dijit._isElementShown(this.domNode.parentNode))
this.initialize();
},

initialize: function() {
if (this.initialized)
return;

frame = document.createElement("IFRAME");
frame.style.border = "0";
frame.style.width = this.options.width;
frame.style.height = this.options.height;
// display: block occasionally suppresses some Firefox bugs, so we
// always add it, redundant as it sounds.
frame.style.display = "block";

this.domNode.appendChild(frame);

// Link back to this object, so that the editor can fetch options
// and add a reference to itself.
frame.CodeMirror = this;
this.win = frame.contentWindow;

var _this = this;
var html = ["<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.options.stylesheet + "\"/>"];
dojo.forEach(this.options.basefiles.concat(this.options.parserfiles), function(file) {
html.push("<script type=\"text/javascript\" src=\"" + _this.options.path + file + "\"></script>");
});
html.push("</head><body style=\"border-width: 0;\" class=\"editbox\" spellcheck=\"" +
(this.options.disableSpellcheck ? "false" : "true") + "\"></body></html>");

var doc = this.win.document;
doc.open();
doc.write(html.join(""));
doc.close();

this.initialized = true;
},

getCode: function() {
return this.editor.getCode();
},

setCode: function(code) {
this.editor.importCode(code);
},

focus: function() {
this.win.focus();
},

jumpToChar: function(start, end) {
this.editor.jumpToChar(start, end);
this.focus();
},

jumpToLine: function(line) {
this.editor.jumpToLine(line);
this.focus();
},

currentLine: function() {
return this.editor.currentLine();
},

selection: function() {
return this.editor.selectedText();
},

reindent: function() {
this.editor.reindent();
},

replaceSelection: function(text, focus) {
this.editor.replaceSelection(text);
if (focus) this.focus();
},

replaceChars: function(text, start, end) {
this.editor.replaceChars(text, start, end);
},

getSearchCursor: function(string, fromCursor) {
return this.editor.getSearchCursor(string, fromCursor);
}
});
Then your should concatenate and minifiy (I use YUI compressor) all the JS files:

util.js, stringstream.js, select.js, undo.js, editor.js
=> codemirror_iframe.js

Also minify the parsers: parsejavascript.js, tokenizejavascript.js, parsecss.js, parsexml.js

At this point you can embed the widget into a test page:
<div
id="html_editor"
dojoType="mystuff.widget.CodeMirror"
type="xml"
style="height:100%;">
</div>
Now comes the tricky part: the editor does not initialize automatically at page load, because with dojo, chances are very high, you are gonna use this widget inside a container, where the editor is hidden at page load, and that would cause trouble with some browsers. So you need to subscribe to an event which triggers the visibility of the editor, so it can be lazy-initialized at first time it becomes visible. For example to use CodeMirror inside a tab container with a tab with id 'html_editor_tab' I do something like this:
dojo.declare("MyApp", null, {
main_tab_selected: function(page) {
if (page.id == 'html_editor_tab'){
dijit.byId('html_editor').initialize();
}
}
});

var myAppInstance = new MyApp();

dojo.subscribe("main_tabs-selectChild", myAppInstance, "main_tab_selected");
Update: Uhh, where was my brain when I wrote this article, I accidentally "misspelled" CodeMirror with CodePress at several places, including the title, it's corrected now ...