Skip to content

Instantly share code, notes, and snippets.

@redraiment
Last active August 29, 2015 14:01
Show Gist options
  • Save redraiment/1e953fb1500ac0ede1c2 to your computer and use it in GitHub Desktop.
Save redraiment/1e953fb1500ac0ede1c2 to your computer and use it in GitHub Desktop.
Embedded JavaScript
var File = function(name) {
this.file = new java.io.File(name);
};
File.prototype.map = function(fn) {
var fin = new java.util.Scanner(this.file);
var content = [];
while (fin.hasNextLine()) {
content.push(fn(fin.nextLine()));
}
fin.close();
return content;
};
var map = function(list, fn) {
var rs = [];
for (var i = 0; i < list.length; i++) {
rs.push(fn(list[i]));
}
return rs;
};
var code = map(arguments, function(name) {
var file = new File(name);
return file.map(function(line) {
if (line.charAt(0) === 37) {
return line.substring(1).concat('\n');
} else {
return map(line.split("(?=\\[=)|(?<==\\])"), function(e) {
if (e.matches('\\[=.*?=\\]')) {
return 'print(' + e.replaceAll('^\\[=|=\\]$', '') + ');\n';
} else {
return 'print("' + e.replaceAll('"', '\\\\"') + '");\n';
}
}).join('') + 'print("\\n");\n';
}
}).join('');
}).join('\n');
eval(code);
#!/bin/bash
exec jrunscript -f "$(cygpath -w "$HOME/lib/js/ejs.js")" $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment