Skip to content

Commit

Permalink
Nicer package.json, prepare for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Brennecke committed Jan 27, 2017
1 parent ca3265a commit 209f878
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 50 deletions.
9 changes: 7 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"env": {
"node": true,
"builtin": true
"builtin": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"globals": {},
"rules": {
Expand Down Expand Up @@ -67,7 +72,7 @@
"no-with": 2,
"quotes": [0, "single"],
"radix": 2,
"semi": [0, "never"],
"semi": [1, "always"],
"strict": 0,
"space-before-blocks": 1,
"space-before-function-paren": [1, {
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
## The Thymol engine for Pattern Lab / Node
The Thymol engine for Pattern Lab / Node
========================================

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).

Installing
----------

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

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.
Supported features
------------------

- [x] [Includes](http://patternlab.io/docs/pattern-including.html)
- [x] Lineage
- [x] [Hidden Patterns](http://patternlab.io/docs/pattern-hiding.html)
- [x] [Pseudo-Patterns](http://patternlab.io/docs/pattern-pseudo-patterns.html)
- [x] [Pattern States](http://patternlab.io/docs/pattern-states.html)
- [ ] [Pattern Parameters](http://patternlab.io/docs/pattern-parameters.html) (Accomplished instead using native Thymeleaf features)
- [ ] [Style Modifiers](http://patternlab.io/docs/pattern-stylemodifier.html) (Accomplished instead using native Thymeleaf features)

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.

**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>`.
**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>`.
74 changes: 33 additions & 41 deletions lib/engine_thymol.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
*/

"use strict";
var engine_thymol = (function() {

const domino = require('domino');
const jQuery = require("jquery");

const engine_thymol = (function () {
global.thymol = {};

thymol = require('thymol-node').thymol;
var domino = require('domino');
var jQuery = require("jquery");
var jsdomOptions = {
features: {
FetchExternalResources: false,
ProcessExternalResources: false
}
};
const thymol = require('thymol-node').thymol;

var resetGlobals = function() {
const resetGlobals = function () {
global.thPrefix = undefined;
global.thDataPrefix = undefined;
global.thAllowNullText = undefined;
Expand All @@ -55,7 +51,7 @@ var engine_thymol = (function() {
global.thDebug = undefined;
};

var setDefaults = function() {
const setDefaults = function () {
thymol.thScriptPath = "";
thymol.thDebug = true; // calls alert() if something bad happens
thymol.thDefaultPrefix = "th";
Expand All @@ -77,22 +73,22 @@ var engine_thymol = (function() {
setDefaults();
thymol.thWindow = domino.createWindow("<html></html>");
global.$ = jQuery(thymol.thWindow);
thymol.jqSetup($);
thymol.jqSetup(global.$);
thymol.setup();

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

var renderThymeleaf = function(content, vars, messages) {
var win = domino.createWindow(content);
const renderThymeleaf = function (content, vars, messages) {
const win = domino.createWindow(content);
thymol.thDocument = win.document;
thymol.thWindow = win;
setDefaults();
thymol.thDataThymolLoading = false;

global.$ = jQuery(thymol.thWindow);
thymol.jqSetup($);
thymol.jqSetup(global.$);

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

// must be global
thVars = vars;
thMessages = messages;
thDebug = true;
global.thVars = vars;
global.thMessages = messages;
global.thDebug = true;

var result = {
const result = {
error: null
};

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

thymol.thWindow.alert = alert;

var resultDocument = thymol.execute(thymol.thDocument);
const resultDocument = thymol.execute(thymol.thDocument);

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

var convertDataToThymol = function(data) {
var result = [];
Object.keys(data).forEach(function(k) {
if (k == 'link') return;
var value = data[k];
const convertDataToThymol = function (data) {
const result = [];
Object.keys(data).forEach(function (k) {
if (k === 'link') {
return;
}
const value = data[k];
result.push([k, value]);
});
return result;
Expand All @@ -141,9 +139,8 @@ var engine_thymol = (function() {
engineName: 'thymol',
engineFileExtension: '.html',

//Important! Needed for Twig compilation. Can't resolve paths otherwise.
//Important! Needed for compilation. Can't resolve paths otherwise.
expandPartials: true,
//expandPartials: false,

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

// render it
renderPattern: function renderPattern(pattern, data) {
var thyData = convertDataToThymol(data);
const thyData = convertDataToThymol(data);

const result = renderThymeleaf(pattern.extendedTemplate, thyData);

var ms = new Date().getTime();
var result = renderThymeleaf(pattern.extendedTemplate, thyData);
console.log("Pattern: " + pattern.name + "," + (new Date().getTime() - ms));
if (!!result.error) {
console.error(pattern.name + ': ' + result.error);
throw `${pattern.name}: ${result.error}`;
}
return result.rendered;
},
Expand All @@ -168,22 +164,18 @@ var engine_thymol = (function() {
return pattern.template.match(this.findPartialsRE);
},

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

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

// not supported with thymol, list_item_hunter is too mustachified.
findListItems: function (pattern) {
findListItems: function (/*pattern*/) {
return [];
},

Expand Down
36 changes: 33 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,41 @@
"version": "0.1.0",
"main": "lib/engine_thymol.js",
"dependencies": {
"jquery": "2.2.4",
"jquery": "^3.1.1",
"domino": "1.0.26",
"thymol-node": "2.0.1-pre.2"
"thymol-node": "git+ssh://[email protected]:headissue/thymol-node.git"
},
"devDependencies": {
"eslint": "^3.5.0",
"nodeunit": "^0.10.2"
},
"keywords": [
"Pattern Lab",
"Atomic Web Design",
"Node",
"Grunt",
"Gulp",
"Javascript",
"Thymeleaf",
"Thymol"
],
"repository": {
"type": "git",
"url": "git://github.com/headissue/patternengine-node-thymol.git"
},
"bugs": "https://github.com/headissue/patternengine-node-thymol/issues",
"author": {
"name": "Maximilian Richt"
},
"contributors": [
{
"name": "Tobias Brennecke"
}
],
"license": "MIT",
"scripts": {
"test": "eslint lib/**/*.js && eslint test/**/*.js && nodeunit test/*.js"
},
"devDependencies": {},
"engines": {
"node": ">=4.0"
}
Expand Down
48 changes: 48 additions & 0 deletions test/engine_thymol_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

var thymol = require('../lib/engine_thymol.js');

// Some basic tests using NodeUnit. Thymol itself is not tested.
exports.thymolTests = {

renderPattern: function renderPattern(test) {
const patternContent = 'Hello, <th:block th:text="${whom}">World</th:block>!';
const pattern = {
name: "Test find partials",
template: patternContent,
extendedTemplate: patternContent
};
test.equal("Hello, universe!", thymol.renderPattern(pattern, {whom: "universe"}));
test.done();
},

/**
* Return the markup that should be replaced by the partial contents, ignore any surrounding HTML
*/
findPartials: function (test) {
const pattern = {
name: "Test find partials",
template: 'Ignored <div th:replace="atoms-basic-button">Foo</div> Ignred'
};
test.deepEqual(['<div th:replace="atoms-basic-button">Foo</div>'], thymol.findPartials(pattern));
test.done();
},

// Done by thymeleaf using th:style and th:with. This function is too mustachified in Pattern Lab.
findPartialsWithStyleModifiers: function (test) {
test.deepEqual([], thymol.findPartialsWithStyleModifiers());
test.done();
},

// Use th:replace and th:include
findPartialsWithPatternParameters: function (test) {
test.deepEqual([], thymol.findPartialsWithPatternParameters());
test.done();
},

// not supported with thymol, list_item_hunter is too mustachified.
findListItems: function (test) {
test.deepEqual([], thymol.findListItems());
test.done();
},
};

0 comments on commit 209f878

Please sign in to comment.