Skip to content

Commit

Permalink
feat: switch engine-twig to use twing rather than node-twig
Browse files Browse the repository at this point in the history
  • Loading branch information
ringods committed Mar 19, 2020
1 parent 8250fe8 commit daca95c
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 64 deletions.
159 changes: 132 additions & 27 deletions packages/engine-twig/lib/engine_twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,126 @@

const fs = require('fs-extra');
const path = require('path');
const process = require('process');
const Twig = require('node-twig');
const twig = Twig.renderFile;
const {
TwingEnvironment,
TwingLoaderFilesystem,
TwingLoaderChain,
TwingSource,
} = require('twing');

class TwingLoaderPatternLab {
patterns = new Map();

constuctor() {}

registerPartial(pattern) {
if (pattern.patternPartial) {
this.patterns.set(pattern.patternPartial, pattern);
}
}

/**
* Returns the source context for a given template logical name.
*
* @param {string} name The template logical name
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<TwingSource>}
*
* @throws TwingErrorLoader When name is not found
*/
getSourceContext(name, from) {
var pattern = this.patterns.get(name);
return Promise.resolve(
new TwingSource(pattern.extendedTemplate, name, pattern.relPath)
);
}

/**
* Gets the cache key to use for the cache for a given template name.
*
* @param {string} name The name of the template to load
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<string>} The cache key
*
* @throws TwingErrorLoader When name is not found
*/
getCacheKey(name, from) {
return Promise.resolve(name);
}

/**
* Returns true if the template is still fresh.
*l
* @param {string} name The template name
* @param {number} time Timestamp of the last modification time of the cached template
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<boolean>} true if the template is fresh, false otherwise
*
* @throws TwingErrorLoader When name is not found
*/
isFresh(name, time, from) {
return Promise.resolve(this.patterns.has(name) ? true : false);
}

/**
* Check if we have the source code of a template, given its name.
*
* @param {string} name The name of the template to check if we can load
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<boolean>} If the template source code is handled by this loader or not
*/
exists(name, from) {
return Promise.resolve(this.patterns.has(name) ? true : false);
}

/**
* Resolve the path of a template, given its name, whatever it means in the context of the loader.
*
* @param {string} name The name of the template to resolve
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<string>} The resolved path of the template
*/
resolve(name, from) {
pattern = this.patterns.get(name);
return null;
}
}

const fileSystemLoader = new TwingLoaderFilesystem();
const patternLabLoader = new TwingLoaderPatternLab();
const chainLoader = new TwingLoaderChain([fileSystemLoader, patternLabLoader]);
const twing = new TwingEnvironment(chainLoader);
var metaPath;

var engine_twig = {
engine: Twig,
engine: twing,
engineName: 'twig',
engineFileExtension: '.twig',

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

// regexes, stored here so they're only compiled once
findPartialsRE: /{%\s*(?:extends|include|embed)\s+('[^']+'|"[^"]+").*?%}/g,
findPartialKeyRE: /"((?:\\.|[^"\\])*)"/,
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g, // TODO

// render it
renderPattern: function renderPattern(pattern, data) {
return Promise.resolve(
twig(
pattern.relPath,
{
root: path.relative(
__dirname,
path.resolve(process.cwd(), 'source', '_patterns')
),
context: data,
},
(error, template) => {
if (error) {
console.log(error);
}
console.log(template);
return template;
}
)
renderPattern: function renderPattern(pattern, data, partials) {
var patternPath = pattern.relPath;
if (patternPath.lastIndexOf(metaPath) === 0) {
patternPath = patternPath.substring(metaPath.length + 1);
}
return Promise.resolve(twing.render(patternPath, data));
},

registerPartial: function registerPartial(pattern) {
console.log(
`registerPartial(${pattern.name} - ${pattern.patternPartial} - ${pattern.patternPath} - ${pattern.relPath})`
);
patternLabLoader.registerPartial(pattern);
},

// find and return any {% include 'template-name' %} within pattern
Expand Down Expand Up @@ -88,7 +170,6 @@ var engine_twig = {
// given a pattern, and a partial string, tease out the "pattern key" and
// return it.
findPartial: function(partialString) {
//var partialKey = partialString.replace(this.findPartialsRE, '$1');
var partial = partialString.match(this.findPartialKeyRE)[0];
partial = partial.replace(/"/g, '');

Expand Down Expand Up @@ -121,6 +202,30 @@ var engine_twig = {
this.spawnFile(config, '_00-head.twig');
this.spawnFile(config, '_01-foot.twig');
},

/**
* Accept a Pattern Lab config object from the core and use the settings to
* load helpers.
*
* @param {object} config - the global config object from core
*/
usePatternLabConfig: function(config) {
metaPath = path.resolve(config.paths.source.meta);
// Global paths
fileSystemLoader.addPath(config.paths.source.meta);
fileSystemLoader.addPath(config.paths.source.patterns);
// Namespaced paths
if (
config['engines'] &&
config['engines']['twig'] &&
config['engines']['twig']['namespaces']
) {
var namespaces = config['engines']['twig']['namespaces'];
Object.keys(namespaces).forEach(function(key, index) {
fileSystemLoader.addPath(namespaces[key], key);
});
}
},
};

module.exports = engine_twig;
2 changes: 1 addition & 1 deletion packages/engine-twig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/engine_twig.js",
"dependencies": {
"fs-extra": "0.30.0",
"node-twig": "1.1.0"
"twing": "4.0.3"
},
"keywords": [
"Pattern Lab",
Expand Down
Loading

0 comments on commit daca95c

Please sign in to comment.