Skip to content

Commit

Permalink
Merge 03083c5 into 225aad0
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Laporte committed Apr 4, 2014
2 parents 225aad0 + 03083c5 commit f206f54
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 56 deletions.
3 changes: 1 addition & 2 deletions build/grunt/hspserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = function(grunt) {
grunt.registerTask('hspserver', 'Start a web server to server compiled templates on the fly', function () {

var renderer = require("../../hsp/compiler/renderer");
var transpiler = require("../../hsp/transpiler");

grunt.config.requires('hspserver.port');
grunt.config.requires('hspserver.base');
Expand Down Expand Up @@ -45,7 +44,7 @@ module.exports = function(grunt) {
if (r.serverErrors && r.serverErrors.length) {
res.send(500,r.serverErrors[0].description);
} else {
res.send(200,transpiler.processString(r.code).code);
res.send(200,r.code);
}
} else {
res.send(500,"[/hsp/compile] src parameter is undefined");
Expand Down
6 changes: 6 additions & 0 deletions hsp/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var parser = require("./parser/index");
var treebuilder = require("./treebuilder/index");
var jsgenerator = require("./jsgenerator/index");
var transpiler = require("../transpiler/index");

/**
* Compiles a template and return a JS compiled string and a list of errors.
Expand Down Expand Up @@ -44,5 +45,10 @@ exports.compile = function (template, path, includeSyntaxTree, bypassJSvalidatio
//Step3 : jsgenerator
res = jsgenerator.generate(res, template, fileName, dirPath, includeSyntaxTree, bypassJSvalidation);

//Step4 : transpiler
if (!res.errors || res.errors.length===0) {
res.code=transpiler.processString(res.code).code;
}

return res;
};
11 changes: 10 additions & 1 deletion hsp/compiler/jsgenerator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var jsv = require("./jsvalidator/validator");
* Header added to all generated JS file
*/
var HEADER_ARR = [
'',
'// ################################################################ ',
'// This file has been generated by the hashspace compiler ',
'// Direct MODIFICATIONS WILL BE LOST when the file is recompiled! ',
Expand Down Expand Up @@ -121,7 +122,15 @@ function _validate (code, lineMap) {
function _getErrorScript (errors, fileName) {
var result = '';
if (errors && errors.length) {
result = ['\r\nrequire("hsp/rt").logErrors("', fileName, '",', JSON.stringify(errors, null), ');\r\n'].join("");
var err=errors[0];
var ctxt={
type:"error",
file:fileName,
code:err.code,
line:err.line,
column:err.column
};
result = ['\r\nrequire("hsp/rt/log").error("',err.description,'",', JSON.stringify(ctxt, null) ,');\r\n'].join("");
}
return result;
}
Expand Down
41 changes: 0 additions & 41 deletions hsp/rt.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@ refresh.addTemplate = function (tpl) {
}
};

/**
* Display a template inside an HTML container sample usage: hsp.display(myTemplate(param1,param2),"mydiv");
* @param {$RootNode} tplResult the template node generated by a template
* @param {string|DOMElement} container the HTML container element or its id
* @param {Boolean} replace if true, the template result will replace the element content - otherwise it will be
* appended (default: true)
*/
module.exports.display = function (tplResult, container, replace) {
// TODO - Validate argument types / implement nice error messages
if (container && tplResult) {
if (tplResult.render === undefined || tplResult.render.constructor !== Function) {
throw new Error("[hsp.display] Invalid argument: tplResult is not a valid template result");
} else {
tplResult.render(container, replace);
}
}
};

/**
* Helper to create template functions
* @param {Array|Object} arg the list of argument names - e.g. ["label", "value"]
Expand Down Expand Up @@ -191,29 +173,6 @@ module.exports.template = function (arg, contentFunction) {
};


/**
* Helper function used by the hashspace compiler to raise errors from the generated scripts when syntax is invalid
*/
module.exports.logErrors = function (fileName, errors) {
if (errors && errors.length) {
var err, code;
for (var i=0,sz=errors.length;sz>i;i++) {
err=errors[i];
code=null;
if (err.lineInfoTxt) {
code = err.lineInfoTxt.replace(/\r?\n/gi, "\r\n>> ");
} else if (err.code) {
code = err.code;
}
log.error(err.description,{
file:fileName,
line:err.line,
column:err.column,
code:code
});
}
}
};

/**
* Collection of the node types supported by the NodeGenerator This collection is attached to the Nodegenerator
Expand Down
2 changes: 1 addition & 1 deletion public/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var Playground = module.exports = klass({
*/
showSample : function (sampleIdx) {
// load layout template
hsp.display(layout.mainLayout(this.data, this), this.containerId);
layout.mainLayout(this.data, this).render(this.containerId);
this.initEditor();
this.loadSample(sampleIdx);
},
Expand Down
2 changes: 1 addition & 1 deletion public/samples/clickhandler/clickhandler.hsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var msg={text:""}, count=-1;

# template message(msg)
<div title="click me!" onclick="{changeMessage()}" onselectstart="return false">
{if msg.isWarning}<div class="warning">WARNING: </div>{/if}
{if msg.isWarning}<div class="warning">WARNING:&nbsp;</div>{/if}
{msg.text}
</div>
# /template
Expand Down
5 changes: 4 additions & 1 deletion public/samples/timer/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ This example originally demonstrated in the [react framework][react] documentati

[#output]

The most important point to note in this example is the **$set** utility that has to be used to update objects or values in the data-model. This utility ensures that objects observing the data-model will be notified of the changes. In the mid-term this utility should become obsolete once the [Object.observe][objobserve] feature is implemented by all web-browsers.
The most important point to understand is that **hashspace is reprocessing JavaScript** to introduce a partial polyfill to [Object.observe][objobserve] in order to detect changes that occur to JavaScript objects. Hashspace actually uses a transpiler to encapsulate assignments with an internal **$set()** method that performs the assignment and notifies the potential observers. You can see the result [here][timer.hsp].

In the mid-term the *$set()* utility will become obsolete once the [Object.observe][objobserve] feature is implemented by all web-browsers - and hashspace will rely on the browser's Object.observe implementation.

The $set function has the following signature:

Expand All @@ -15,3 +17,4 @@ You can also note the **klass** utility that is used to create the **Timer** con

[react]: http://facebook.github.io/react/
[objobserve]: http://www.youtube.com/watch?v=VO--VXFJnmE
[timer.hsp]: /samples/timer/timer.hsp
11 changes: 5 additions & 6 deletions public/test/compiler/errtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ describe('Template compilation errors: ', function () {
var s = [
jsgenerator.HEADER,
'\r\n',
'require("hsp/rt").logErrors("mixed1",[{',
'"description":"SyntaxError: Unexpected token",',
'"lineInfoTxt":" foo({blah:\\\"hello\\\",});\\r\\n----------------------^--",',
'"lineInfoHTML":"<span class=\\\"code\\\"> foo({blah:\\\"hello\\\",<span class=\\\"error\\\" title=\\\"SyntaxError: Unexpected token\\\">}</span>);</span>",',
'"code":" foo({blah:\\\"hello\\\",});",', '"line":13,', '"column":22', '}]);\r\n'].join('');

'require("hsp/rt/log").error(',
'"SyntaxError: Unexpected token",',
'{"type":"error","file":"mixed1","code":" foo({blah:\\"hello\\",});","line":13,"column":22}',
');\r\n'
].join('');
assert.equal(r.code, s, "generated code");

});
Expand Down
8 changes: 5 additions & 3 deletions public/test/compiler/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,18 @@ describe('Block Parser: ', function () {
// assert.equal(r.code,s,"template generated code"); // strange issue with non visible characters
assert.equal(ut.compareJSCode(r.code.replace(/\r/g, ""), s), "", "template generated code");

var lm = [0, 5, 6, 7, 8, 8, 8, 13, 14, 15, 16, 17, 17, 17, 17, 22];
var lm = [0, 6, 7, 8, 9, 9, 9, 14, 15, 16, 17, 18, 18, 18, 18, 23];
assert.equal(ut.jsonContains(r.lineMap, lm, "lineMap"), "", "line map comparison");
});

it('validates full compiled template with export', function () {
var sample = ut.getSampleContent("template2");
var r = compiler.compile(sample.template, "template2");

var s = [jsgenerator.HEADER, '', 'var hello4 = exports.hello4 = require("hsp/rt").template([], function(n){',
' return [n.$text(0,["Hello World!"])];', '});'].join("\n");
var s = ['var $set=require("hsp/$set"); ',
jsgenerator.HEADER, '',
'var hello4 = $set(exports, "hello4", require("hsp/rt").template([], function(n){',
' return [n.$text(0,["Hello World!"])];', '}));'].join("\n");

assert.equal(r.errors.length, 0, "no compilation error");
// console.log(s.length) // 587
Expand Down

0 comments on commit f206f54

Please sign in to comment.