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

JSON File Database

04/10/15

Okay, so, copying each blogpost into the diary or blog's home index.html file is not a particularly DRY way to go about things. What would be much better is if I constructed the home index.html areas out of the individual blogposts. Unfortunately, the blogposts alone don't contain the necessary information for the proper order of the blogposts in the main area.

I mean, yeah, they are more-or-less in chronological order, so I could, perhaps, get away with date of the files.... Except, you know, I just recently created a lot of old blogpost files. So, quite clearly, that won't work. I also remember modifying some typos here and there after-the-fact, so it probably wouldn't work in the first place. Filesystem timing wouldn't be something that's particularly trustworthy.

So, currently, the only thing that is aware of the order of the blogposts are the index.html files. So, I'm going to have to steal that information from those files and save it to some other files.

Now, really, a proper database might be ideal, but considering my use case, I can get away with human-readable blog.json and diary.json files. For now, the files need but two things: index placement of post and filepath. The rest can be constructed from there.

Thinking about it, I guess it works better for it to be an array of filepaths. It could just be one json file, too. Let's just make it one json file that looks something like:

{
  blog: [
    <filepaths>
  ],
  diary: [
    <filepaths>
  ]
}

Now, then, let's hack together something that makes this file.

Actually, if we're going to hack something together, let's just make a blog.txt and diary.txt that's a simple list of the links. No additional syntax stuff required.

And, first-things-first, let's just print out the blog links in the order they appear:

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

    fs.readFile('public/blog/index.html', 'utf-8', function (err, data) {
        if (err) {
            return console.log(err);
        }

        $('.blogpost', data).each(function () {
            var blogpost = $(this);

            console.log(blogpost.find('h1 a').attr('href'));
        });
    });
}());

This outputs the correct answer (output was long and so I'm not pasting it here, I think).

Oh, wait, spoke too soon, there is something interesting in that output:

...
individual_posts_have_permalink.html
less_everyday_items.html
no_evo_for_me.html
http://www.ksionda.me/blog/highlighting_80th_column_vim.html
http://www.ksionda.me/blog/meta_styling_code.html
http://www.ksionda.me/blog/some_days_you_just_gotta_read.html
http://www.ksionda.me/blog/quakecon_approaches.html
http://www.ksionda.me/blog/bash_scripting_improvements.html
http://www.ksionda.me/blog/code_complete_complete.html
http://www.ksionda.me/blog/quakecon_retrospective.html
http://www.ksionda.me/blog/quakecon_over.html
...

That's right, I wasn't really aware of relative links when I first started doing links. Or I thought absolute paths were better for some reason. Well, for whatever reason some links are absolute paths. That doesn't really jive with me. Let's switch over and devote some time to ridding the site of unnecessary absolute paths.

This'll be continued tomorrow.