Simplifying My Blogging Workflow with Ruby

This is the 100th post on this blog ­čÄë I wanted to share how a simple Ruby script simplified my blogging workflow and┬ádramatically increased the number of posts I write on my various blogs.

sharpened pencil

If you've been a reader of this blog for a while, you know that I love writing, especially writing online and blogging, something I've been doing for over a decade now.

In addition to this blog on Ruby and Rails, I have three other blogs on the Internet where I write regularly: one to keep track of my reading and taking notes, a family blog as a travel journal and sharing photos, and a new blog I started a few weeks ago to teach Ruby to a group of friends in India (the time difference between Canada and India is just too much to do this over Zoom).

Except for this blog where you're reading this post, all my blogs are powered by Jekyll and hosted on Cloudflare. I've been using Jekyll for almost a decade now, and it's my favorite blogging platform to host simple static websites. Over the years, I've built and published dozens of websites using Jekyll.

I love the simplicity of Jekyll, the markdown syntax, and how it lets me keep all my writing on my computer under version control. It's also super-easy to switch hosts, like when I migrated from GitHub Pages to Cloudflare Pages a few years ago.

However, there's been a minor annoyance nagging me for a long time. Every time I want to publish a new post, here're the steps I need to take before I actually start writing.

  • Create a new markdown file under the _posts directory.
  • Give it a name that follows a specific syntax: date followed by the title of the post, all separated with dashes. For example: 2023-3-2-getting-started-with-ruby.md
  • Add the YAML front matter for the title and layout, as follows:
---
layout: post
title: "Chapter 1: Getting Started with Ruby"
---
  • Most of my blog posts include a lot of images. So I need to copy + paste the following HTML snippet to insert an image tag. Then I've to replace the image name and alt text. ┬á
<div style="margin: 2em 0; text-align: center;">
  <img src="/images/learn-to-program.jpg" alt="Learn to Program" width="150">
</div>

I have lost count of how many times I've done the above steps in all these years.

By now, I've probably got them memorized; but I still have to pause and think for a few moments to come up with today's date, format it, and write it in the special syntax for the file name, then create the front matter, etc.

Precious brain cells spent on repetitive stuff.

I've been programming for a while now, but for some reason never thought of automating this workflow.

A few weeks ago, as I was going to create a new post, I realized I could write a Ruby script that does the above steps for me. So I sit down to code it and after about 30 minutes, I had the following Ruby script.

#!/usr/bin/env ruby

title = ARGV.join(' ')

stamp = Time.now.strftime('%Y-%m-%d')

dashed_title = title.downcase.split.join('_').tr("_", "-")

filename = "#{stamp}-#{dashed_title}.md"

filepath = File.join('./_posts', filename)

content =  <<~CONTENT
  ---
  layout: post
  title: "#{title}"
  ---

  <div style="margin: 2em 0; text-align: center;">
    <img src="/images/#{dashed_title}.jpg" alt="#{title}" width="150">
  </div>
CONTENT

File.open(filepath, 'w+') { |file| file.write content }

puts "created new post: #{filepath}"

I saved it in the bin directory on all my blogs.

P.S. Did you notice the tr method above? I wanted to learn how ActiveSupport implements dasherize method and that's when I came across Ruby's tr method, which replaces characters in a String. Pretty interesting.

Now, whenever I have to write a new post, I just open the repository for that blog, type bin/post Learn to Program and it creates a pre-baked Jekyll post for me, which looks like this:

<!--- posts/2023-05-12-learn-to-program.md -->

---
layout: post
title: "Learn to Program"
---

<div style="margin: 2em 0; text-align: center;">
  <img src="/images/learn-to-program.jpg" alt="Learn to Program" width="150">
</div>

No more brainpower spent on coming up with the date, formatting it, and copy-pasting stuff.

Since then, the number of blog posts I've written has gone up significantly. In the last two weeks, I've published more than a few dozen blog articles on my various blogs. The time invested in programming the script has already been paid back in the time saved and the number of posts written.

I love how Ruby lets me create simple scripts without much ceremony. No need for classes, including any libraries, compiling, etc. Just create a file and start programming.

Have you used programming to automate similar stuff?

Share in the comments below or reply to the email. Nothing's too trivial. If not, can you think of a few ways in which you can automate some of the boring or repetitive stuff you do frequently?

I look forward to hearing back from you.

Akshay

Subscribe to Akshay's Blog

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe