<commit_before>import gm from 'gm';
import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import svg2png from 'svg2png';
import os from 'os';
import DefaultSizes from './default-sizes';
import regexEscape from './regex-escape';
import Promise from 'bluebird';
import tmpl from 'blueimp-tmpl';

async function svg2png_shim(sourceFile,destFile){
	fs.writeFileSync(destFile,await svg2png(fs.readFileSync(sourceFile)));
}

function ParamMap(str,params,before = '%',after = '%'){
	if(str != null){
		Object.entries(params).forEach(([param,value]) => {
			if(value != null){
				let regex = RegExp([
					regexEscape(before + param),
					'(?:\\(([^\\)]+)\\))?',
					regexEscape(after)
				].join(''),'g');
				str = str.replace(regex,(full,modifiers) => {
					if(modifiers){
						// This bit of code doesn't seem right.
						return modifiers.split(',').reduce((current,modifier) => {
							if(modifier == '%'){
								return parseFloat(value,10) * 100;
							}
							else {
								let matches = modifier.match(/^(\d+)x$/);
								if(matches){
									return Array(parseInt(matches[1],10)).fill(value).join('');
								}
								else {
									return value;
								}
							}
						},value);
					}
					else {
						return value;
					}
				})
			}
		});
	}
	return str;
}

function arrayParseConfig(configItem,parentConfig){
	this.push(...ParseConfig(configItem,parentConfig));
}

function transformArray(key,result){
	let transformed = false;
	if(key instanceof Array){
		transformed = key.some(k => this::transformArray(k,result));
	}
	else if(transformed = this[key] instanceof Array){
		this[key].forEach(item => result::arrayParseConfig({[key]:item},this));
	}
	return transformed;
}

function ParseConfig(config,parentConfig = {}){
	let result = [];
	let bundle = {
		scale: 1,
		resize: 'fill',
		resizeFlags: '',
		backgroundColor: 'transparent',
		gravity: 'center',
		...parentConfig,
		...config
	};
	if(bundle.size != null){
		bundle.width = bundle.height = bundle.size;
	}
	bundle.preName = ParamMap(bundle.preName,{preName:parentConfig.preName});
	bundle.name = ParamMap(bundle.name,{name:parentConfig.name});
	bundle.postName = ParamMap(bundle.postName,{postName:parentConfig.postName});
	if(bundle.skip == null){
		let transformed = bundle::transformArray('scale size'.split(' '),result);
		if(!transformed && (bundle.width != null && bundle.height != null && bundle.name != null)){
			bundle.name = ParamMap(bundle.name,bundle);
			result.push(bundle);
		}
	}
	delete bundle.skip;
	if(config.extend != null){
		config.extend.forEach(extend => result::arrayParseConfig(extend,bundle));
	}
	delete bundle.extend;
	return result;<commit_msg>Change case of default gravity<commit_after>import gm from 'gm';
import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import svg2png from 'svg2png';
import os from 'os';
import DefaultSizes from './default-sizes';
import regexEscape from './regex-escape';
import Promise from 'bluebird';
import tmpl from 'blueimp-tmpl';

async function svg2png_shim(sourceFile,destFile){
	fs.writeFileSync(destFile,await svg2png(fs.readFileSync(sourceFile)));
}

function ParamMap(str,params,before = '%',after = '%'){
	if(str != null){
		Object.entries(params).forEach(([param,value]) => {
			if(value != null){
				let regex = RegExp([
					regexEscape(before + param),
					'(?:\\(([^\\)]+)\\))?',
					regexEscape(after)
				].join(''),'g');
				str = str.replace(regex,(full,modifiers) => {
					if(modifiers){
						// This bit of code doesn't seem right.
						return modifiers.split(',').reduce((current,modifier) => {
							if(modifier == '%'){
								return parseFloat(value,10) * 100;
							}
							else {
								let matches = modifier.match(/^(\d+)x$/);
								if(matches){
									return Array(parseInt(matches[1],10)).fill(value).join('');
								}
								else {
									return value;
								}
							}
						},value);
					}
					else {
						return value;
					}
				})
			}
		});
	}
	return str;
}

function arrayParseConfig(configItem,parentConfig){
	this.push(...ParseConfig(configItem,parentConfig));
}

function transformArray(key,result){
	let transformed = false;
	if(key instanceof Array){
		transformed = key.some(k => this::transformArray(k,result));
	}
	else if(transformed = this[key] instanceof Array){
		this[key].forEach(item => result::arrayParseConfig({[key]:item},this));
	}
	return transformed;
}

function ParseConfig(config,parentConfig = {}){
	let result = [];
	let bundle = {
		scale: 1,
		resize: 'fill',
		resizeFlags: '',
		backgroundColor: 'transparent',
		gravity: 'Center',
		...parentConfig,
		...config
	};
	if(bundle.size != null){
		bundle.width = bundle.height = bundle.size;
	}
	bundle.preName = ParamMap(bundle.preName,{preName:parentConfig.preName});
	bundle.name = ParamMap(bundle.name,{name:parentConfig.name});
	bundle.postName = ParamMap(bundle.postName,{postName:parentConfig.postName});
	if(bundle.skip == null){
		let transformed = bundle::transformArray('scale size'.split(' '),result);
		if(!transformed && (bundle.width != null && bundle.height != null && bundle.name != null)){
			bundle.name = ParamMap(bundle.name,bundle);
			result.push(bundle);
		}
	}
	delete bundle.skip;
	if(config.extend != null){
		config.extend.forEach(extend => result::arrayParseConfig(extend,bundle));
	}
	delete bundle.extend;
	return result;
{'fraction_non_alphanumeric': 0.17183150862868807, 'fraction_numerical': 0.004824642790870292, 'mean_word_length': 3.842767295597484, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4297848', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1737, 'n_words': 343}