Skip to content

Instantly share code, notes, and snippets.

@youpy
Created March 20, 2009 05:24
Show Gist options
  • Save youpy/82233 to your computer and use it in GitHub Desktop.
Save youpy/82233 to your computer and use it in GitHub Desktop.
/* appjet:version 0.1 */
print(H1(appjet.appName));
print(P('Library for building simple text converter application'));
print(P(
A({href:'http://docs.lib-text-converter.appjet.net/'}, 'documentation'),
' / ',
A({href:'http://text-converter-example.appjet.net/'}, 'example'),
' / ',
A({href:'http://text-converter-example.appjet.net/convert?q=hello'}, 'result(plain)'),
' / ',
A({href:'http://text-converter-example.appjet.net/convert?q=hello&format=json'}, 'result(JSON)'),
' / ',
A({href:'http://text-converter-example.appjet.net/convert?q=hello&format=json&callback=foo'}, 'result(JSONP)')
));
print(H2('functional style'));
print(PRE("""/* appjet:version 0.1 */
import("lib-text-converter");
convertText(function(text) {
return text + '!!!';
});
"""));
print(H2('OOP style'));
print(PRE("""/* appjet:version 0.1 */
import("lib-text-converter");
var converter = new TextConverter();
converter.setConverter(function(text) {
return text + '!!!';
});
converter.dispatch();
"""));
/* appjet:library */
import("lib-json");
var TextConverter = function() {
if(!(this instanceof arguments.callee)) {
return new arguments.callee();
}
this.params = [];
this.converter = function(text) {
return text;
};
this.renderer = function(form) {
return raw(form);
};
}
TextConverter.prototype.addParam = function(name, label, size) {
this.params.push({
name: name,
label: label || name,
size: size || 20
});
};
TextConverter.prototype.setConverter = function(func) {
this.converter = func;
};
TextConverter.prototype.setRenderer = function(func) {
this.renderer = func;
};
TextConverter.prototype.dispatch = function() {
var self = this;
var inputs = this.params.map(function(param) {
return P(param.label +':', BR(), INPUT({text:"",name:param.name,size:param.size}));
});
patternDispatch.apply(null,
[[/^\/$/, function() {
print(H1(appjet.appName));
print(raw(self.renderer(FORM.apply(null,
[{action:"/convert", method:"get"}]
.concat([P('text:', BR(), INPUT({text:"",name:'q',size:50}))])
.concat(inputs)
.concat(P(INPUT({type:"submit",value:"convert"})))))));
print(UL(
LI(A({href: '/convert?q=foo&format=json'}, 'JSON')),
LI(A({href: '/convert?q=foo&format=json&callback=callback'}, 'JSONP'))));
}],
[/^\/convert/, function() {
page.setMode("plain");
var format = request.params['format'] || 'plain';
var callback = request.params['callback'] || '';
var text = request.params['q'] || '';
var result = self.converter(text, request.params);
if(format == 'json') {
response.setHeader('content-type', 'application/json');
result = JSON.stringify({result: result});
if(callback.match(/^[a-zA-Z_]+$/)) {
result = callback + '(' + result + ');';
}
} else {
response.setHeader('content-type', 'text/plain');
}
print(raw(result));
}]]);
};
var convertText = function(func) {
var converter = new TextConverter();
converter.setConverter(func);
converter.dispatch();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment