<commit_before>import convert from 'xml-js';
import { ACL, Bucket, getLocation, getSvgUploadOptions, mkUrl } from './upload';
import {getViewBox, isDef, merge, nodeList2Array, reduceByConcat, unDef} from './helper';
import { getColorsFromRects } from './colors';
import { getFontsFromGroups } from './fonts';
import { parseImagesFromSVG, makeSvg } from './images';

export const extractId = ({attributes}) => {
    const match = attributes.id.match(/^toolkit_(?:default_)(.+)/);
    return isDef(attributes) && isDef(attributes.id) && match && match[1];
}

export const parseToolkit = options => svg => {
    console.info('parseToolkit', 'options', options);
    const { filename, S3, hashFunction, hashMethod } = options;
    const fn = hashFunction;
    const method = hashMethod;
    return new Promise((resolve, reject) => {
        const json = JSON.parse(convert.xml2json(svg.outerHTML, {compact: false, spaces: 4})).elements[0];
        let toolkitId = extractId(json);
        toolkitId = toolkitId ? toolkitId.replace(/[_]?default[_]?/, '') : filename;
        const useDefaultToolkit = !!json.attributes.id.match(/(?:default_)/);
        const colorsGroup = nodeList2Array(svg.querySelectorAll('#colors rect'));
        const fontsGroup = nodeList2Array(svg.querySelectorAll('#fonts text'));
        const urlThumbPath = `${filename}/${filename.replace('.svg', '.thumb.svg')}`;

        Promise.all([
            // new Promise((resolve, reject) => S3.upload(getSvgUploadOptions(urlThumbPath)(svg.outerHTML)).promise().then(getLocation).then(thumbUrl => resolve({ thumbUrl }))),
            // Promise.resolve({ id: id ? id.replace(/[_]?default[_]?/, '') : filename }),
            Promise.resolve({useDefaultToolkit}),
            Promise.resolve({ colors: getColorsFromRects(colorsGroup)({fn, method}) }),
            Promise.resolve({ fonts: getFontsFromGroups(fontsGroup)({fn, method}) }),
            new Promise((resolve, reject) => parseImagesFromSVG(toolkitId, filename)(svg)(S3)({fn, method}).then(images => resolve({ images }))),
        ]).then(values => {
            // console.info('svg.outerHTML', svg.outerHTML);
            S3.upload(getSvgUploadOptions(urlThumbPath)(svg.outerHTML)).promise().then(getLocation)
            .then(thumbUrl => {<commit_msg>major thumbnail rewrite, id used in place of filename (#36)<commit_after>import convert from 'xml-js';
import { ACL, Bucket, getLocation, getSvgUploadOptions, mkUrl } from './upload';
import {getViewBox, isDef, merge, nodeList2Array, reduceByConcat, unDef} from './helper';
import { getColorsFromRects } from './colors';
import { getFontsFromGroups } from './fonts';
import { parseImagesFromSVG, makeSvg } from './images';

export const extractId = ({attributes}) => {
    const match = attributes.id.match(/^toolkit_(?:default_)(.+)/);
    return isDef(attributes) && isDef(attributes.id) && match && match[1];
}

export const parseToolkit = options => svg => {
    console.info('parseToolkit', 'options', options);
    const { filename, S3, hashFunction, hashMethod } = options;
    const fn = hashFunction;
    const method = hashMethod;
    return new Promise((resolve, reject) => {
        const json = JSON.parse(convert.xml2json(svg.outerHTML, {compact: false, spaces: 4})).elements[0];
        let toolkitId = extractId(json);
        toolkitId = toolkitId ? toolkitId.replace(/[_]?default[_]?/, '') : filename;
        const useDefaultToolkit = !!json.attributes.id.match(/(?:default_)/);
        const colorsGroup = nodeList2Array(svg.querySelectorAll('#colors rect'));
        const fontsGroup = nodeList2Array(svg.querySelectorAll('#fonts text'));
        const urlThumbPath = `${toolkitId}/${toolkitId}.thumb.svg`;

        Promise.all([
            // new Promise((resolve, reject) => S3.upload(getSvgUploadOptions(urlThumbPath)(svg.outerHTML)).promise().then(getLocation).then(thumbUrl => resolve({ thumbUrl }))),
            // Promise.resolve({ id: id ? id.replace(/[_]?default[_]?/, '') : filename }),
            Promise.resolve({useDefaultToolkit}),
            Promise.resolve({ colors: getColorsFromRects(colorsGroup)({fn, method}) }),
            Promise.resolve({ fonts: getFontsFromGroups(fontsGroup)({fn, method}) }),
            new Promise((resolve, reject) => parseImagesFromSVG(toolkitId, filename)(svg)(S3)({fn, method}).then(images => resolve({ images }))),
        ]).then(values => {
            // console.info('svg.outerHTML', svg.outerHTML);
            S3.upload({ Key: urlThumbPath, Body: svg.outerHTML, Bucket, ACL , ContentType: 'image/svg+xml' }).promise().then(getLocation)
            .then(thumbUrl => {
{'fraction_non_alphanumeric': 0.16673866090712744, 'fraction_numerical': 0.005183585313174946, 'mean_word_length': 3.8850210970464136, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '7079868', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1354, 'n_words': 283}