<commit_before>/*
    This component is designed to feed template content stored on the server to the client-side components included
    with this library.

    This component requires the full path to all template content.  That path is expected to point to a directory
    that follows the handlebars conventions, and stores template content in subdirectories based on the type of template
    (`layouts`, `pages`, and `partials`).  Any content found in these subdirectories will be read and stored in the
    `templates` member object using the template directory name and the filename (minus the handlebars suffix).

    Say that we have a views directory that contains a `pages` subdirectory, and that the `pages` subdirectory contains
    a single template, called `myTemplate.handlebars`.  When `loadTemplates` finishes running, the `templates` member
    object should look like:

     {
       pages: {
         myTemplate: "..." // template content omitted for brevity
       }
     }

     With the data this router supplies, the handlebars client has what it needs to load partials and handle updating
     markup on the client side.
 */

"use strict";
var fluid  = fluid || require("infusion");
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.express.hb.inline");
var fs     = require("fs");
var path   = require("path");

fluid.registerNamespace("gpii.express.hb.inline.request");
gpii.express.hb.inline.request.sendResponse = function (that) {
    if (that.options.templates) {
        gpii.express.handler.sendResponse(that, 200, { ok: true, templates: that.options.templates });
    }
    else {
        gpii.express.handler.sendResponse(that, 500, { ok: false, message: that.options.messages.noTemplates});
    }
};

fluid.defaults("gpii.express.hb.inline.request", {
    gradeNames: ["gpii.express.requestAware"],
    templates: "{inline}.templates",
    messages: {
        noTemplates: "No templates were found."
    },
    invokers: {
        "handleRequest": {
            funcName: "gpii.express.hb.inline.request.sendResponse",
            args:     ["{that}"]
        }
    }
});

gpii.express.hb.inline.loadTemplates =  function (that, views) {
    var viewDirs = fluid.makeArray(views);
    fluid.each(viewDirs, function (viewDir) {
        // Start with each "views" directory and work our way down
        var dirContents = fs.readdirSync(viewDir);
        dirContents.forEach(function (entry) {
            var subDirPath = path.resolve(viewDir, entry);
            var stats = fs.statSync(subDirPath);
            if (stats.isDirectory() && that.options.allowedTemplateDirs.indexOf[entry] !== -1) {
                gpii.express.hb.inline.scanTemplateSubdir(that, entry, subDirPath);
            }
        });
    });

    //that.options.dynamicComponents.requestHandler.options.templates = that.templates;

    that.events.templatesLoaded.fire(that);
};

gpii.express.hb.inline.scanTemplateSubdir = function (that, key, dirPath) {
    var dirContents = fs.readdirSync(dirPath);
    dirContents.forEach(function (entry) {
        var entryPath = path.resolve(dirPath, entry);
        var stats = fs.statSync(entryPath);
        if (stats.isFile()) {
            var matches = that.options.hbsExtensionRegexp.exec(entry);
            if (matches) {
                var templateName = matches[1];
                if (!that.templates[key][templateName]) {
                    var templateContent = fs.readFileSync(entryPath, {encoding: "utf8"});
                    that.templates[key][templateName] = templateContent;<commit_msg>GPII-1494:  Removed stale comment.<commit_after>/*
    This component is designed to feed template content stored on the server to the client-side components included
    with this library.

    This component requires the full path to all template content.  That path is expected to point to a directory
    that follows the handlebars conventions, and stores template content in subdirectories based on the type of template
    (`layouts`, `pages`, and `partials`).  Any content found in these subdirectories will be read and stored in the
    `templates` member object using the template directory name and the filename (minus the handlebars suffix).

    Say that we have a views directory that contains a `pages` subdirectory, and that the `pages` subdirectory contains
    a single template, called `myTemplate.handlebars`.  When `loadTemplates` finishes running, the `templates` member
    object should look like:

     {
       pages: {
         myTemplate: "..." // template content omitted for brevity
       }
     }

     With the data this router supplies, the handlebars client has what it needs to load partials and handle updating
     markup on the client side.
 */

"use strict";
var fluid  = fluid || require("infusion");
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.express.hb.inline");
var fs     = require("fs");
var path   = require("path");

fluid.registerNamespace("gpii.express.hb.inline.request");
gpii.express.hb.inline.request.sendResponse = function (that) {
    if (that.options.templates) {
        gpii.express.handler.sendResponse(that, 200, { ok: true, templates: that.options.templates });
    }
    else {
        gpii.express.handler.sendResponse(that, 500, { ok: false, message: that.options.messages.noTemplates});
    }
};

fluid.defaults("gpii.express.hb.inline.request", {
    gradeNames: ["gpii.express.requestAware"],
    templates: "{inline}.templates",
    messages: {
        noTemplates: "No templates were found."
    },
    invokers: {
        "handleRequest": {
            funcName: "gpii.express.hb.inline.request.sendResponse",
            args:     ["{that}"]
        }
    }
});

gpii.express.hb.inline.loadTemplates =  function (that, views) {
    var viewDirs = fluid.makeArray(views);
    fluid.each(viewDirs, function (viewDir) {
        // Start with each "views" directory and work our way down
        var dirContents = fs.readdirSync(viewDir);
        dirContents.forEach(function (entry) {
            var subDirPath = path.resolve(viewDir, entry);
            var stats = fs.statSync(subDirPath);
            if (stats.isDirectory() && that.options.allowedTemplateDirs.indexOf[entry] !== -1) {
                gpii.express.hb.inline.scanTemplateSubdir(that, entry, subDirPath);
            }
        });
    });

    that.events.templatesLoaded.fire(that);
};

gpii.express.hb.inline.scanTemplateSubdir = function (that, key, dirPath) {
    var dirContents = fs.readdirSync(dirPath);
    dirContents.forEach(function (entry) {
        var entryPath = path.resolve(dirPath, entry);
        var stats = fs.statSync(entryPath);
        if (stats.isFile()) {
            var matches = that.options.hbsExtensionRegexp.exec(entry);
            if (matches) {
                var templateName = matches[1];
                if (!that.templates[key][templateName]) {
                    var templateContent = fs.readFileSync(entryPath, {encoding: "utf8"});
                    that.templates[key][templateName] = templateContent;
{'fraction_non_alphanumeric': 0.10912419036891016, 'fraction_numerical': 0.0030977189524077726, 'mean_word_length': 2.961517010596765, 'pattern_counts': {'":': 2, '<': 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': '6165775', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1936, 'n_words': 568}