Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' of git@github.com:jbarnette/johnson
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarnette committed Apr 19, 2008
2 parents 2c8606c + 8a70209 commit 0f242d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Manifest.txt
Expand Up @@ -23,6 +23,8 @@ ext/spidermonkey/ruby_land_proxy.c
ext/spidermonkey/ruby_land_proxy.h
ext/spidermonkey/spidermonkey.c
ext/spidermonkey/spidermonkey.h
js/prelude.js
js/template.js
lib/johnson.rb
lib/johnson/context.rb
lib/johnson/nodes.rb
Expand All @@ -44,7 +46,6 @@ lib/johnson/visitable.rb
lib/johnson/visitors.rb
lib/johnson/visitors/ecma_visitor.rb
lib/johnson/visitors/sexp_visitor.rb
lib/prelude.js
test/helper.rb
test/johnson/context_test.rb
test/johnson/conversions/boolean_test.rb
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions js/template.js
@@ -0,0 +1,30 @@
$ = {};
$.makeTemplate = function (template, begin, end) {
var begin = begin || "<%"
var end = end || "%>"
var rebegin = begin.replace(/([\]{}[\\])/g, '\\$1');
var reend = end.replace(/([\]{}[\\])/g, '\\$1');

var code = "_context = _context || {}; with (_context) {" +
"var _result = '';" +
template
.replace(/[\t\r\n]/g, ' ')
.replace(/^(.*)$/, end + '$1' + begin)
.replace(new RegExp(reend + "(.*?)" + rebegin, "g"), function (text) {
return text
.replace(new RegExp("^" + reend + "(.*)" + rebegin + "$"), "$1")
.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/^(.*)$/,
function(sub, m) {
return end + (m == "" ? "" : "_result += '" + m + "';") + begin;
});
})
.replace(new RegExp(rebegin + "=(.*?)" + reend, "g"), "_result += ($1);")
.replace(new RegExp(rebegin + "(.*?)" + reend, "g"), ' $1 ')
.replace(new RegExp("^" + reend + "(.*)" + rebegin + "$"), '$1') +
"return _result;" +
"}";

return new Function("_context", code);
};
4 changes: 3 additions & 1 deletion lib/johnson.rb
Expand Up @@ -24,8 +24,10 @@
require "johnson/context"
require "johnson/parser"

$LOAD_PATH.push File.expand_path("#{File.dirname(__FILE__)}/../js")

module Johnson
PRELUDE = IO.read(File.dirname(__FILE__) + "/prelude.js")
PRELUDE = IO.read(File.dirname(__FILE__) + "/../js/prelude.js")

def self.evaluate(expression, vars={})
context = Johnson::Context.new
Expand Down

0 comments on commit 0f242d2

Please sign in to comment.