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.
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
_postsdirectory. - 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}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 thetrmethod above? I wanted to learn how ActiveSupport implementsdasherizemethod and that's when I came across Ruby'strmethod, 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
---
<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
Sign up for my newsletter
Let's learn to become better developers.
Comments (2)
You could also use Rake :-) My "new post" rake task uses the following code: desc "Create a new post" task :new do |t| _, title = ARGV title ||= "New post" timestamp = Time.now.strftime("%Y-%m-%d") slug = I18n.transliterate title.downcase.gsub(/\s|'/, "-") filename = File.join(POSTS_DIR, "#{timestamp}-#{slug}.md") mkdir_p File.join(ASSETS_DIR, "img", "posts", slug) puts "==> Creating new post: #{filename}" open(filename, "w") do |f| f << "---\n" f << "title: \"#{title}\"\n" f << "excerpt: \n" f << "tags: []\n" f << "---\n" f << "\n" end sh "open #{filename}" exit end As you can see, I create a folder in img/posts with the post slug, where I put images associated with a blog post. sh "open #{filename}" => opens the newly-created file in my editor I use I18n.transliterate to convert accented letters (I blog in French and English), you'll need to add this to your rakefile if you want to do the same: require "i18n" I18n.available_locales = [:en] Have you tried using filters to wrap plain images with the required HTML? You could automatically add class, lazy-loading and srcset attribute… As an aside, I'm leaving Jekyll for Bridgetown (using erb) because as a programmer I find Liquid templates (smarty, nunjucks, and the like) to be a waste of time. Battling with a dumbed-down templating language when you know how to do something is very, very frustrating. Just try assigning variables from splitting a variable-length array using Liquid… Have you tried Bridgetown?
Hey Goulven, Thanks for your reply. I haven’t tried Rake yet. The above program solved all my problems, so didn’t think about improving/modifying it. Your solution looks interesting though, thanks for sharing. Regarding Bridgetown, I haven’t really found the need to switch away from Jekyll. Jekyll solves all my needs for blogging, and after using it for almost 10 years, it’s muscle-memory by now. Regarding liquid templates, I just have a theme that I’ve been using for many years and don’t have feel need to keep modifying it. Let me know how Bridgetown works for you, though. It’s interesting. Take care, Akshay