<commit_before>'use strict';

var fs = require('fs');
var path = require('path');
var minify = require('html-minifier').minify;
var CleanCSS = require('clean-css');

module.exports = function (content, options) {
	options = options || {};
	options.base = options.base || './';

	content = processStyleUrls(content, options);
	content = processTemplateUrl(content, options);

	return content;
};

function processStyleUrls(content, options) {
	let re = /styleUrls\s*:\s*(\[[^](.[^]*?)\])/g;
	let matches = content.match(re);

	if (matches === null || matches.length <= 0) {
		return content;
	}

	matches.forEach(function () {
		let exec = re.exec(content);
		let style = exec[0];
		let urls = exec[1];
		urls = urls.replace(/'/g, '"');
		urls = JSON.parse(urls);

		var result = '';
		urls.forEach(function (url) {
			let file = fs.readFileSync(path.join(options.base, url), 'utf-8');
			if (options.compress) {
				file = new CleanCSS().minify(file).styles;
			} else {
				file = file.replace(/[\r\n]/g, '');
			}

			result += file;
		});

		content = content.replace(style, 'styles: [\'' + result + '\']');
	});

	return content;
}

function processTemplateUrl(content, options) {
	let re = /templateUrl\s*:\s*(?:"([^"]+)"|'([^']+)')/g;
	let matches = content.match(re);

	if (matches === null || matches.length <= 0) {
		return content;
	}

	matches.forEach(function () {
		let exec = re.exec(content);
		let template = exec[0];
		let quote;
		let url;
		if (exec[1]) {
			url = exec[1];
			quote = '"';
		} else {
			url = exec[2];
			quote = '\'';
		}

		let file = fs.readFileSync(path.join(options.base, url), 'utf-8');
		if (options.compress) {
			file = minify(file,
				{
					collapseWhitespace: true,
					removeComments: true,
// ng2 bindings break the parser for html-minifer, so the following blocks the processing of ()="" and []="" attributes
					ignoreCustomFragments: [/\s\[.*\]=\"[^\"]*\"/, /\s\([^)"]+\)=\"[^\"]*\"/]
				});
				// escape quote chars<commit_msg>cleaned up comments for ignore fragment<commit_after>'use strict';

var fs = require('fs');
var path = require('path');
var minify = require('html-minifier').minify;
var CleanCSS = require('clean-css');

module.exports = function (content, options) {
	options = options || {};
	options.base = options.base || './';

	content = processStyleUrls(content, options);
	content = processTemplateUrl(content, options);

	return content;
};

function processStyleUrls(content, options) {
	let re = /styleUrls\s*:\s*(\[[^](.[^]*?)\])/g;
	let matches = content.match(re);

	if (matches === null || matches.length <= 0) {
		return content;
	}

	matches.forEach(function () {
		let exec = re.exec(content);
		let style = exec[0];
		let urls = exec[1];
		urls = urls.replace(/'/g, '"');
		urls = JSON.parse(urls);

		var result = '';
		urls.forEach(function (url) {
			let file = fs.readFileSync(path.join(options.base, url), 'utf-8');
			if (options.compress) {
				file = new CleanCSS().minify(file).styles;
			} else {
				file = file.replace(/[\r\n]/g, '');
			}

			result += file;
		});

		content = content.replace(style, 'styles: [\'' + result + '\']');
	});

	return content;
}

function processTemplateUrl(content, options) {
	let re = /templateUrl\s*:\s*(?:"([^"]+)"|'([^']+)')/g;
	let matches = content.match(re);

	if (matches === null || matches.length <= 0) {
		return content;
	}

	matches.forEach(function () {
		let exec = re.exec(content);
		let template = exec[0];
		let quote;
		let url;
		if (exec[1]) {
			url = exec[1];
			quote = '"';
		} else {
			url = exec[2];
			quote = '\'';
		}

		let file = fs.readFileSync(path.join(options.base, url), 'utf-8');
		if (options.compress) {
			file = minify(file,
				{
					collapseWhitespace: true,
					removeComments: true,
					/*
						ng2 bindings break the parser for html-minifer, so the 
						following blocks the processing of ()="" and []="" attributes
					*/
					ignoreCustomFragments: [/\s\[.*\]=\"[^\"]*\"/, /\s\([^)"]+\)=\"[^\"]*\"/]
				});
				// escape quote chars
{'fraction_non_alphanumeric': 0.21073558648111332, 'fraction_numerical': 0.0054671968190854875, 'mean_word_length': 3.3372844827586206, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4238300', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1389, 'n_words': 316}