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

Blog Refactor Progress

09/19/15

I now have a copy of each published post on this blog in its own area where only the title, content, and RSS description in each file. Just writing that, I realize I should also put the published date in the file as well.

Anyways, the script itself is becoming quite the piece of bloat, but as it stands, it'll be something I throw away once it's done.

Speaking of throwing stuff away, I wonder if you ever noticed that the tab title of all my posts pre-"Post have individual Links Now!", Stephan. I certainly didn't until now. It seems the old script that generated those posts was not properly written. As can be seen by the titles as they are now (javascript function definitions). That will be fixed soon enough.

Eventually, I'll want some file in place for good updating to my system from the old system. It may be of use to explore how the Django framework manages such intelligent transitions. Though, such digging may only be useful once I start putting these posts into a proper database.

In any case, here's the script in all its glory:

/*jslint node: true */
(function () {
    "use strict";
    var cheerio = require('cheerio'),
        fs = require('fs'),
        path = require('path'),
        postsInit;

    postsInit = function (callback) {
        var database;

        database = path.join('database', 'blog.txt');

        fs.readFile(database, 'utf-8', function (err, data) {
            var filenames,
                count;

            if (err) {
                throw err;
            }

            filenames = data.split('\n').filter(function (filename) {
                return filename !== '';
            });

            count = 0;
            filenames.forEach(function (filename, index) {
                var postpath = 'public/blog/' + filename;
                fs.readFile(postpath, 'utf-8', function (err, data) {
                    var post,
                        viewpath,
                        viewtext;

                    if (err) {
                        throw err;
                    }

                    post = cheerio.load(data);

                    viewtext = (
                        '     title '
                        + '\n'
                        + '      ' + post('h1').text().trim()
                        + '\n'
                        + '     /title '
                        + '\n'
                        + '     div id="content" '
                        + post('#content,#temp').html()
                        + ' /div '
                    );

                    viewpath = postpath.replace('public', 'views')

                    fs.writeFile(viewpath, viewtext, function (err) {
                        if (err) {
                            throw err;
                        }
                        console.log(filename)
                        count += 1;
                        if (count === filenames.length) {
                            callback();
                        }
                    });
                });

            });
        });
    };

    postsInit(function () {
        fs.readFile('public/basic.rss', 'utf-8', function (err, data) {
            var rss;

            if (err) {
                return console.trace(err);
            }

            rss = cheerio.load(data, {xmlMode: true});

            // TODO: sync up every RSS item with its post
            rss('item').each(function (index) {
                var postpath,
                    itemtext;

                postpath = rss(this).find('guid').text().trim()
                    .replace('http://www.ksionda.me', 'public');

                itemtext = (
                    '       description '
                    + rss('description', this).text()
                    + ' /description '
                );

                fs.readFile(postpath, 'utf-8', function (err, data) {
                    var post,
                        viewpath,
                        viewtext;

                    if (err) {
                        return console.trace(err);
                    }

                    post = cheerio.load(data);

                    viewtext = (
                        '     title '
                        + '\n'
                        + '    ' + post('h1').text().trim()
                        + '\n'
                        + '    /title '
                        + '\n'
                        + '     div id="content" '
                        + post('#content,#temp').html()
                        + ' /div '
                        + '\n'
                        + itemtext
                    );

                    viewpath = postpath.replace('public', 'views')

                    fs.writeFile(viewpath, viewtext, function (err) {
                        if (err) {
                            throw err;
                        }
                    });
                });

            });
        });
    });


    // TODO: Generate a file with an RSS section and blog post section

    // TODO: create copy of indexGenerate that updates the blog index file
    // and each blog post url that doesn't currently exist and updates the
    // rss file
}());

I got a ways to go with my javascript craft.