<commit_before>exports.handler = function (event, context) {
    console.log(event.object);
    context.done(null, 'dodgercms-converter complete.');
};<commit_msg>Code the mvp for the converter<commit_after>console.log('Loading event');

var fs = require('fs');
var aws = require('aws-sdk');
var marked = require('marked');
var async = require('async');
var config = require('config');
var mustache = require('mustache');
var s3 = new aws.S3({apiVersion: '2006-03-01'});

exports.handler = function(event, context) {
   console.log('Received event:');
   console.log(JSON.stringify(event, null, '  '));

   // Get the object from the event and show its content type
    var bucket = event.Records[0].s3.bucket.name;
    var key = event.Records[0].s3.object.key;

    async.waterfall([
        function(callback) {
            var params = {
                Bucket: bucket, 
                Key: key
            };

            s3.getObject(params, function(err, data) {
                if (err) {
                    callback(err);
                } else {
                    callback(null, data);
                }
            });
        },
        function(data, callback) {
            
            var content = data.Body.toString();
            var options = {
                renderer: new marked.Renderer(),
                gfm: true,
                tables: true,
                breaks: false,
                pedantic: false,
                sanitize: true,
                smartLists: true,
                smartypants: true,
                highlight: function(code) {
                    return require('highlight.js').highlightAuto(code).value;
                }
            };

            marked(content, options, function(err, data) {
                if (err) {
                    callback(err);
                } else {
                    callback(null, data);
                }
            })
            
        },
        // Process the templates
        function(body, callback) {

            var view = {
                body: body
            };

            fs.readFile("./templates/entry.html", "utf8", function(err, data) {
                if (err) {
                    callback(err);
                } else {
                    var output = mustache.render(data.toString(), view);
                    callback(null, output);
                }
            });
        },
        function(html, callback) {
            var metadata = {
                "Content-Type": "text/html"
            };

            var keyNoExtension = key.substr(0, key.lastIndexOf('.')) || key;

            var params = {
                Bucket: config.targetBucket,
                Key: keyNoExtension,
                Body: html,
                ContentType: "text/html",
                Expires: 0,
                CacheControl: "public, max-age=0, no-cache"
            };
            s3.upload(params, function(err, data) {
                if (err) {
                    callback(err);
                } else {
                    console.log('upload successful');
                    callback(null, data);
                }
            });
        }
    ], function(err, result) {
        if (err) {
            context.done('error', err);
        } else {
            context.done(null, 'Successfull!');
        } 
    });
};
{'fraction_non_alphanumeric': 0.12137518684603886, 'fraction_numerical': 0.005979073243647235, 'mean_word_length': 1.2486559139784945, 'pattern_counts': {'":': 1, '<': 3, '<?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': '5717833', 'n_tokens_mistral': 898, 'n_tokens_neox': 834, 'n_words': 187}