<commit_before>/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
var extend = require('raptor-util').extend;
var forEachEntry = require('raptor-util').forEachEntry;
var stringify = require('raptor-json/stringify');

function addHandlerVar(template, renderer) {
    var handlerVars = template._handlerVars || (template._handlerVars = {});
    var handlerVar = handlerVars[renderer];
    if (!handlerVar) {
        handlerVars[renderer] = handlerVar = renderer.replace(/[.\-\/]/g, '_').replace(/^[_]+/g, '');
        template.addStaticVar(handlerVar, 'require(' + stringify(renderer) + ')');
    }
    return handlerVar;
}
function getPropsStr(props, template) {
    var propsArray = [];
    if (props) {
        template.indent(function () {
            forEachEntry(props, function (name, value) {
                if (template.isExpression(value)) {
                    var expressionStr;
                    template.indent(function () {
                        expressionStr = value.expression.toString();
                    });
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + expressionStr);
                } else if (typeof value === 'string' || typeof value === 'object') {
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + stringify(value));
                } else {
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + value);
                }
            });
        });

        
        if (propsArray.length) {
            return '{\n' + propsArray.join(',\n') + '\n' + template.indentStr() + '}';
        } else {
            return '{}';
        }
    } else {
        return '{}';
    }
}
function TagHandlerNode(tag) {
    if (!this.nodeType) {
        TagHandlerNode.$super.call(this);
    }
    this.tag = tag;
    this.dynamicAttributes = null;
    this.inputExpression = null;
}
TagHandlerNode.nodeType = 'element';

TagHandlerNode.convertNode = function (node, tag) {
    extend(node, TagHandlerNode.prototype);
    TagHandlerNode.call(node, tag);
};

