Skip to content

Commit

Permalink
support comments and _call prop for tokens (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jun 14, 2018
1 parent 6b758b8 commit f651b2f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/electrode-react-webapp/lib/async-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class AsyncTemplate {
const pos = template.indexOf(tokenOpenTag, pt);
if (pos >= pt) {
const str = template.substring(pt, pos).trim();
// if there are text between a close tag and an open tag, then consider
// that as plain HTML string
if (str) tokens.push({ str });

const ex = template.indexOf(tokenCloseTag, pos);
Expand All @@ -81,7 +83,8 @@ class AsyncTemplate {
.trim()
.split("\n")
.map(x => x.trim())
.filter(x => x)
// remove empty and comment lines that start with "//"
.filter(x => x && !x.startsWith("//"))
.join(" ");

const token = remain.split(" ", 1)[0];
Expand Down
16 changes: 15 additions & 1 deletion packages/electrode-react-webapp/lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Token {
this.custom = undefined;
this.wantsNext = undefined;
this.props = props || {};
if (this.props._call) {
this._modCall = [].concat(this.props._call);
}
}

// if token is a module, then load it
Expand All @@ -29,7 +32,18 @@ class Token {
viewTokenModules[this.id] = tokenMod;
}

this.custom = tokenMod(options || {}); // call setup function to get an instance
if (this._modCall) {
// call setup function to get an instance
const params = [options || {}, this].concat(this._modCall[1] || []);
assert(
tokenMod[this._modCall[0]],
`electrode-react-webapp: _call of token ${this.id} - '${this._modCall[0]}' not found`
);
this.custom = tokenMod[this._modCall[0]].apply(undefined, params);
} else {
this.custom = tokenMod(options || {}, this);
}

assert(
this.custom && this.custom.process,
`custom token ${this.id} module doesn't have process method`
Expand Down
4 changes: 4 additions & 0 deletions packages/electrode-react-webapp/test/data/template3.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
"test": [1,2,3]
}
}-->
<!--%{
#./test/fixtures/custom-call
_call="setup"
}-->
14 changes: 14 additions & 0 deletions packages/electrode-react-webapp/test/fixtures/custom-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

function setup() {
return {
name: "custom-call",
process: function() {
return `_call`;
}
};
}

module.exports = {
setup
};
16 changes: 16 additions & 0 deletions packages/electrode-react-webapp/test/spec/parse-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const AsyncTemplate = require("../../lib/async-template");
const Path = require("path");
const expect = require("chai").expect;
const _ = require("lodash");

describe("AsyncTemplate._parseTemplate", function() {
it("should parse template into tokens", () => {
Expand Down Expand Up @@ -211,8 +212,23 @@ describe("AsyncTemplate._parseTemplate", function() {