It's the little things that help make things better

Just cleaned up some of the code that generates this blog. I'm using Pelican so one of the pain-points is creating a template for each new post. I have a script called "newpost.py" that does this. I also post a monthly check-in for my "Designing a Well Lived Life" challenge, which required me to import a new template each time.

One of my maxims is if I have to remember to do things more than once then it needs to be automated.

So I set about replacing the old crufty code (using argparse) with something that better suited my needs.

First off, I needed to have some way of passing the month / year for the title. I also needed a way to pass the location of the template and the title itself (which is true of all posts).

It's not pretty and will likely change in the future, but here's my first-pass at this:

design_life.sh

#!/bin/bash
TEMPLATE="/home/craig/Documents/zen_habits/designing_a_well_lived_life/2022_designing-a-well-lived-life_template.md"
newpost.py --titlemonth $1 --titleyear $2 --templatefile $TEMPLATE "Designing a Well Lived Life: Checking In ($1 $2)"

newpost.py

#!/usr/bin/env python3
import sys
import click
from datetime import datetime
from subprocess import call
from string import punctuation
from slugify import slugify

# apt install python3-slugify

TEMPLATE = """
Title: {title}
Date: {year}-{month}-{day} {hour}:{minute:02d}
Author: craig
Tags: A day in the life
Slug: {slug}
Status: published

"""


@click.command()
@click.argument('title')
@click.option('--titlemonth', default=None, help="Title month")
@click.option('--titleyear', default=None, help="Title year")
@click.option('--templatefile', default=None, type=click.File('r'), help="Template file to use")
def make_entry(title, titlemonth, titleyear, templatefile):
    title = title.strip().format(
        titlemonth=titlemonth,
        titleyear=titleyear)
    today = datetime.today()
    slug = slugify(title)
    f_create = "/home/craig/projects/pelican/decafbad/content/{}_{:0>2}_{:0>2}_{}.md".format(
        today.year, today.month, today.day, slug)
    template = TEMPLATE
    if templatefile is not None:
        template = templatefile.read()

    t = template.strip().format(
        title=title,
        year=today.year,
        month=today.month,
        day=today.day,
        hour=today.hour,
        minute=today.minute,
        titlemonth=titlemonth,
        titleyear=titleyear,
        slug=slug)
    with open(f_create, 'w') as w:
        w.write(t)
    print("File created -> " + f_create)
    call(['vim', f_create])


if __name__ == '__main__':
    make_entry()

With the new code I could make parameters for the items that I needed. The TEMPLATE="/home/craig/Documents/zen_habits/designing_a_well_lived_life/2022_designing-a-well-lived-life_template.md" file has the TEMPLATE part in the header so I don't have to repeat that with my normal TEMPLATE block.

This saves me a few keystrokes and makes the process easier for me to remember all of the items that normally would be in my mental checklist. And really, isn't that what computers are better at handling than humans?


links

social