<commit_before>'use strict';

var mediaQuery = require('css-mediaquery'),
    rework     = require('rework'),
    grids      = require('rework-pure-grids'),
    hbs        = require('../lib/hbs'),
    utils      = require('../lib/utils'),
    middleware = require('../middleware');

exports.index = [middleware.exposeTemplates('start'), showStart];
//exports.index = showStart;

// -----------------------------------------------------------------------------

/*
    Routes for /start/ could be any of the following:
    * `/start/`
    * `/start/?cols=6&med=48em&lrg=60em`
    * `/start/?cols=6&sm=screen and (min-device-width: 480px)`
*/
function showStart (req, res, next) {

    var query = normalizeQuery(utils.extend({}, req.query));

    query.css = rework('').use(grids.units(query.cols, {
        mediaQueries: query.mediaQueries.reduce(function (prev, curr) {
            prev[curr.id] = curr.value;
            return prev;
        }, {})
    })).toString();

    res.expose(query, 'start.query');
    res.render('start', query);
}

// Takes in a string input for number of columns and converts it into an array.
function normalizeCols (cols) {
    //cols will always be a string, so we can convert the string to an array of 1 or more integers.
    return cols.split(',').map(function (x) {
        return parseInt(x, 10);
    });
}

/*
Checks to see if media queries are valid, by following these steps:
    Does the query param value parse as a media query?
        If yes? return true.
        If not, then assume it's a width value, so wrap it with "screen and (min-width: " + value + ")"
    Does it now parse as a media query?<commit_msg>add and expose limits for columns and media queries<commit_after>'use strict';

var mediaQuery = require('css-mediaquery'),
    rework     = require('rework'),
    grids      = require('rework-pure-grids'),
    hbs        = require('../lib/hbs'),
    utils      = require('../lib/utils'),
    middleware = require('../middleware');

exports.index = [middleware.exposeTemplates('start'), showStart];
//exports.index = showStart;

// -----------------------------------------------------------------------------

/*
    Routes for /start/ could be any of the following:
    * `/start/`
    * `/start/?cols=6&med=48em&lrg=60em`
    * `/start/?cols=6&sm=screen and (min-device-width: 480px)`
*/
function showStart (req, res, next) {

    var query = normalizeQuery(utils.extend({}, req.query));


    if (isBelowColLimit(query.cols) && isBelowMqLimit(query.mediaQueries)) {
        query.css = rework('').use(grids.units(query.cols, {
            mediaQueries: query.mediaQueries.reduce(function (prev, curr) {
                prev[curr.id] = curr.value;
                return prev;
            }, {})
        })).toString();

        res.expose(query, 'start.query');
        res.expose({
            isBelowColLimit: isBelowColLimit,
            isBelowMqLimit: isBelowMqLimit
        }, 'start.utils');

        res.render('start', query);
    }
    else {
        if (!res.locals.message) {
            res.locals.message = 'To protect our servers from being overloaded, our online tool can only generate up to 100 columns and 20 media queries. Try again with a lower number of columns or media queries.';
        }
        next(utils.error(400));
    }
}

// Takes in a string input for number of columns and converts it into an array.
function normalizeCols (cols) {
    //cols will always be a string, so we can convert the string to an array of 1 or more integers.
    return cols.split(',').map(function (x) {
        return parseInt(x, 10);
    });
}

function isBelowColLimit (cols) {
    if (cols) {
        return (cols <= 100);
    }
    return true;

}

function isBelowMqLimit (mq) {
    if (mq && mq.length) {
        return (mq.length <= 20);
    }
    return true;
}

/*
Checks to see if media queries are valid, by following these steps:
    Does the query param value parse as a media query?
        If yes? return true.
        If not, then assume it's a width value, so wrap it with "screen and (min-width: " + value + ")"
    Does it now parse as a media query?
{'fraction_non_alphanumeric': 0.1694055099081682, 'fraction_numerical': 0.008941517641372644, 'mean_word_length': 2.284920634920635, 'pattern_counts': {'":': 0, '<': 5, '<?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': '865446', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1226, 'n_words': 380}