TagHandlerNode.prototype = {
    addDynamicAttribute: function (name, value) {
        if (!this.dynamicAttributes) {
            this.dynamicAttributes = {};
        }
        this.dynamicAttributes[name] = value;
    },
    setDynamicAttributesProperty: function(name) {
        this.dynamicAttributesProperty = name;
    },
    setInputExpression: function (expression) {
        this.inputExpression = expression;
    },
    doGenerateCode: function (template) {
        var rendererPath = template.getRequirePath(this.tag.renderer); // Resolve a path to the renderer relative to the directory of the template
        var handlerVar = addHandlerVar(template, rendererPath);
        var tagHelperVar = template.addStaticVar('_tag', '__helpers.t');

        this.tag.forEachImportedVariable(function (importedVariable) {
            this.setProperty(importedVariable.targetProperty, template.makeExpression(importedVariable.expression));
        }, this);
        
        var _this = this;
        var variableNames = [];
        _this.tag.forEachVariable(function (nestedVar) {
            var varName;
            if (nestedVar.nameFromAttribute) {
                var possibleNameAttributes = nestedVar.nameFromAttribute.split(/\s+or\s+|\s*,\s*/i);
                for (var i = 0, len = possibleNameAttributes.length; i < len; i++) {
                    var attrName = possibleNameAttributes[i];
                    var keep = false;
                    if (attrName.endsWith('|keep')) {
                        keep = true;
                        attrName = attrName.slice(0, 0 - '|keep'.length);
                        possibleNameAttributes[i] = attrName;
                    }
                    varName = this.getAttribute(attrName);
                    if (varName) {
                        if (!keep) {
                            this.removeProperty(attrName);
                        }
                        break;
                    }
                }
                if (!varName) {
                    this.addError('Attribute ' + possibleNameAttributes.join(' or ') + ' is required');
                    varName = '_var';    // Let it continue with errors
                }
            } else {
                varName = nestedVar.name;
                if (!varName) {
                    this.addError('Variable name is required');
                    varName = '_var';    // Let it continue with errors
                }
            }
            variableNames.push(varName);
        }, this);

        template.functionCall(tagHelperVar, function () {
            template.code('context,\n').indent(function () {
                template.line(handlerVar + ',').indent();
                if (_this.inputExpression) {
                    template.code(_this.inputExpression);<commit_msg>Allow property values to be functions that resolve to the actual value<commit_after>/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
var extend = require('raptor-util').extend;
var forEachEntry = require('raptor-util').forEachEntry;
var stringify = require('raptor-json/stringify');

function addHandlerVar(template, renderer) {
    var handlerVars = template._handlerVars || (template._handlerVars = {});
    var handlerVar = handlerVars[renderer];
    if (!handlerVar) {
        handlerVars[renderer] = handlerVar = renderer.replace(/[.\-\/]/g, '_').replace(/^[_]+/g, '');
        template.addStaticVar(handlerVar, 'require(' + stringify(renderer) + ')');
    }
    return handlerVar;
}
function getPropsStr(props, template) {
    var propsArray = [];
    if (props) {
        template.indent(function () {
            forEachEntry(props, function (name, value) {
                if (typeof value === 'function') {
                    value = value();
                }

                if (template.isExpression(value)) {
                    var expressionStr;
                    template.indent(function () {
                        expressionStr = value.expression.toString();
                    });
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + expressionStr);
                } else if (typeof value === 'string' || typeof value === 'object') {
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + stringify(value));
                } else {
                    propsArray.push(template.indentStr() + stringify(name) + ': ' + value);
                }
            });
        });

        
        if (propsArray.length) {
            return '{\n' + propsArray.join(',\n') + '\n' + template.indentStr() + '}';
        } else {
            return '{}';
        }
    } else {
        return '{}';
    }
}
function TagHandlerNode(tag) {
    if (!this.nodeType) {
        TagHandlerNode.$super.call(this);
    }
    this.tag = tag;
    this.dynamicAttributes = null;
    this.inputExpression = null;
    this.additionalVars = [];
}
TagHandlerNode.nodeType = 'element';

TagHandlerNode.convertNode = function (node, tag) {
    if (node._TagHandlerNode) {
        return;
    }

    extend(node, TagHandlerNode.prototype);
    TagHandlerNode.call(node, tag);
};

TagHandlerNode.prototype = {

    _TagHandlerNode: true,

    addNestedVariable: function(name) {
        this.additionalVars.push(name);
    },
    addDynamicAttribute: function (name, value) {
        if (!this.dynamicAttributes) {
            this.dynamicAttributes = {};
        }
        this.dynamicAttributes[name] = value;
    },
    setDynamicAttributesProperty: function(name) {
        this.dynamicAttributesProperty = name;
    },
    setInputExpression: function (expression) {
        this.inputExpression = expression;
    },
    doGenerateCode: function (template) {
        var rendererPath = template.getRequirePath(this.tag.renderer); // Resolve a path to the renderer relative to the directory of the template
        var handlerVar = addHandlerVar(template, rendererPath);
        var tagHelperVar = template.addStaticVar('_tag', '__helpers.t');

        this.tag.forEachImportedVariable(function (importedVariable) {
            this.setProperty(importedVariable.targetProperty, template.makeExpression(importedVariable.expression));
        }, this);
        
        var _this = this;
        var variableNames = [];
        _this.tag.forEachVariable(function (nestedVar) {
            var varName;
            if (nestedVar.nameFromAttribute) {
                var possibleNameAttributes = nestedVar.nameFromAttribute.split(/\s+or\s+|\s*,\s*/i);
                for (var i = 0, len = possibleNameAttributes.length; i < len; i++) {
                    var attrName = possibleNameAttributes[i];
                    var keep = false;
                    if (attrName.endsWith('|keep')) {
                        keep = true;
                        attrName = attrName.slice(0, 0 - '|keep'.length);
                        possibleNameAttributes[i] = attrName;
                    }
                    varName = this.getAttribute(attrName);
                    if (varName) {
                        if (!keep) {
                            this.removeProperty(attrName);
                        }
                        break;
                    }
                }
                if (!varName) {
                    this.addError('Attribute ' + possibleNameAttributes.join(' or ') + ' is required');
                    varName = '_var';    // Let it continue with errors
                }
            } else {
                varName = nestedVar.name;
                if (!varName) {
                    this.addError('Variable name is required');
                    varName = '_var';    // Let it continue with errors
                }
            }
            variableNames.push(varName);
        }, this);

        if (this.additionalVars.length) {
            variableNames = variableNames.concat(this.additionalVars);
        }

        template.functionCall(tagHelperVar, function () {
            template.code('context,\n').indent(function () {
                template.line(handlerVar + ',').indent();
                if (_this.inputExpression) {
                    template.code(_this.inputExpression);
{'fraction_non_alphanumeric': 0.11660096364432763, 'fraction_numerical': 0.0019272886552781428, 'mean_word_length': 1.9324428461340868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2244740', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2866, 'n_words': 675}