<commit_before>/*
 * Copyright (c) 2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE.txt file for terms.
 */

/*jslint nomen:true, node:true */

"use strict";

var core = require('./core'),
    libpath = require('path');

module.exports = {

    describe: {
        summary: 'Compile handlebars templates to yui modules',
        extensions: ['hb', 'handlebars', 'hbs'],
        partialDir: 'partials'
    },

    fileUpdated: function (evt, api) {

        var self = this,
            options = this.describe.options  || {},
            file = evt.file,
            source_path = file.fullPath,
            bundleName = file.bundleName,
            name,
            destination_path;

        // building the name of the module based on:
        // * bundle name, partial or template denomination, and filename
        name = [
            bundleName,
            core.isPartial(source_path, this.describe.partialDir) ? 'partial' : 'template',
            libpath.basename(file.relativePath, '.' + file.ext)
        ].join('-');
        // destination is not very relevant, just dropping the file in buildDirectory
        destination_path = name + '.js';

        return api.promise(function (fulfill, reject) {

            var compiled,
                partials;

            try {
                partials = core.partials(source_path);
                compiled = core.compile(source_path, options);
            } catch (e) {
                reject(e);
            }

            // trying to write the destination file which will fulfill or reject the initial promise
            api.writeFileInBundle(bundleName, destination_path,
                self._wrapAsYUI(bundleName, name, compiled, partials))
                .then(function () {
                    // provisioning the module to be used on the server side automatically
                    evt.bundle.useServerModules = evt.bundle.useServerModules || [];
                    evt.bundle.useServerModules.push(name);
                    // we are now ready to roll
                    fulfill();
                }, reject);

        });

    },

    _wrapAsYUI: function (bundleName, name, compiled, partials) {

        // base dependency
        var dependencies = ["handlebars-base"],
            partialPrefix = bundleName + '-partial-';

        // each partial should be provisioned thru another yui module
        // and the name of the partial should translate into a yui module
        // to become a dependency
        partials = partials || [];
        partials.forEach(function (name) {
            // adding prefix to each partial
            dependencies.push(partialPrefix + name);
        });

        return [
            'YUI.add("' + name + '",function(Y, NAME){',
            '   var fn = Y.Template.Handlebars.revive(' + compiled + '),',
            '       cache = Y.Template._cache = Y.Template._cache || {},',
            '       partials = {};',
            '',
            '   Y.Array.each(' + JSON.stringify(partials) + ', function (name) {',
            '       cache["' + partialPrefix + '" + name] && (partials[name] = cache["' + partialPrefix + '" + name]);',
            '   });',
            '',
            '   cache[NAME] = function (data) {',
            '       return fn(data, {',
            '           partials: partials',
            '       });',
            '   };',
            '}, "", {requires: ' + JSON.stringify(dependencies) + '});'<commit_msg>consolidating the plugin to work with express-yui new rules for templates and partials.<commit_after>/*
 * Copyright (c) 2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE.txt file for terms.
 */

/*jslint nomen:true, node:true */

"use strict";

var core = require('./core'),
    libpath = require('path');

module.exports = {

    describe: {
        summary: 'Compile handlebars templates to yui modules',
        extensions: ['hb', 'handlebars', 'hbs'],
        partialDir: 'partials'
    },

    fileUpdated: function (evt, api) {

        var self = this,
            options = this.describe.options  || {},
            file = evt.file,
            source_path = file.fullPath,
            bundleName = file.bundleName,
            treatItAsPartial = core.isPartial(source_path, this.describe.partialDir),
            templateName = libpath.basename(file.relativePath, '.' + file.ext),
            destination_path = (treatItAsPartial ? 'partial-' : 'template-') + templateName + '.js',
            moduleName = bundleName + (treatItAsPartial ? '-partials-' : '-templates-') + templateName;

        return api.promise(function (fulfill, reject) {

            var compiled,
                partials;

            try {
                partials = core.partials(source_path);
                compiled = core.compile(source_path, options);
            } catch (e) {
                reject(e);
            }

            // trying to write the destination file which will fulfill or reject the initial promise
            api.writeFileInBundle(bundleName, destination_path,
                self._wrapAsYUI(bundleName, templateName, moduleName, treatItAsPartial, compiled, partials))
                .then(function () {
                    // provisioning the module to be used on the server side automatically
                    evt.bundle.useServerModules = evt.bundle.useServerModules || [];
                    evt.bundle.useServerModules.push(moduleName);
                    // we are now ready to roll
                    fulfill();
                }, reject);

        });

    },

    _wrapAsYUI: function (bundleName, templateName, moduleName, isPartial, compiled, partials) {

        // base dependency
        var dependencies = ["handlebars-base"];

        // each partial should be provisioned thru another yui module
        // and the name of the partial should translate into a yui module
        // to become a dependency
        partials = partials || [];
        partials.forEach(function (name) {
            // adding prefix to each partial
            dependencies.push(bundleName + '-partials-' + name);
        });

        return [
            'YUI.add("' + moduleName + '",function(Y, NAME){',
            '   var fn = Y.Template.Handlebars.revive(' + compiled + '),',
            '       cache = Y.Template._cache = Y.Template._cache || {},',
            '       partials = {};',
            '',
            '   Y.Array.each(' + JSON.stringify(partials) + ', function (name) {',
            '       if (cache["' + bundleName + '/partials/' + '" + name]) {',
            '           partials[name] = cache["' + bundleName + '/partials/' + '" + name];',
            '       }',
            '   });',
            '',
            '   cache["' + bundleName + '/' + (isPartial ? 'partials/' : '') + templateName + '"] = function (data) {',
            '       return fn(data, {',
            '           partials: partials',
            '       });',
            '   };',
            '}, "", {requires: ' + JSON.stringify(dependencies) + '});'
{'fraction_non_alphanumeric': 0.12646402677077523, 'fraction_numerical': 0.0011154489682097045, 'mean_word_length': 1.7419724770642202, 'pattern_counts': {'":': 0, '<': 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': '2040896', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1834, 'n_words': 490}