<commit_before>// Barbiche
// version: 0.6.0
// author: <NAME> <<EMAIL>>
// license: MIT

'use strict';

var attrs = ['if', 'alias', 'text', 'html', 'repeat', 'import', 'attr', 'class', 'global'];
var BB_IF = 0, BB_ALIAS = 1, BB_TEXT = 2, BB_HTML = 3, BB_REPEAT = 4,
		BB_IMPORT = 5, BB_ATTR = 6, BB_CLASS = 7, BB_GLOBAL = 8;

var TEMPLATE = 'TEMPLATE';

/* shared context */

var context = {
	stack: []
};

context.resolve = function(identifier) {
	var m = this.stack.length - 1;
	var value;
	while (value === undefined && m >= 0) {
		value = this.stack[m][identifier];
		m--;
	}
	return (value === undefined) ? window[identifier] : value;
};

context.init = function(arr) {
	this.stack = arr || [];
};

context.push = function(obj) {
	this.stack.push(obj);
};

context.pop = function() {
	return this.stack.pop();
};

/* bbObj class */

function bbObj(a, b) {
	this.value = a;
	this.name = b;
}

bbObj.prototype.toString = function() {
	if (this.value && this.name != null) return this.name.toString();
	else return '';
}

/* shared Parser*/

var Parser = require('../parser.js');
Parser.parser.yy.context = context;
Parser.parser.yy.bbObj = bbObj;

/* Barbiche instance builder */

