Skip to content

Commit

Permalink
- Moved Default.register(languageContext); as noticed by Willis Black…
Browse files Browse the repository at this point in the history
…burn
  • Loading branch information
soywiz committed Mar 6, 2013
1 parent 01da342 commit 165055c
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 124 deletions.
244 changes: 122 additions & 122 deletions lib/atpl.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
var TemplateParser = require('./parser/TemplateParser')
var TemplateProvider = require('./TemplateProvider')
var LanguageContext = require('./LanguageContext')
var Default = require('./lang/Default')
var fs = require('fs')
var FileSystemTemplateProvider = TemplateProvider.FileSystemTemplateProvider;
var registryTemplateParser = {
};
var languageContext = new LanguageContext.LanguageContext();
function internalCompile(options, absolutePath) {
if (typeof absolutePath === "undefined") { absolutePath = false; }
if(options.root === undefined) {
options.root = '';
}
if(registryTemplateParser[options.root] === undefined) {
Default.register(languageContext);
var templateParser = new TemplateParser.TemplateParser(new FileSystemTemplateProvider(options.root), languageContext);
registryTemplateParser[options.root] = templateParser;
}
return function (params) {
var cache = options.cache;
if(params && params.settings) {
if(options.cache === undefined) {
cache = params.settings['view cache'];
}
}
return languageContext.templateConfig.setCacheTemporal(cache, function () {
var templateParser = registryTemplateParser[options.root];
if(options.path === undefined) {
if(options.content === undefined) {
throw (new Error("No content or path"));
}
return templateParser.compileAndRenderStringToString(options.content, params);
} else {
if(absolutePath) {
return templateParser.compileAndRenderToString(options.path, params);
} else {
if(options.path.indexOf(options.root) !== 0) {
throw (new Error("Path '" + options.path + "' not inside root '" + options.root + "'"));
}
return templateParser.compileAndRenderToString(options.path.substr(options.root.length), params);
}
}
});
};
}
var rootPathCache = {
};
function internalRender(filename, options, absolutePath) {
if (typeof absolutePath === "undefined") { absolutePath = false; }
options = options || {
};
if(options.settings === undefined) {
options.settings = {
};
}
var rootPath = options.settings['views'] || '.';
if(rootPathCache[rootPath] === undefined) {
rootPathCache[rootPath] = fs.realpathSync(rootPath);
}
var params = {
path: filename,
root: rootPathCache[rootPath],
cache: options.cache,
content: options.content
};
return internalCompile(params, absolutePath)(options);
}
function internalCompileString(content, options) {
return function (params) {
params.content = content;
params.path = params.filename;
return internalRender(params.filename, params, true);
};
}
exports.internalCompileString = internalCompileString;
function express2Compile(templateString, options) {
options = options || {
};
if(options.settings === undefined) {
options.settings = {
};
}
return internalCompileString(templateString, options.settings['atpl options']);
}
exports.express2Compile = express2Compile;
function express3RenderFile(filename, options, callback) {
if('function' == typeof options) {
callback = options;
options = {
};
}
var err = null;
var result = null;
try {
result = internalRender(filename, options);
} catch (err) {
}
return callback(null, result);
}
function registerExtension(items) {
return languageContext.registerExtension(items);
}
exports.registerExtension = registerExtension;
function registerTags(items) {
return languageContext.registerTags(items);
}
exports.registerTags = registerTags;
function registerFunctions(items) {
return languageContext.registerFunctions(items);
}
exports.registerFunctions = registerFunctions;
function registerFilters(items) {
return languageContext.registerFilters(items);
}
exports.registerFilters = registerFilters;
function registerTests(items) {
return languageContext.registerTests(items);
}
exports.registerTests = registerTests;
exports.compile = express2Compile;
exports.__express = express3RenderFile;
var TemplateParser = require('./parser/TemplateParser')
var TemplateProvider = require('./TemplateProvider')
var LanguageContext = require('./LanguageContext')
var Default = require('./lang/Default')
var fs = require('fs')
var FileSystemTemplateProvider = TemplateProvider.FileSystemTemplateProvider;
var registryTemplateParser = {
};
var languageContext = new LanguageContext.LanguageContext();
Default.register(languageContext);
function internalCompile(options, absolutePath) {
if (typeof absolutePath === "undefined") { absolutePath = false; }
if(options.root === undefined) {
options.root = '';
}
if(registryTemplateParser[options.root] === undefined) {
var templateParser = new TemplateParser.TemplateParser(new FileSystemTemplateProvider(options.root), languageContext);
registryTemplateParser[options.root] = templateParser;
}
return function (params) {
var cache = options.cache;
if(params && params.settings) {
if(options.cache === undefined) {
cache = params.settings['view cache'];
}
}
return languageContext.templateConfig.setCacheTemporal(cache, function () {
var templateParser = registryTemplateParser[options.root];
if(options.path === undefined) {
if(options.content === undefined) {
throw (new Error("No content or path"));
}
return templateParser.compileAndRenderStringToString(options.content, params);
} else {
if(absolutePath) {
return templateParser.compileAndRenderToString(options.path, params);
} else {
if(options.path.indexOf(options.root) !== 0) {
throw (new Error("Path '" + options.path + "' not inside root '" + options.root + "'"));
}
return templateParser.compileAndRenderToString(options.path.substr(options.root.length), params);
}
}
});
};
}
var rootPathCache = {
};
function internalRender(filename, options, absolutePath) {
if (typeof absolutePath === "undefined") { absolutePath = false; }
options = options || {
};
if(options.settings === undefined) {
options.settings = {
};
}
var rootPath = options.settings['views'] || '.';
if(rootPathCache[rootPath] === undefined) {
rootPathCache[rootPath] = fs.realpathSync(rootPath);
}
var params = {
path: filename,
root: rootPathCache[rootPath],
cache: options.cache,
content: options.content
};
return internalCompile(params, absolutePath)(options);
}
function internalCompileString(content, options) {
return function (params) {
params.content = content;
params.path = params.filename;
return internalRender(params.filename, params, true);
};
}
exports.internalCompileString = internalCompileString;
function express2Compile(templateString, options) {
options = options || {
};
if(options.settings === undefined) {
options.settings = {
};
}
return internalCompileString(templateString, options.settings['atpl options']);
}
exports.express2Compile = express2Compile;
function express3RenderFile(filename, options, callback) {
if('function' == typeof options) {
callback = options;
options = {
};
}
var err = null;
var result = null;
try {
result = internalRender(filename, options);
} catch (err) {
}
return callback(null, result);
}
function registerExtension(items) {
return languageContext.registerExtension(items);
}
exports.registerExtension = registerExtension;
function registerTags(items) {
return languageContext.registerTags(items);
}
exports.registerTags = registerTags;
function registerFunctions(items) {
return languageContext.registerFunctions(items);
}
exports.registerFunctions = registerFunctions;
function registerFilters(items) {
return languageContext.registerFilters(items);
}
exports.registerFilters = registerFilters;
function registerTests(items) {
return languageContext.registerTests(items);
}
exports.registerTests = registerTests;
exports.compile = express2Compile;
exports.__express = express3RenderFile;
3 changes: 1 addition & 2 deletions lib/atpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ export interface IOptionsExpress {
}

var languageContext = new LanguageContext.LanguageContext();
Default.register(languageContext);

function internalCompile(options: IOptions, absolutePath = false) {
if (options.root === undefined) options.root = '';

// options.cache

if (registryTemplateParser[options.root] === undefined) {
Default.register(languageContext);

var templateParser = new TemplateParser.TemplateParser(
new FileSystemTemplateProvider(options.root),
languageContext
Expand Down

0 comments on commit 165055c

Please sign in to comment.