<commit_before>'use strict';

module.exports = function load(templatePath, templateSrc, options) {
    if (arguments.length === 1) {
        return doLoad(templatePath);
    } else if (arguments.length === 2) {
        // see if second argument is templateSrc (a String)
        // or options (an Object)
        var lastArg = arguments[arguments.length - 1];
        if (typeof lastArg === 'string') {
            return doLoad(templatePath, templateSrc);
        } else {
            var finalOptions = templateSrc;
            return doLoad(templatePath, null, finalOptions);
        }
    } else if (arguments.length === 3) {
        // assume function called according to function signature
        return doLoad(templatePath, templateSrc, options);
    } else {
        throw new Error('Illegal arguments');
    }
};

var nodePath = require('path');
var fs = require('fs');
var Module = require('module').Module;
var compilerPath = nodePath.join(__dirname, '../../compiler');
var markoCompiler = require(compilerPath);
var cwd = process.cwd();
var fsOptions = {encoding: 'utf8'};

function loadSource(templatePath, compiledSrc) {
    var templateModulePath = templatePath + '.js';

    // Short-circuit loading if the template has already been cached in the Node.js require cache
    var cached = require.cache[templateModulePath];
    if (cached) {
        return cached.exports;
    }

    var templateModule = new Module(templateModulePath, module);
    templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templateModulePath));
    templateModule.filename = templateModulePath;

    Module._cache[templateModulePath] = templateModule;

    templateModule._compile(
        compiledSrc,
        templateModulePath);

    return templateModule.exports;
}

function getLoadedTemplate(path) {
    var cached = require.cache[path];
    return cached && cached.exports.render ? cached.exports : undefined;
}

function loadFile(templatePath, options) {
    options = Object.assign({}, markoCompiler.defaultOptions, options);

    var targetFile = templatePath + '.js';

    // Short-circuit loading if the template has already been cached in the Node.js require cache
    var cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath);
    if (cachedTemplate) {
        return cachedTemplate;
    }

    // Just in case the the path wasn't a fully resolved file system path...
    templatePath = nodePath.resolve(cwd, templatePath);
    targetFile = templatePath + '.js';

    // Check the require cache again after fully resolving the path
    cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath);
    if (cachedTemplate) {
        return cachedTemplate;
    }

    // If the `assumeUpToDate` option is true then we just assume that the compiled template on disk is up-to-date
    // if it exists
    if (options.assumeUpToDate) {
        if (fs.existsSync(targetFile)) {
            return require(targetFile);
        }
    }

    var isUpToDate = markoCompiler.checkUpToDate(targetFile);

    if (isUpToDate) {
        return require(targetFile);
    }

	var compiledSrc = markoCompiler.compileFile(templatePath, options);

    // console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);

    var filename = nodePath.basename(targetFile);
    var targetDir = nodePath.dirname(targetFile);
    var tempFile = nodePath.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename);
    fs.writeFileSync(tempFile, compiledSrc, fsOptions);
    fs.renameSync(tempFile, targetFile);

    return require(targetFile);
}

function createRenderProxy(template) {
    return function(data, out) {
        template._(data, out);
    };
}

function doLoad(templatePath, templateSrc, options) {
    options = Object.assign({}, markoCompiler.defaultOptions, options);

    var template;
    if (typeof templatePath.render === 'function') {
        template = templatePath;
    } else {
        var writeToDisk = options.writeToDisk;


        // If the template source is provided then we can compile the string
        // in memory and there is no need to read template file from disk or
        // write compiled code to disk.
        //
        // If writeToDisk is false then there will be no up-to-date check
        // since compiled source won't be written to disk.
        if ((templateSrc != null) || (writeToDisk === false)) {
            // Don't write the compiled template to disk. Instead, load it
            // directly from the compiled source using the internals of the
            // Node.js module loading system.
            if (templateSrc == null) {
                templateSrc = fs.readFileSync(templatePath, fsOptions);
            }

        	var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options);

            if (writeToDisk === true) {
                var targetFile = templatePath + '.js';
                fs.writeFileSync(targetFile, compiledSrc, fsOptions);
            }

            template = loadSource(templatePath, compiledSrc);
        } else {
            template = loadFile(templatePath, options);
        }
    }

    if (options.buffer === false) {
        var Template = template.constructor;

        template = new Template(
            template.path,
            createRenderProxy(template),
            options);
    }

    return template;
}<commit_msg>Simplified server-side template loading logic<commit_after>'use strict';

