Made core static

This commit is contained in:
nicco
2018-02-13 22:13:34 +01:00
parent 483b4d625c
commit 5e527b45d1
2 changed files with 73 additions and 30 deletions

43
dist/cometa.js vendored
View File

@@ -5,33 +5,30 @@ const util = require("./util");
const parser = require("./parser");
const compiler = require("./compiler");
const options_1 = require("./options");
module.exports = class {
module.exports = class Cometa {
constructor(opt, rexp) {
this.options = options_1.options;
this.expressions = options_1.re;
this._express = this.renderFile;
this.cache = new Map();
this.options = Object.assign(this.options, opt);
this.expressions = Object.assign(this.expressions, rexp);
this.options = Object.assign(options_1.options, opt);
this.expressions = Object.assign(options_1.re, rexp);
if (module.parent === null)
throw new Error('Not imported');
this.options.views = path.join(path.dirname(module.parent.filename), this.options.views);
}
renderFile(file, data, callback) {
console.log('Options', this.options);
static exec(file, data, callback, env) {
util.readFile(file).then(html => {
console.log('Options', this.options);
if (html === undefined) {
callback(`No template found: ${file}`, '');
return;
}
util.checksum(html, true).then(hash => {
if (this.options.caching && !this.cache.get(html))
this.cache.set(html, {
template: compiler.process(html, this.options, this.expressions),
if (env.options.caching && !env.cache.get(html)) {
process.stdout.write(`Compiling: ${hash}\n`);
env.cache.set(html, {
template: compiler.process(html, env.options, env.expressions),
time: Date.now()
});
const compiled = this.cache.get(html);
}
const compiled = env.cache.get(html);
if (compiled)
callback(null, parser.computeParts(compiled.template, data));
else
@@ -39,8 +36,26 @@ module.exports = class {
});
});
}
renderTemplate(template_name, data, callback) {
render(template_name, data, callback) {
const template_path = path.join(this.options.views, `${template_name}.${this.options.extension}`);
this.renderFile(template_path, data, callback);
}
renderFile(template_path, data, callback) {
Cometa.exec(template_path, data, callback, {
options: this.options,
expressions: this.expressions,
cache: this.cache
});
}
static __express(file, data, callback) {
if (Cometa.permCache === undefined) {
process.stdout.write('Initializing cache map\n');
Cometa.permCache = new Map();
}
Cometa.exec(file, data, callback, {
options: options_1.options,
expressions: options_1.re,
cache: Cometa.permCache,
});
}
};