Rory's Blog
Home
Blog
Projects
Facebook
Google+
Github
RSS

Updating Scripts

10/12/15

The first thing I should do is update my new area to mirror my current area, as I'm not yet publishing with the new files or anything.

node scripts/fixPosts.js

Okay, what do I need to do?

  1. Grab each post in the views/blog area and create a proper html translation in public/blog

Actually, let's focus on that for now.

/*jslint node: true */
(function () {
    'use strict';

    var args,
        fs = require('fs'),
        path = require('path'),
        translateDirectory,
        translateFile,
        populateTemplate;

    args = (function () {
        var ArgumentParser = require('argparse').ArgumentParser,
            parser;

        parser = new ArgumentParser({
            version: '0.0.1',
            addHelp: true,
            description: 'Generate html for serving'
        });
        return parser.parseArgs();
    }());


    populateTemplate = function (template, data) {
        var cheerio = require('cheerio'),
            {{post}}#xA0;= cheerio.load(data),
            post;

        post = (template
            .replace(/\{\{title\}\}/g, $('.title').text().trim())
            .replace(/\{\{date\}\}/g, $('.date').text().trim())
            .replace(/\{\{content\}\}/g, $('.blogpost').html().trim())
        );

        return post;
    };

    translateFile = function (filename) {
        fs.readFile('templates/post.html', 'utf-8', function (err, template) {
            if (err) {
                console.log(err);
                return;
            }
            fs.readFile(filename, 'utf-8', function (err, data) {
                var translatedData;
                if (err) {
                    console.log(filename);
                    console.log(err);
                    return;
                }

                // Grab title, date, and blogpost
                translatedData = populateTemplate(template, data);
                throw translatedData;
            });
        });
    };

    translateDirectory = function (area) {
        fs.readdir(area, function (err, files) {
            if (err) {
                console.log(area);
                console.log(err);
                return;
            }

            files.forEach(function (file) {
                var relpath;

                relpath = path.join(area, file);
                translateFile(relpath);
            });
        });
    };

    // Get all the files in views/blog area
    translateDirectory('views/blog');
}());

That's what I got so far, it prints out a formatted blogpost from each of my barebones posts that only contain the title, date, post content, and RSS item descriptions within them.

Next step is actually saving them to an area. At first I'll save them to a test area, and once I verify posts are generated as I want them to be, I'll actually let it overwrite the actual public/blog area.