<commit_before>'use strict';

var queue = require('queue-async');
var SubstitutionTokens = require('../substitution-tokens');

function TurboCartoAdapter(turboCartoParser) {
    this.turboCartoParser = turboCartoParser;
}

module.exports = TurboCartoAdapter;

TurboCartoAdapter.prototype.getLayers = function (username, layers, callback) {
    var self = this;

    if (!layers || layers.length === 0) {
        return callback(null, layers);
    }

    var parseCartoQueue = queue(layers.length);

    layers.forEach(function(layer) {
        parseCartoQueue.defer(self._parseCartoCss.bind(self), username, layer);
    });

    parseCartoQueue.awaitAll(function (err, layers) {
        if (err) {
            return callback(err);
        }

        return callback(null, layers);
    });
};

TurboCartoAdapter.prototype._parseCartoCss = function (username, layer, callback) {
    if (!shouldParseLayerCartocss(layer)) {
        return callback(null, layer);
    }

    var sql = SubstitutionTokens.replace(layer.options.sql, {
        bbox: 'ST_MakeEnvelope(-20037508.34,-20037508.34,20037508.34,20037508.34,3857)',
        scale_denominator: '500000001',
        pixel_width: '156412',
        pixel_height: '156412'
    });

    this.turboCartoParser.process(username, layer.options.cartocss, sql, function (err, cartocss) {
        // Only return turbo-carto errors
        if (err && err.name === 'TurboCartoError') {
            err = new Error('turbo-carto: ' + err.message);
            err.http_status = 400;
            return callback(err);
        }

        // Try to continue in the rest of the cases
        if (cartocss) {
            layer.options.cartocss = cartocss;
        }
        return callback(null, layer);<commit_msg>Very raw implementation of SubstitutionTokens based on origin data<commit_after>'use strict';

var dot = require('dot');
dot.templateSettings.strip = false;
var queue = require('queue-async');
var SubstitutionTokens = require('../substitution-tokens');

function TurboCartoAdapter(turboCartoParser) {
    this.turboCartoParser = turboCartoParser;
}

module.exports = TurboCartoAdapter;

TurboCartoAdapter.prototype.getLayers = function (username, layers, callback) {
    var self = this;

    if (!layers || layers.length === 0) {
        return callback(null, layers);
    }

    var parseCartoQueue = queue(layers.length);

    layers.forEach(function(layer) {
        parseCartoQueue.defer(self._parseCartoCss.bind(self), username, layer);
    });

    parseCartoQueue.awaitAll(function (err, layers) {
        if (err) {
            return callback(err);
        }

        return callback(null, layers);
    });
};

var bboxTemplate = dot.template('(select ST_SetSRID(st_extent(the_geom_webmercator), 3857) from ({{=it._sql}}) __c)');
var zoomTemplate = dot.template([
    'GREATEST(',
    'ceil(log(40075017000 / 256 / GREATEST(',
    '  st_xmax({{=it._bbox}}) - st_xmin({{=it._bbox}}),',
    '  st_ymax({{=it._bbox}}) - st_ymin({{=it._bbox}})',
    '))/log(2)),',
    '0',
    ')'
].join('\n'));
var pixelSizeTemplate = dot.template('40075017 * cos(ST_Y(ST_Centroid({{=it._bbox}}))) / 2 ^ (({{=it._zoom}}) + 8)');
var scaleDenominatorTemplate = dot.template('({{=it._pixelSize}} / 0.00028)::numeric');


TurboCartoAdapter.prototype._parseCartoCss = function (username, layer, callback) {
    if (!shouldParseLayerCartocss(layer)) {
        return callback(null, layer);
    }

    var tokens = {
        bbox: 'ST_MakeEnvelope(-20037508.34,-20037508.34,20037508.34,20037508.34,3857)',
        scale_denominator: '500000001',
        pixel_width: '156412',
        pixel_height: '156412'
    };

    var layerSql = layer.options.sql;
    var layerRawSql = layer.options.sql_raw;
    if (SubstitutionTokens.hasTokens(layerSql) && layerRawSql) {
        var bbox = bboxTemplate({ _sql: layerRawSql });
        var zoom = zoomTemplate({ _bbox: bbox });
        var pixelSize = pixelSizeTemplate({ _bbox: bbox, _zoom: zoom });
        var scaleDenominator = scaleDenominatorTemplate({ _pixelSize: pixelSize });

        tokens = {
            bbox: bbox,
            scale_denominator: scaleDenominator,
            pixel_width: pixelSize,
            pixel_height: pixelSize
        };
    }

    var sql = SubstitutionTokens.replace(layerSql, tokens);

    this.turboCartoParser.process(username, layer.options.cartocss, sql, function (err, cartocss) {
        // Only return turbo-carto errors
        if (err && err.name === 'TurboCartoError') {
            err = new Error('turbo-carto: ' + err.message);
            err.http_status = 400;
            return callback(err);
        }

        // Try to continue in the rest of the cases
        if (cartocss) {
            layer.options.cartocss = cartocss;
        }
        return callback(null, layer);
{'fraction_non_alphanumeric': 0.13643926788685523, 'fraction_numerical': 0.03618968386023295, 'mean_word_length': 2.977667493796526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6880034', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1517, 'n_words': 298}