var etpl = require('./main');
var path = require('path');
var fs = require('fs');

etpl.Engine.prototype.loadFromFile = function (file) {
    var load = this.load.bind(this);
    var targets = this.targets;

    /* jshint -W054 */
    var renderer = new Function('return ""');
    /* jshint +W054 */

    var encoding = this.options.encoding || 'UTF-8';
    var source = fs.readFileSync(file, encoding);

    var parseInfo = etpl.util.parseSource(source, this);
    var targetNames = parseInfo.targets;


    parseInfo.deps.forEach(function (dep) {
        if (!targets[dep]) {
            load(dep);
        }
    });

    if (targetNames.length) {
        renderer = targets[targetNames[0]].getRenderer();
    }

    return renderer;
};

etpl.Engine.prototype.load = function (targetName) {
    if (this.targets[targetName]) {
        return this.targets[targetName].getRenderer();
    }

    return this.loadFromFile(resolveTargetPath(targetName, this));
};



function resolveTargetPath(targetName, engine) {
    var dir = engine.options.dir || process.pwd();
    var ext = engine.options.extname || '.etpl';

    return path.resolve(dir, targetName) + ext;
}

module.exports = exports = etpl;

@@ -1,86 +1,389 @@
-var etpl = require('./main');%0Avar path = require('path');%0Avar fs = require('fs');%0A
+/**%0A * ETPL (Enterprise Template)%0A * Copyright 2016 Baidu Inc. All rights reserved.%0A *%0A * @file node%E7%8E%AF%E5%A2%83%E6%94%AF%E6%8C%81%E6%A8%A1%E5%9D%97%EF%BC%8C%E4%B8%BB%E8%A6%81%E6%8F%90%E4%BE%9Bimport%E5%92%8Cmaster%E7%9A%84%E4%BE%9D%E8%B5%96%E6%96%87%E4%BB%B6%E8%87%AA%E5%8A%A8%E5%8A%A0%E8%BD%BD%E5%8A%9F%E8%83%BD%0A * @author errorrik(errorrik@gmail.com)%0A */%0A%0A%0A/* eslint-env node */%0A%0Avar etpl = require('./main');%0Avar path = require('path');%0Avar fs = require('fs');%0A%0A/**%0A * %E5%8A%A0%E8%BD%BD%E6%A8%A1%E6%9D%BF%E6%96%87%E4%BB%B6%0A *%0A * @param %7Bstring%7D file %E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84%0A * @return %7Bfunction(Object):string%7D renderer%E5%87%BD%E6%95%B0%0A */
 %0Aetp
@@ -1036,16 +1036,126 @@
 er;%0A%7D;%0A%0A
+/**%0A * %E5%8A%A0%E8%BD%BDtarget%0A *%0A * @param %7Bstring%7D targetName target%E5%90%8D%E7%A7%B0%0A * @return %7Bfunction(Object):string%7D renderer%E5%87%BD%E6%95%B0%0A */%0A
 etpl.Eng
@@ -1369,16 +1369,149 @@
 );%0A%7D;%0A%0A%0A
+/**%0A * %E8%8E%B7%E5%8F%96 target %E5%AF%B9%E5%BA%94%E7%9A%84%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84%0A *%0A * @inner%0A * @param %7Bstring%7D targetName target%E5%90%8D%E7%A7%B0%0A * @param %7BEngine%7D engine etpl%E5%BC%95%E6%93%8E%0A * @return %7Bstring%7D%0A */
 %0Afunctio
@@ -1704,16 +1704,17 @@
 ext;%0A%7D%0A%0A
+%0A
 module.e

add comments