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

Personal System Iterate: Weekly Review Init

04/03/15

Last time I hacked something together to list the title and date of posts since the date of my last weekly review. I think from here I could also try and grab the relative link of the posts and print those out as well.

One thing that might be valuable at this point is to clean up the code I already have a little bit.

$('.blogpost').each(function (index) {
    var blogpost = this,
        whitespace = /\ /g,
        valid,
        date,
        title;

    title = function (blogpost) {
        return $(blogpost).children('h1').text();
    };

    date = function (blogpost) {
        return $(blogpost).children('h2').text();
    };

    valid = function () {
        if (earliestDate && date(blogpost).search(earliestDate) === -1) {
            return false;
        }
        if (!earliestDate) {
            if (title(blogpost).replace(whitespace, '') === 'WeeklyReview') {
                earliestDate = date(blogpost);
            }
        }
        return true;
    };

    if (valid()) {
        blogposts[index] = $(blogpost).children('h1,h2').text();
        return;
    }
    return false;
});

Much better, now the code is a little more descriptive about what it's doing or trying to do. Some comments may also help but this is good for now.

Also, I realized I could print out the posts in chronological order via a simple reversal of the array. Yay!

So, getting the link should be as simple as

blogpost.children('a').attr('href');

Is it really that easy?

Nope, getting a bunch of undefined values. Must be missing something. Let's check the documentation.

Hmmm....

Maybe find will do what I want?

blogpost.find('a').attr('href');

It works! Even if there are multiple links inside a div post, the attr function will only return the value of the first one. Even so, I figure being more explicit would be better for the code in the long run. Though, that itself doesn't matter because this is prototype code that I will throw away soon, but still, practice makes better.

if (valid()) {
    blogposts[index] = (
        $(blogpost).children('h1,h2').text()
        + $(blogpost).find('a').attr('href')
    );
    return;
}

This code spits out:

$ node blogpost.js ../public/diary/index.html
  Plan Document  03/30/15 plan_document_150330.html
  UA Game Dev Club Meeting  03/30/15 gamedevclub_150330.html
  Weekly Review  03/30/15 weekly_review_150330.html
  End of Day Review  03/30/15 end_of_day_review_150330.html
  Plan Document  03/30/15 log_150330.html
  Plan Document  03/31/15 plan_document_150331.html
  Personal System Iterate: Weekly Review Init  03/31/15 personal_system_iterate_weekly_review_init_150331.html
  End of Day Routine  03/31/15 end_of_day_routine_150331.html
  End of Day Review  03/31/15 end_of_day_review_150331.html
  Log  03/31/15 log_150331.html
  Plan Document  04/01/15 plan_document_150401.html
  Personal System Iterate: Weekly Review Init  04/01/15 personal_system_iterate_weekly_review_init_150401.html
  Log  04/01/15 log_150401.html
  Plan Document  04/02/15 plan_document_150402.html
  Personal System Iterate: Weekly Review Init  04/02/15 personal_system_iterate_weekly_review_init_150402.html
  Log  04/01/15 log_150402.html
  Plan Document  04/03/15 plan_document_150403.html

And that's all the time I have for today.