From 0c895d25be27693cd8ecf742305673984e07dfd1 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 29 Dec 2022 23:59:59 +0100 Subject: New blog post --- src/blog/2022-12-30-blog-ready/blog-ready.md | 52 ++++++++++++++++++++++++++ src/blog/2022-12-30-blog-ready/pc-planner.jpg | Bin 0 -> 6497789 bytes 2 files changed, 52 insertions(+) create mode 100644 src/blog/2022-12-30-blog-ready/blog-ready.md create mode 100644 src/blog/2022-12-30-blog-ready/pc-planner.jpg (limited to 'src/blog/2022-12-30-blog-ready') diff --git a/src/blog/2022-12-30-blog-ready/blog-ready.md b/src/blog/2022-12-30-blog-ready/blog-ready.md new file mode 100644 index 0000000..144e5e2 --- /dev/null +++ b/src/blog/2022-12-30-blog-ready/blog-ready.md @@ -0,0 +1,52 @@ +# Getting my blog ready for 2023 + +My first year of blogging is about to end, and I am happy with what +I wrote. I wanted to write at least one post every month, and I did. I +intend to keep this pace next year, but I want to make this easier by +writing shorter posts from time to time. This is not a trivial task: +I found out that writing good *short* content is harder than writing +good *long* content! + +But if I keep writing, [my blogs's index page](../) is going to become +messy at some point! It would be nice to divide these posts by year... + +## Adding year sections to my blog index + +Very easy: in my +[build.sh](https://git.tronto.net/sebastiano.tronto.net/file/build.sh.html) +script that I run to +[build my website](../2022-08-14-website), there is a +[`makeblog()`](https://git.tronto.net/sebastiano.tronto.net/file/build.sh.html#l52) +function that takes care of building the index page and RSS feed for my blog. + +It is enough to add the following lines inside its main loop: + +``` +thisyear=$(echo $d | sed 's/-.*//') +if [ "$thisyear" != "$lastyear" ]; then + printf "\n## $thisyear\n\n" >> $bf + lastyear=$thisyear +fi +``` + +And that's it! These few lines introduce two new variables, `thisyear` +and `lastyear`, that keep track of the years of the last and next blog +post that the loop is scanning. If there was a year change, a new line +with the current year is added, and the `lastyear` variable is updated. +The first line refers to a variable `d` that holds the date of the +current post in `yyyy-mm-dd` format. + +A last note on the variables: if you are familiar with other programming +languages, you might wonder where the variable `lastyear` is initialized. +After all, I am using it in the `if` statement's condition, so it must +be initialized outside of its body, right? + +Actually, no. The shell's variable scoping does not work like in C or +similar languages, and a variable initialized inside a block is also +visibile outside of it. Moreover, un-initialized variables evaluate to +the empty string, so the first time the condition is checked it correctly +determines that the current year is different from the last. + +This was my last UNIX shell tip for this year. Stay tuned for more! + +![My netbook and planner for 2023](pc-planner.jpg) diff --git a/src/blog/2022-12-30-blog-ready/pc-planner.jpg b/src/blog/2022-12-30-blog-ready/pc-planner.jpg new file mode 100644 index 0000000..8fb52e5 Binary files /dev/null and b/src/blog/2022-12-30-blog-ready/pc-planner.jpg differ -- cgit v1.3