module.exports = function load(templatePath, templateSrc, options) {
    if (arguments.length === 1) {
        return doLoad(templatePath);
    } else if (arguments.length === 2) {
        // see if second argument is templateSrc (a String)
        // or options (an Object)
        var lastArg = arguments[arguments.length - 1];
        if (typeof lastArg === 'string') {
            return doLoad(templatePath, templateSrc);
        } else {
            var finalOptions = templateSrc;
            return doLoad(templatePath, null, finalOptions);
        }
    } else if (arguments.length === 3) {
        // assume function called according to function signature
        return doLoad(templatePath, templateSrc, options);
    } else {
        throw new Error('Illegal arguments');
    }
};

var nodePath = require('path');
var fs = require('fs');
var Module = require('module').Module;
var compilerPath = nodePath.join(__dirname, '../../compiler');
var markoCompiler = require(compilerPath);
var cwd = process.cwd();
var fsOptions = {encoding: 'utf8'};

function loadSource(templatePath, compiledSrc) {
    templatePath += '.js';
    
    // Short-circuit loading if the template has already been cached in the Node.js require cache
    var cached = require.cache[templatePath];
    if (cached) {
        return cached.exports;
    }

    var templateModule = new Module(templatePath, module);
    templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templatePath));
    templateModule.filename = templatePath;

    Module._cache[templatePath] = templateModule;

    templateModule._compile(
        compiledSrc,
        templatePath);

    return templateModule.exports;
}

function getCachedTemplate(path) {
    var cached = require.cache[path];
    return cached && cached.exports.render ? cached.exports : undefined;
}
/**
 * This helper function will check the Node.js require cache for the previous
 * loaded template and it will also check the disk for the compiled template
 * if `options.assumeUpToDate` is true

 * @param  {String} templatePath The fully resolved path to the template
 * @param  {Object} options      The options for the template
 * @return {Template}            The loaded template or undefined
 */
function getPreviousTemplate(templatePath, options) {
    /*
    The require.cache is search in the following order:
    1) /path/to/my-template.js
    2) /path/to/my-template.marko.js
    3) /path/to/my-template.marko
     *
    If the template is not found in require.cache and `assumeUpToDate` is true
    then we will check the disk for the precompiled templates in the following
    order:
    1) /path/to/my-template.js
    2) /path/to/my-template.marko.js
    */
    var ext = nodePath.extname(templatePath);
    var targetFilePrecompiled = templatePath.slice(0 - ext.length) + '.js';
    var targetFileDebug = templatePath + '.js';

    // Short-circuit loading if the template has already been cached in the Node.js require cache
    var cachedTemplate =
        getCachedTemplate(targetFilePrecompiled) ||
        getCachedTemplate(targetFileDebug) ||
        getCachedTemplate(templatePath);

    if (cachedTemplate) {
        return cachedTemplate;
    }

    // Just in case the the path wasn't a fully resolved file system path...
    templatePath = nodePath.resolve(cwd, templatePath);

    if (options.assumeUpToDate) {
        if (fs.existsSync(targetFilePrecompiled)) {
            return require(targetFilePrecompiled);
        }

        if (fs.existsSync(targetFileDebug)) {
            return require(targetFileDebug);
        }
    }

    return undefined;
}

function createRenderProxy(template) {
    return function(data, out) {
        template._(data, out);
    };
}

function doLoad(templatePath, templateSrc, options) {
    options = Object.assign({}, markoCompiler.defaultOptions, options);

    var template;
    if (typeof templatePath.render === 'function') {
        template = templatePath;
    } else {
        templatePath = nodePath.resolve(cwd, templatePath);

        template = getPreviousTemplate(templatePath, options);
        if (!template) {
            var writeToDisk = options.writeToDisk;

            if (templateSrc == null) {
                templateSrc = fs.readFileSync(templatePath, fsOptions);
            }

            var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options);

            if (writeToDisk === true) {
                var targetFile = templatePath + '.js';
                fs.writeFileSync(targetFile, compiledSrc, fsOptions);
            }

            template = loadSource(templatePath, compiledSrc);
        }
    }

    if (options.buffer === false) {
        var Template = template.constructor;

        template = new Template(
            template.path,
            createRenderProxy(template),
            options);
    }

    return template;
}
{'fraction_non_alphanumeric': 0.10115718418514948, 'fraction_numerical': 0.0015429122468659595, 'mean_word_length': 2.744043321299639, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991047', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2683, 'n_words': 807}