Skip to main content

Automating the blog making process

<time datetime="2022-11-11 10:17:33 &#43;0530 IST">11 November 2022</time><span class="px-2 text-primary-500">&middot;</span><span>290 words</span><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">2 mins</span><span class="px-2 text-primary-500">&middot;</span> <span class="mb-[2px]"> <a href="#/blog/Automating-Blog.md" class="text-lg hover:text-primary-500" rel="noopener noreferrer" target="_blank" title="Edit content" ><span class="relative inline-block align-text-bottom px-1 icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M490.3 40.4C512.2 62.27 512.2 97.73 490.3 119.6L460.3 149.7L362.3 51.72L392.4 21.66C414.3-.2135 449.7-.2135 471.6 21.66L490.3 40.4zM172.4 241.7L339.7 74.34L437.7 172.3L270.3 339.6C264.2 345.8 256.7 350.4 248.4 353.2L159.6 382.8C150.1 385.6 141.5 383.4 135 376.1C128.6 370.5 126.4 361 129.2 352.4L158.8 263.6C161.6 255.3 166.2 247.8 172.4 241.7V241.7zM192 63.1C209.7 63.1 224 78.33 224 95.1C224 113.7 209.7 127.1 192 127.1H96C78.33 127.1 64 142.3 64 159.1V416C64 433.7 78.33 448 96 448H352C369.7 448 384 433.7 384 416V319.1C384 302.3 398.3 287.1 416 287.1C433.7 287.1 448 302.3 448 319.1V416C448 469 405 512 352 512H96C42.98 512 0 469 0 416V159.1C0 106.1 42.98 63.1 96 63.1H192z"/></svg> </span></a > </span>

The problem #

Whenever I need to make a blog, i need to go in the hugo directory, make a new file with hugo new , and also update the repo after that.

I want a script that makes a blog every day (just a file in the blogs folder) and then gives the name and the title of the date.

The Script #

#!/bin/sh

hugo_project=$1
today_date=`date | cut -d' ' -f1-4 | tr ' ' '-'`
cd $hugo_project
hugo new blog/$today_date.md 2> /dev/null || notify-send "Blog Exists" "Exiting" && exit
git add content/blog/$today_date.md
git commit -m "Added Blog file for Date $today_date"
git push origin main

hugo
hugo -D

cd public
git add -A
git commit -m "Added Blog for date $today_date to website"
git push origin master

notify-send "Added New Blog" "Successfully Done"

How it works #

  • So the the script is pretty simple, it first takes the hugo repo path from the user, and gets the date from the simple date command.

  • Then it goes to the directory, makes a new file, and sends a notify-send to check if the file exists already. If it does, it exits out of the loop.

  • Then the git commands just add and commit and push the new file

  • Then hugo makes the project (hugo -D)

  • Then I go in the public directory which is my actual website, and add everything hugo has added, commit and push.

  • And yeah, sends a notify-send to let me know its done

Run this as a cron job every day at 10, and I got a blog Automating script!!

Update: #

Couldn’t get cron to work (I’m an idiot), so made a systemd script which runs new blog every day at 10, else on startup