<commit_before>var promise         = require("bluebird");
var fs              = promise.promisifyAll(require("fs"));
var merge           = require("deepmerge");
var _               = require("lodash");
var express         = require('express');
var exphbs          = require('express-handlebars');
var path            = require('path');
var swag            = require('swag');
var Handlebars      = require('handlebars');
var beautifyHTML    = require('js-beautify').html;

var fractal = require('../../fractal');
var config  = fractal.getConfig();

module.exports = function(){
    
    var port = config.get('port');
    var app = express();
    var hbs = exphbs.create({
        extname: 'hbs',
        partialsDir: [
            config.get('theme.views')
        ],
        helpers: {}
    });

    swag.registerHelpers(hbs.handlebars);
    
    var tplData = {
        config: config.all(),
    };

    app.engine('hbs', hbs.engine);
    app.set('views', config.get('theme.views'))
    app.set('view engine', 'hbs');

    app.use('/_theme', express.static(config.get('theme.assets')));

    app.use(function (req, res, next) {
        if ( req.path === '/favicon.ico') {
            // TODO: send favicon rather than 404 :-)
            return res.status(404).render('404', tplData);
        }
        req.segments = _.compact(req.path.split('/'));
        tplData.req = req;
        fractal.getSources().then(function(sources){
            
            tplData.sources = sources;
            tplData.navigation = generatePrimaryNav(sources);

            // TEMP LOGGING ----
            // var output = JSON.stringify(sources.components.getComponents(), null, 4)
            // fs.writeFileAsync(path.join(__dirname, "/output.json"), output, function(err) {
            //   console.log('file saved');
            // }); 
            // TEMP LOGGING ----

            next();
        });
    });

    app.get('/ui', function (req, res) {
        var compSource = tplData.sources.components;
        res.render('ui', merge(tplData, {
            sectionName: 'UI Components',
            baseUrl: '/ui',
            components: compSource ? compSource.getComponents() : null
        }));
    });

    app.get('/ui/*', function (req, res) {
        var compSource = tplData.sources.components;
        var component = compSource.findComponent('path', req.path.replace(new RegExp('^\/ui\/'), ''));
        if (component) {
            var viewType = req.query.view || 'component';
            var variant = req.query.variant || 'default';
            var data = merge(tplData, {
                component: {
                    title:      component.title,
                    path:       component.path,
                    meta:       JSON.stringify(component.meta, null, 4),
                    rendered:   component.render(variant),
                    template:   component.getTemplateMarkup(),
                    data:       JSON.stringify(component.getPreviewData(variant), null, 4),
                    variant:    variant,
                    variants:   component.getVariants()<commit_msg>Fix a few server bugs<commit_after>var promise         = require("bluebird");
var fs              = promise.promisifyAll(require("fs"));
var merge           = require("deepmerge");
var _               = require("lodash");
var express         = require('express');
var exphbs          = require('express-handlebars');
var path            = require('path');
var swag            = require('swag');
var Handlebars      = require('handlebars');
var beautifyHTML    = require('js-beautify').html;

var fractal = require('../../fractal');
var config  = fractal.getConfig();

module.exports = function(){
    
    var port = config.get('port');
    var app = express();
    var hbs = exphbs.create({
        extname: 'hbs',
        partialsDir: [
            config.get('theme.views')
        ],
        helpers: {}
    });

    swag.registerHelpers(hbs.handlebars);

    var tplData = {
        config: config.all(),
    };

    app.engine('hbs', hbs.engine);
    app.set('views', config.get('theme.views'))
    app.set('view engine', 'hbs');

    app.use('/_theme', express.static(config.get('theme.assets')));

    app.use(function (req, res, next) {
        if ( req.path === '/favicon.ico') {
            // TODO: send favicon rather than 404 :-)
            return res.status(404).render('404', tplData);
        }
        req.segments = _.compact(req.path.split('/'));
        tplData.req = req;
        fractal.getSources().then(function(sources){
            
            tplData.sources = sources;
            tplData.navigation = generatePrimaryNav(sources);

            // TEMP LOGGING ----
            // var output = JSON.stringify(sources.components.getComponents(), null, 4)
            // fs.writeFileAsync(path.join(__dirname, "/output.json"), output, function(err) {
            //   console.log('file saved');
            // }); 
            // TEMP LOGGING ----

            next();
        });
    });

    app.get('/ui', function (req, res) {
        var compSource = tplData.sources.components;
        res.render('ui', merge(tplData, {
            sectionName: 'UI Components',
            baseUrl: '/ui',
            components: compSource ? compSource.getComponents() : null
        }));
    });

    app.get('/ui/*', function (req, res) {
        var compSource = tplData.sources.components;
        var component = compSource.findComponent('path', req.path.replace(new RegExp('^\/ui\/'), ''));
        if (component) {
            var viewType = req.query.view || 'component';
            var variant = req.query.variant || 'default';
            var variants = component.getVariants() || [];
            var data = merge(tplData, {
                component: {
                    title:      component.title,
                    path:       component.path,
                    meta:       JSON.stringify(component.meta, null, 4),
                    rendered:   component.render(variant),
                    template:   component.getTemplateMarkup(),
                    data:       JSON.stringify(component.getPreviewData(variant), null, 4),
                    variant:    variant,
                    variants:   variants.length > 1 ? variants : null 
{'fraction_non_alphanumeric': 0.14965769781881866, 'fraction_numerical': 0.003980257920713263, 'mean_word_length': 1.825910931174089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2094844', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1706, 'n_words': 349}