<commit_before>'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var path = require('path');
var fs = require('fs');

var varsCache = {},
	partialsCache = {},
	templates = {},
	varRegExpCache = {},
	attrRegExpCache = {},
	includeRegExp = /<%include([^>]+)%>/gim,
	attrTemplateRegExp = /\{\{[ ]*([a-zA-Z]+)[ ]*\}\}/gim,
	getSrcAttr = createGetAttrFn('src');

function renderPartial(partialPath) {
	var partial = getPartial(partialPath);
	return includePartial(partial);
}

function includePartial(partial) {
	var dir = partial.dir,
		html = partial.content,
		matches = html.match(includeRegExp);

	if (!matches) {
		return html;
	}

	for(var i = 0, max = matches.length; i < max; i++) {
		var src = getSrcAttr(matches[i]),
			childPartialPath = path.join(dir, src),
			childPartial = getPartial(childPartialPath);

		if (typeof childPartial !== "undefined") {
			html = html.replace(matches[i], includePartial(childPartial));
		}
	}

	return html;
};

function getPartial(partialPath) {
	var partial = partialsCache[partialPath],
		stat = getPartialStat(partialPath);

	if (!stat) {
		gutil.log(gutil.colors.red('ERROR:'), 'Partial', partialPath, 'does not exists');
		gutil.beep();
		return undefined;
	}

	if (!partial || partial.mtime < stat.mtime) {
		var partialContent = fs.readFileSync(partialPath, 'utf8');
		partial = cachePartial(partialPath, partialContent, stat.mtime);
	}

	return partial;
}

function getPartialStat(partialPath) {
	try {
		return fs.statSync(partialPath);
	} catch(e) {
		return false;
	}
}

function cachePartial(partialPath, content, mtime) {
	return partialsCache[partialPath] = {
		content: content,
		mtime: mtime,
		dir: path.dirname(partialPath)
	}
}

function updatePartialFromVinyl(vinylFile) {
	var partial = partialsCache[vinylFile.path];
	cachePartial(vinylFile.path, vinylFile.contents.toString('utf8'), vinylFile.stat.mtime);
}

function addTemplate(name, template) {
	var templateRegExp = new RegExp('<%' + name + '([^>]+)%>', 'gim'),
		attrs = resolveTemplateAttrs(template),
		getAttrFns = [];

	for(var i = 0; i < attrs.length; i++) {<commit_msg>Handling invalid src during interpolation of partials<commit_after>'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var path = require('path');
var fs = require('fs');

var varsCache = {},
	partialsCache = {},
	templates = {},
	varRegExpCache = {},
	attrRegExpCache = {},
	includeRegExp = /<%include([^>]+)%>/gim,
	attrTemplateRegExp = /\{\{[ ]*([a-zA-Z]+)[ ]*\}\}/gim,
	getSrcAttr = createGetAttrFn('src');

function renderPartial(partialPath) {
	var partial = getPartial(partialPath);
	return includePartial(partial);
}

function includePartial(partial) {
	var dir = partial.dir,
		html = partial.content,
		matches = html.match(includeRegExp);

	if (!matches) {
		return html;
	}

	for(var i = 0, max = matches.length; i < max; i++) {
		var src = getSrcAttr(matches[i]);
		if (!src) {
			gutil.log(gutil.colors.red('ERROR:'), 'Invalid src attribute in',
				gutil.colors.green(matches[i]), gutil.colors.blue('(' + partial.path + ')'));
			gutil.beep();
			continue;
		}

		var childPartialPath = path.join(dir, src),
			childPartial = getPartial(childPartialPath);

		if (typeof childPartial !== "undefined") {
			html = html.replace(matches[i], includePartial(childPartial));
		}
	}

	return html;
};

function getPartial(partialPath) {
	var partial = partialsCache[partialPath],
		stat = getPartialStat(partialPath);

	if (!stat) {
		gutil.log(gutil.colors.red('ERROR:'), 'Partial', partialPath, 'does not exists');
		gutil.beep();
		return undefined;
	}

	if (!partial || partial.mtime < stat.mtime) {
		var partialContent = fs.readFileSync(partialPath, 'utf8');
		partial = cachePartial(partialPath, partialContent, stat.mtime);
	}

	return partial;
}

function getPartialStat(partialPath) {
	try {
		return fs.statSync(partialPath);
	} catch(e) {
		return false;
	}
}

function cachePartial(partialPath, content, mtime) {
	return partialsCache[partialPath] = {
		content: content,
		mtime: mtime,
		dir: path.dirname(partialPath),
		path: partialPath
	}
}

function updatePartialFromVinyl(vinylFile) {
	var partial = partialsCache[vinylFile.path];
	cachePartial(vinylFile.path, vinylFile.contents.toString('utf8'), vinylFile.stat.mtime);
}

function addTemplate(name, template) {
	var templateRegExp = new RegExp('<%' + name + '([^>]+)%>', 'gim'),
		attrs = resolveTemplateAttrs(template),
		getAttrFns = [];

	for(var i = 0; i < attrs.length; i++) {
{'fraction_non_alphanumeric': 0.16438053097345132, 'fraction_numerical': 0.0022123893805309734, 'mean_word_length': 4.940867279894875, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '4449480', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1512, 'n_words': 321}