function Barbiche(_opt) {
	var opt = _opt || {};
	var doc = opt.document || document;

	var destructive = (opt.destructive !== undefined) ? !!opt.destructive : true;

	var prefixedAttrs = attrs.map(function(str) {
		return (opt.prefix || 'bb-') + str;
	});
	var prefixedAttrsObj = {};
	prefixedAttrs.forEach(function(attr, index) {
		prefixedAttrsObj[attr] = attrs[index];
	});

	var globalAttr = prefixedAttrs[BB_GLOBAL];
	var globalAttrSel = '[' + globalAttr + ']';

	function createTemplate() {
		return doc.createElement('template');
	}

	var store = {};

	/* Compilation helpers */

	var compile_works = {};
	compile_works[Node.ELEMENT_NODE] = function(node, template) {
		if (node.hasAttribute(prefixedAttrs[BB_REPEAT]) && node.nodeName != TEMPLATE) {
			if (node.hasAttribute(prefixedAttrs[BB_TEXT]) || node.hasAttribute(prefixedAttrs[BB_HTML]))
				node.removeAttribute(prefixedAttrs[BB_REPEAT]);
			else {
				var wrapper = createTemplate();
				wrapper.setAttribute(prefixedAttrs[BB_REPEAT], node.getAttribute(prefixedAttrs[BB_REPEAT]));
				node.removeAttribute(prefixedAttrs[BB_REPEAT]);
				[prefixedAttrs[BB_IF], prefixedAttrs[BB_ALIAS]].forEach(function(attr) {
					if (node.hasAttribute(attr)) {
						wrapper.setAttribute(attr, node.getAttribute(attr));
						node.removeAttribute(attr);
					}
				});
				node.before(wrapper);
				wrapper.content.appendChild(node);
				node = wrapper;
			}
		}
		var bbAttrs = {};
		var attrFound = false;
		if (node.attributes.length > attrs.length) {
			prefixedAttrs.forEach(function(attr) {
				if (node.hasAttribute(attr)) {
					attrFound = true;
					var parsed = Parser.parse(node.getAttribute(attr));
					bbAttrs[prefixedAttrsObj[attr]] = template._addClosure(parsed);
					node.removeAttribute(attr);
				}
			});
		} else {
			for (var i = node.attributes.length - 1; i >= 0; i--) {
				var attr = node.attributes[i].name;
				if (attr in prefixedAttrsObj) {
					attrFound = true;
					var parsed = Parser.parse(node.attributes[i].value);
					bbAttrs[prefixedAttrsObj[attr]] = template._addClosure(parsed);
					node.removeAttribute(attr);
				}
			}
		}
		if (attrFound) node.setAttribute(globalAttr, JSON.stringify(bbAttrs));
		if (node.nodeName == TEMPLATE) {
			compile(node.content, template);
			if (!attrFound) node.replaceWith(node.content);
		} else {
			Array.from(node.childNodes).forEach(function(child) {
				compile(child, template);
			});
		}
	}

	compile_works[Node.TEXT_NODE] = (function() {
		var delimiters = opt.delimiters || ['{', '}'];

		function regExpEscape(str) {
			return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
		}

		var UnescapeDelimitersRegExp = new RegExp('\\\\(' +
			['\\\\', regExpEscape(delimiters[0]), regExpEscape(delimiters[1])].join('|') +
		')', 'g');
		function unescapeDelimiters(str) {
			return str.replace(UnescapeDelimitersRegExp, function() {return arguments[1];});
		}

		var regExpTemplate = [
			'aaa((?:a+f+c+d+e+b(?:a+f+c+d+e+b(?:a+f+c+d+e)))*)bbb',
			'aa((?:a+f+c+d+e+b(?:a+f+c+d+e))*)bb',
			'((?:b+f+c+d+e+a(?:b+f+c+d+e))(?:b+f+c+d+e+a(?:b+f+c+d+e))*(?:a(?!a)+b+f+c+d+e++a(?:b+f+c+d+e))+(?:a(?!a)))'
		];
		var table = {
			a: regExpEscape(delimiters[0]),
			b: regExpEscape(delimiters[1]),
			c: regExpEscape("\\" + delimiters[0]),
			d: regExpEscape("\\" + delimiters[1]),
			e: '[^' + regExpEscape(delimiters[0]) + regExpEscape(delimiters[1]) + ']'
		};

		var textNodeRegExp = new RegExp(regExpTemplate.map(function(str) {
			return '(?:' +
				str.replace(/\+/g, '|').replace(/a|b|c|d|e/g, function() {return table[arguments[0]];}) + ')';
		}).join('|'), 'g');

		return function(node, template) {
			var res;
			while(res = textNodeRegExp.exec(node.nodeValue)) {
				if (res[3]) {
					var t = doc.createTextNode(unescapeDelimiters(res[3]));
					node.before(t);
				} else if (res[2]) {
					var t = createTemplate();
					t.setAttribute(prefixedAttrs[BB_TEXT], unescapeDelimiters(res[2]));
					node.before(t);compile(t, template);
				} else {
					var t = createTemplate();
					t.setAttribute(prefixedAttrs[BB_HTML], unescapeDelimiters(res[1]));
					node.before(t); compile(t, template);
				}
			}
			node.remove();
		};
	})();<commit_msg>Add missing replace element for regExpTemplate<commit_after>// Barbiche
// version: 0.6.0
// author: <NAME> <<EMAIL>>
// license: MIT

'use strict';

var attrs = ['if', 'alias', 'text', 'html', 'repeat', 'import', 'attr', 'class', 'global'];
var BB_IF = 0, BB_ALIAS = 1, BB_TEXT = 2, BB_HTML = 3, BB_REPEAT = 4,
		BB_IMPORT = 5, BB_ATTR = 6, BB_CLASS = 7, BB_GLOBAL = 8;

var TEMPLATE = 'TEMPLATE';

/* shared context */

var context = {
	stack: []
};

context.resolve = function(identifier) {
	var m = this.stack.length - 1;
	var value;
	while (value === undefined && m >= 0) {
		value = this.stack[m][identifier];
		m--;
	}
	return (value === undefined) ? window[identifier] : value;
};

context.init = function(arr) {
	this.stack = arr || [];
};

context.push = function(obj) {
	this.stack.push(obj);
};

context.pop = function() {
	return this.stack.pop();
};

/* bbObj class */

function bbObj(a, b) {
	this.value = a;
	this.name = b;
}

bbObj.prototype.toString = function() {
	if (this.value && this.name != null) return this.name.toString();
	else return '';
}

/* shared Parser*/

var Parser = require('../parser.js');
Parser.parser.yy.context = context;
Parser.parser.yy.bbObj = bbObj;

/* Barbiche instance builder */

function Barbiche(_opt) {
	var opt = _opt || {};
	var doc = opt.document || document;

	var destructive = (opt.destructive !== undefined) ? !!opt.destructive : true;

	var prefixedAttrs = attrs.map(function(str) {
		return (opt.prefix || 'bb-') + str;
	});
	var prefixedAttrsObj = {};
	prefixedAttrs.forEach(function(attr, index) {
		prefixedAttrsObj[attr] = attrs[index];
	});

	var globalAttr = prefixedAttrs[BB_GLOBAL];
	var globalAttrSel = '[' + globalAttr + ']';

	function createTemplate() {
		return doc.createElement('template');
	}

	var store = {};

	/* Compilation helpers */

	var compile_works = {};
	compile_works[Node.ELEMENT_NODE] = function(node, template) {
		if (node.hasAttribute(prefixedAttrs[BB_REPEAT]) && node.nodeName != TEMPLATE) {
			if (node.hasAttribute(prefixedAttrs[BB_TEXT]) || node.hasAttribute(prefixedAttrs[BB_HTML]))
				node.removeAttribute(prefixedAttrs[BB_REPEAT]);
			else {
				var wrapper = createTemplate();
				wrapper.setAttribute(prefixedAttrs[BB_REPEAT], node.getAttribute(prefixedAttrs[BB_REPEAT]));
				node.removeAttribute(prefixedAttrs[BB_REPEAT]);
				[prefixedAttrs[BB_IF], prefixedAttrs[BB_ALIAS]].forEach(function(attr) {
					if (node.hasAttribute(attr)) {
						wrapper.setAttribute(attr, node.getAttribute(attr));
						node.removeAttribute(attr);
					}
				});
				node.before(wrapper);
				wrapper.content.appendChild(node);
				node = wrapper;
			}
		}
		var bbAttrs = {};
		var attrFound = false;
		if (node.attributes.length > attrs.length) {
			prefixedAttrs.forEach(function(attr) {
				if (node.hasAttribute(attr)) {
					attrFound = true;
					var parsed = Parser.parse(node.getAttribute(attr));
					bbAttrs[prefixedAttrsObj[attr]] = template._addClosure(parsed);
					node.removeAttribute(attr);
				}
			});
		} else {
			for (var i = node.attributes.length - 1; i >= 0; i--) {
				var attr = node.attributes[i].name;
				if (attr in prefixedAttrsObj) {
					attrFound = true;
					var parsed = Parser.parse(node.attributes[i].value);
					bbAttrs[prefixedAttrsObj[attr]] = template._addClosure(parsed);
					node.removeAttribute(attr);
				}
			}
		}
		if (attrFound) node.setAttribute(globalAttr, JSON.stringify(bbAttrs));
		if (node.nodeName == TEMPLATE) {
			compile(node.content, template);
			if (!attrFound) node.replaceWith(node.content);
		} else {
			Array.from(node.childNodes).forEach(function(child) {
				compile(child, template);
			});
		}
	}

	compile_works[Node.TEXT_NODE] = (function() {
		var delimiters = opt.delimiters || ['{', '}'];

		function regExpEscape(str) {
			return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
		}

		var UnescapeDelimitersRegExp = new RegExp('\\\\(' +
			['\\\\', regExpEscape(delimiters[0]), regExpEscape(delimiters[1])].join('|') +
		')', 'g');
		function unescapeDelimiters(str) {
			return str.replace(UnescapeDelimitersRegExp, function() {return arguments[1];});
		}

		var regExpTemplate = [
			'aaa((?:a+f+c+d+e+b(?:a+f+c+d+e+b(?:a+f+c+d+e)))*)bbb',
			'aa((?:a+f+c+d+e+b(?:a+f+c+d+e))*)bb',
			'((?:b+f+c+d+e+a(?:b+f+c+d+e))(?:b+f+c+d+e+a(?:b+f+c+d+e))*(?:a(?!a)+b+f+c+d+e++a(?:b+f+c+d+e))+(?:a(?!a)))'
		];
		var table = {
			a: regExpEscape(delimiters[0]),
			b: regExpEscape(delimiters[1]),
			c: regExpEscape("\\" + delimiters[0]),
			d: regExpEscape("\\" + delimiters[1]),
			e: '[^' + regExpEscape(delimiters[0]) + regExpEscape(delimiters[1]) + ']',
			f: regExpEscape("\\\\")
		};

		var textNodeRegExp = new RegExp(regExpTemplate.map(function(str) {
			return '(?:' +
				str.replace(/\+/g, '|').replace(/a|b|c|d|e|f/g, function() {return table[arguments[0]];}) + ')';
		}).join('|'), 'g');

		return function(node, template) {
			var res;
			while(res = textNodeRegExp.exec(node.nodeValue)) {
				if (res[3]) {
					var t = doc.createTextNode(unescapeDelimiters(res[3]));
					node.before(t);
				} else if (res[2]) {
					var t = createTemplate();
					t.setAttribute(prefixedAttrs[BB_TEXT], unescapeDelimiters(res[2]));
					node.before(t);compile(t, template);
				} else {
					var t = createTemplate();
					t.setAttribute(prefixedAttrs[BB_HTML], unescapeDelimiters(res[1]));
					node.before(t); compile(t, template);
				}
			}
			node.remove();
		};
	})();
{'fraction_non_alphanumeric': 0.19807567767601073, 'fraction_numerical': 0.00573596077342955, 'mean_word_length': 4.020901068276823, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '58734', 'n_tokens_mistral': 4550, 'n_tokens_neox': 4035, 'n_words': 671}