I've been tinkering with Zola the past few days. Learning how templating and theme-ing works. Also, I was curious how pages and sections behave. I started with very little expectations and was about to just go with the default setup, but something inside me tells me that I need to do what I think works best with how I intent to write a post in Zola.

I wrote a small Bash script that I put in my Zola root directory. It expect a certain directory structure. I was thinking that putting all pages in a single directory will mean harder to look for old pages if ever I need to make corrections later on. Imagine having a hundred pages and how much scrolling you will need to do to reach the bottom of the list. I don't think I'll reach that much but just thinkging ahead. Actually, the reason is that my text editor of choice - Vim - sort the files with newest at the bottom. And so, I wanted to make my Zola pages organized in folders based on year and month.

- content
---- 2024
------- 07
------------ 2024-07-15-hello.md
------------ 2024-07-16-whats-up.md
------- 08
------------ 2024-08-01-its-august.md

There might be a better way but this is the most logical way for me. My blog is very simple. Just one Zola section for all my writings. The script below will generate a basic Page. It's templated with most of the things need - title, slug, date, author, taxonomies and a read more shortcode.

The command is either sh newpost.sh or ./newpost.sh, depending if you made the script executable by doing chmod 0755 newpost.sh.

Create a page by simple typing running the script and the title

./newpost.sh Title of your entry

It does not matter if you capitalize each word or now as the script have a very simple logic to convert the words into title case. The script will throw an error if you try to use the same title again on the same day. The page will be created with the draft page config set to true. Just delete the line if you are ready to publish. Drafts can be viewed when you run zola serve --drafts.

Copy pasta the code below and write more blog entries. Enjoy.

#!/bin/bash

# newpost.sh

# show usage banner
if [ $# -eq 0 ] || [ ${1:0:1} == "-" ]
then
  echo "Usage: newpost.sh [title]"
  exit 1
fi

author=$(
  cat config.toml | \
  grep "^author" | \
  head -n 1 | \
  sed -r 's/author[[:space:]]*=[[:space:]]*\"([[:alnum:]]+)\"/\1/' \
)

if [ -z "${author}" ]
then
  echo "Error: No author found in config.toml."
  exit 1
fi

# titleize words
for word in "$@"
do
  title="${title:+"$title "}$(
    printf ${word:0:1} | \
    tr '[:lower:]' '[:upper:]' \
  )${word:1}"
done

# assign title to $@
set $title

# slugify the title
for word in "$@"
do
  slug="${slug:+"$slug-"}$(
    printf ${word} | \
    tr '_' '-' | \
    tr -dc '[:alnum:] '-'' | \
    tr '[:upper:]' '[:lower:]' \
  )"
done

date=$(date "+%Y-%m-%d")
target_dir="content"

# create directories if needed
set $(date "+%Y %m")

for segment in $@
do
  target_dir="${target_dir}/${segment}"

  if [ ! -d ${target_dir} ]
  then
    echo "Creating directory \"${target_dir}\" ..."
    mkdir -p "${target_dir}"
    cat > ${target_dir}/_index.md <<EOF
+++
transparent = true
+++
EOF
  fi
done

# build file path
file=${target_dir}/${date}-${slug}.md

# check if already exists, we don't want to overwrite old entries or drafts
if [ -e ${file} ]
then
  echo "Error: ${file} already exists."
  exit 1
fi

# create the page
echo "creating ${file} ..."
cat > ${file} <<EOF
+++
title = "${title}"
date = "$(date -Iseconds)"
slug = "${slug}"

# delete to publish
draft = true

[taxonomies]
tags = ["random"]

[extra]
author = "${author}"
+++

Write text here.

<!-- more -->
More text here.

EOF

echo "Done"