Personal System Iterate: Weekly Review Init
04/04/15
Last time, I managed to get the link, date, and links to all the blog posts since my last weekly review. Now, I want to learn a good means of striping the white space from the resulting string(s), but it's not actually necessary for functional code, the browser output will be the same regardless.
So now it's time to throw away this prototype code and start from scratch with a consideration for the design of my script. Why? Because a lot of the structure of this script is the way it is not for design reasons but just because of the process I took to figure things out. Now that I've figured out what I want, I should start over to structure things with more purpose.
Instead of actually deleting it, I'm going to create a new javascript file in my scripts area and work on it without looking at the old script. The old script will be used as reference when necessary.
So, to start things off, I want a jslint header that says node is present. Then I want an anonymous main function to keep my stuff from being global.
This script, specifically, will read in the blog and diary posts I've written since the last weekly review of interest and provide and init'd weekly review post with a header list of all those blogposts (with each blogpost being linked to proper).
External Distraction: Phone is dying.
Now, this end result itself can be throwaway, hard-coded code again, but here I want to make some functions that can be more generally usuable for getting at blog posts and stuff. So what I want is a function that will return blogposts that passes some kind of filtering function. In this case, I'll feed this function a function that passes blogposts written on or after the date of the last weekly review.
At first I scaffold the other parts of the code that would get that date and just hard-code the date for now.
Okay, here's what I got:
/*jslint node: true */
(function () {
'use strict';
var fs = require('fs'),
$ = require('cheerio'),
beforeDate,
linkText;
linkText = function (postList) {
return function () {
var title,
date,
link,
blogpost = $(this);
title = function (blogpost) {
return blogpost.children('h1').text().trim();
};
date = function (blogpost) {
return blogpost.children('h2').text().trim();
};
link = function (blogpost) {
return blogpost.find('h1 a').attr('href').trim();
};
postList.push(
'<h3>\n <a href="'
+ link(blogpost)
+ '">\n '
+ title(blogpost)
+ '('
+ date(blogpost)
+ ')'
+ '</a>\n</h3>'
);
};
};
beforeDate = function (date) {
var postDate = function (blogpost) {
var h2 = blogpost.children('h2').text();
return h2.trim();
};
return function () {
var blogpost = $(this);
return (postDate(blogpost) < date);
};
};
fs.readFile('public/diary/index.html', 'utf-8', function (err, data) {
var diaryPost = [];
if (err) {
return;
}
$('.blogpost', data)
.not(beforeDate('04/04/15'))
.each(linkText(diaryPost));
console.log(diaryPost.reverse().join('\n'));
});
}());
Both the linkText and the beforeDate functions above are pieces of code I find generally usable for various website-related scripts. linkText will be useful for generating various review posts (as I may want to review series of posts as well as all the posts between weekly reviews).
I really like how the naming convention worked out, making:
$('.blogpost', data)
.not(beforeDate('04/04/15'))
.each(linkText(diaryPost));
Reads exactly like what it does, grabs all blogposts not before the date 04/04/15 and links each post. And its output:
$ node scripts/searchPosts.js
<h3>
<a href="plan_document_150404.html">
Plan Document(04/04/15)</a>
</h3>
Which is exactly correct!
Next time, I'll grab all the blogposts, as well, which will take factoring out that readFile callback function.