Skip to content

Commit 209f878

Browse files
author
Tobias Brennecke
committed
Nicer package.json, prepare for release
1 parent ca3265a commit 209f878

File tree

5 files changed

+141
-50
lines changed

5 files changed

+141
-50
lines changed

.eslintrc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"env": {
33
"node": true,
4-
"builtin": true
4+
"builtin": true,
5+
"es6": true
6+
},
7+
"parserOptions": {
8+
"ecmaVersion": 6,
9+
"sourceType": "module"
510
},
611
"globals": {},
712
"rules": {
@@ -67,7 +72,7 @@
6772
"no-with": 2,
6873
"quotes": [0, "single"],
6974
"radix": 2,
70-
"semi": [0, "never"],
75+
"semi": [1, "always"],
7176
"strict": 0,
7277
"space-before-blocks": 1,
7378
"space-before-function-paren": [1, {

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
## The Thymol engine for Pattern Lab / Node
1+
The Thymol engine for Pattern Lab / Node
2+
========================================
3+
4+
This engine adds [Thymeleaf](http://www.thymeleaf.org/) support to the Node edition of Pattern Lab using [headissue/thymol-node](https://github.com/headissue/thymol-node).
5+
6+
Installing
7+
----------
28

39
To install the Thymol engine in your edition, `npm install patternengine-node-thymol` should do the trick.
410

5-
Partial calls and lineage hunting are supported. Thymol does not support the mustache-specific syntax extensions, style modifiers and pattern parameters, because their use cases are addressed by the core Thymol feature set.
6-
Thymeleafs th:include is also not possible, use th:replace instead.
11+
Supported features
12+
------------------
13+
14+
- [x] [Includes](http://patternlab.io/docs/pattern-including.html)
15+
- [x] Lineage
16+
- [x] [Hidden Patterns](http://patternlab.io/docs/pattern-hiding.html)
17+
- [x] [Pseudo-Patterns](http://patternlab.io/docs/pattern-pseudo-patterns.html)
18+
- [x] [Pattern States](http://patternlab.io/docs/pattern-states.html)
19+
- [ ] [Pattern Parameters](http://patternlab.io/docs/pattern-parameters.html) (Accomplished instead using native Thymeleaf features)
20+
- [ ] [Style Modifiers](http://patternlab.io/docs/pattern-stylemodifier.html) (Accomplished instead using native Thymeleaf features)
21+
22+
Partial calls and lineage hunting are supported. Thymol does not support the mustache-specific syntax extensions, style modifiers and pattern parameters, because their use cases are addressed by the core Thymol feature set. Thymeleafs th:include is also not possible, use th:replace instead.
723

8-
**Note**: You _cannot_ use `th:replace` with other attributes on a tag. `th:replace` gets interpreted and string-replaced by the patternengine, even before thymol has the chance to parse it. One workaround for this is by wrapping it with a `<th:block>`.
24+
**Note**: You *cannot* use `th:replace` with other attributes on a tag. `th:replace` gets interpreted and string-replaced by the patternengine, even before thymol has the chance to parse it. One workaround for this is by wrapping it with a `<th:block>`.

lib/engine_thymol.js

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@
1616
*/
1717

1818
"use strict";
19-
var engine_thymol = (function() {
19+
20+
const domino = require('domino');
21+
const jQuery = require("jquery");
22+
23+
const engine_thymol = (function () {
2024
global.thymol = {};
2125

22-
thymol = require('thymol-node').thymol;
23-
var domino = require('domino');
24-
var jQuery = require("jquery");
25-
var jsdomOptions = {
26-
features: {
27-
FetchExternalResources: false,
28-
ProcessExternalResources: false
29-
}
30-
};
26+
const thymol = require('thymol-node').thymol;
3127

32-
var resetGlobals = function() {
28+
const resetGlobals = function () {
3329
global.thPrefix = undefined;
3430
global.thDataPrefix = undefined;
3531
global.thAllowNullText = undefined;
@@ -55,7 +51,7 @@ var engine_thymol = (function() {
5551
global.thDebug = undefined;
5652
};
5753

58-
var setDefaults = function() {
54+
const setDefaults = function () {
5955
thymol.thScriptPath = "";
6056
thymol.thDebug = true; // calls alert() if something bad happens
6157
thymol.thDefaultPrefix = "th";
@@ -77,22 +73,22 @@ var engine_thymol = (function() {
7773
setDefaults();
7874
thymol.thWindow = domino.createWindow("<html></html>");
7975
global.$ = jQuery(thymol.thWindow);
80-
thymol.jqSetup($);
76+
thymol.jqSetup(global.$);
8177
thymol.setup();
8278

8379
// patch thymol to support schema.org:
8480
thymol.appendToAttrList(thymol.processSpecAttrMod, 1e3, ["itemprop", "itemtype"]);
8581
thymol.appendToAttrList(thymol.processFixedValBoolAttr, 1e3, ["itemscope"]);
8682

87-
var renderThymeleaf = function(content, vars, messages) {
88-
var win = domino.createWindow(content);
83+
const renderThymeleaf = function (content, vars, messages) {
84+
const win = domino.createWindow(content);
8985
thymol.thDocument = win.document;
9086
thymol.thWindow = win;
9187
setDefaults();
9288
thymol.thDataThymolLoading = false;
9389

9490
global.$ = jQuery(thymol.thWindow);
95-
thymol.jqSetup($);
91+
thymol.jqSetup(global.$);
9692

9793
// add a fake window.top, thymol stores things in there
9894
thymol.thTop = {
@@ -103,34 +99,36 @@ var engine_thymol = (function() {
10399
thymol.reset();
104100

105101
// must be global
106-
thVars = vars;
107-
thMessages = messages;
108-
thDebug = true;
102+
global.thVars = vars;
103+
global.thMessages = messages;
104+
global.thDebug = true;
109105

110-
var result = {
106+
const result = {
111107
error: null
112108
};
113109

114110
// add an faked alert to get messages from thymols debugging mode
115-
var alert = function( arg ) {
111+
const alert = function (arg) {
116112
console.log(arg);
117113
result.error = "" + arg.name + ": " + arg.message;
118114
};
119115

120116
thymol.thWindow.alert = alert;
121117

122-
var resultDocument = thymol.execute(thymol.thDocument);
118+
const resultDocument = thymol.execute(thymol.thDocument);
123119

124120
result.rendered = resultDocument.documentElement.outerHTML;
125121
result.rendered = result.rendered.replace('<html><head></head><body>', '').replace('</body></html>', '');
126122
return result;
127123
};
128124

129-
var convertDataToThymol = function(data) {
130-
var result = [];
131-
Object.keys(data).forEach(function(k) {
132-
if (k == 'link') return;
133-
var value = data[k];
125+
const convertDataToThymol = function (data) {
126+
const result = [];
127+
Object.keys(data).forEach(function (k) {
128+
if (k === 'link') {
129+
return;
130+
}
131+
const value = data[k];
134132
result.push([k, value]);
135133
});
136134
return result;
@@ -141,9 +139,8 @@ var engine_thymol = (function() {
141139
engineName: 'thymol',
142140
engineFileExtension: '.html',
143141

144-
//Important! Needed for Twig compilation. Can't resolve paths otherwise.
142+
//Important! Needed for compilation. Can't resolve paths otherwise.
145143
expandPartials: true,
146-
//expandPartials: false,
147144

148145
// regexes, stored here so they're only compiled once
149146
findPartialsRE: /<(\S+)[^>]*th:replace=["'](.+)["'][^>]*>.*?<\/\1>/g,
@@ -152,13 +149,12 @@ var engine_thymol = (function() {
152149

153150
// render it
154151
renderPattern: function renderPattern(pattern, data) {
155-
var thyData = convertDataToThymol(data);
152+
const thyData = convertDataToThymol(data);
153+
154+
const result = renderThymeleaf(pattern.extendedTemplate, thyData);
156155

157-
var ms = new Date().getTime();
158-
var result = renderThymeleaf(pattern.extendedTemplate, thyData);
159-
console.log("Pattern: " + pattern.name + "," + (new Date().getTime() - ms));
160156
if (!!result.error) {
161-
console.error(pattern.name + ': ' + result.error);
157+
throw `${pattern.name}: ${result.error}`;
162158
}
163159
return result.rendered;
164160
},
@@ -168,22 +164,18 @@ var engine_thymol = (function() {
168164
return pattern.template.match(this.findPartialsRE);
169165
},
170166

167+
// Done by thymeleaf using th:style and th:with. This function is too mustachified in Pattern Lab.
171168
findPartialsWithStyleModifiers: function () {
172-
// TODO: make the call to this from oPattern objects conditional on their
173-
// being implemented here.
174169
return [];
175170
},
176171

177-
// returns any patterns that match {{> value(foo:"bar") }} or {{>
178-
// value:mod(foo:"bar") }} within the pattern
172+
// Use th:replace and th:include
179173
findPartialsWithPatternParameters: function () {
180-
// TODO: make the call to this from oPattern objects conditional on their
181-
// being implemented here.
182174
return [];
183175
},
184176

185177
// not supported with thymol, list_item_hunter is too mustachified.
186-
findListItems: function (pattern) {
178+
findListItems: function (/*pattern*/) {
187179
return [];
188180
},
189181

package.json

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,41 @@
44
"version": "0.1.0",
55
"main": "lib/engine_thymol.js",
66
"dependencies": {
7-
"jquery": "2.2.4",
7+
"jquery": "^3.1.1",
88
"domino": "1.0.26",
9-
"thymol-node": "2.0.1-pre.2"
9+
"thymol-node": "git+ssh://git@github.com:headissue/thymol-node.git"
10+
},
11+
"devDependencies": {
12+
"eslint": "^3.5.0",
13+
"nodeunit": "^0.10.2"
14+
},
15+
"keywords": [
16+
"Pattern Lab",
17+
"Atomic Web Design",
18+
"Node",
19+
"Grunt",
20+
"Gulp",
21+
"Javascript",
22+
"Thymeleaf",
23+
"Thymol"
24+
],
25+
"repository": {
26+
"type": "git",
27+
"url": "git://github.com/headissue/patternengine-node-thymol.git"
28+
},
29+
"bugs": "https://github.com/headissue/patternengine-node-thymol/issues",
30+
"author": {
31+
"name": "Maximilian Richt"
32+
},
33+
"contributors": [
34+
{
35+
"name": "Tobias Brennecke"
36+
}
37+
],
38+
"license": "MIT",
39+
"scripts": {
40+
"test": "eslint lib/**/*.js && eslint test/**/*.js && nodeunit test/*.js"
1041
},
11-
"devDependencies": {},
1242
"engines": {
1343
"node": ">=4.0"
1444
}

test/engine_thymol_tests.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
3+
var thymol = require('../lib/engine_thymol.js');
4+
5+
// Some basic tests using NodeUnit. Thymol itself is not tested.
6+
exports.thymolTests = {
7+
8+
renderPattern: function renderPattern(test) {
9+
const patternContent = 'Hello, <th:block th:text="${whom}">World</th:block>!';
10+
const pattern = {
11+
name: "Test find partials",
12+
template: patternContent,
13+
extendedTemplate: patternContent
14+
};
15+
test.equal("Hello, universe!", thymol.renderPattern(pattern, {whom: "universe"}));
16+
test.done();
17+
},
18+
19+
/**
20+
* Return the markup that should be replaced by the partial contents, ignore any surrounding HTML
21+
*/
22+
findPartials: function (test) {
23+
const pattern = {
24+
name: "Test find partials",
25+
template: 'Ignored <div th:replace="atoms-basic-button">Foo</div> Ignred'
26+
};
27+
test.deepEqual(['<div th:replace="atoms-basic-button">Foo</div>'], thymol.findPartials(pattern));
28+
test.done();
29+
},
30+
31+
// Done by thymeleaf using th:style and th:with. This function is too mustachified in Pattern Lab.
32+
findPartialsWithStyleModifiers: function (test) {
33+
test.deepEqual([], thymol.findPartialsWithStyleModifiers());
34+
test.done();
35+
},
36+
37+
// Use th:replace and th:include
38+
findPartialsWithPatternParameters: function (test) {
39+
test.deepEqual([], thymol.findPartialsWithPatternParameters());
40+
test.done();
41+
},
42+
43+
// not supported with thymol, list_item_hunter is too mustachified.
44+
findListItems: function (test) {
45+
test.deepEqual([], thymol.findListItems());
46+
test.done();
47+
},
48+
};

0 commit comments

Comments
 (0)