Afterschool Activities

Let's return home!

Neocities is great. I get to play around with raw HTML, unfiltered through many design tools — it brings me back to a simpler time where all you need is one website tinted in blue (and somehow cyan, but we won't talk about that).

However, because of how it works, you can't quite put in 'dynamic' content in Neocities (unless you become a supporter, I suppose). This means you have to update some sort of index by hand if you want to make this a 'blog' site.

"Fear not!" my brain spoke as I was editing my index file, "for there is light in sed and awk!"

Oh, right! We can do this automatically (locally, at least) with shell scripts!

Through the afternoon, I came up with this nifty little script:
#!/bin/sh
# append_to_file
# Usage: SOURCE, APPENDED_FILE, OUTPUT
append_to_file() {
	source="$1"
	append="$2"
	output="$3"
	awk -v source="$source" '/###POST-TAG###/ {while (getline line < source){ print line }; next} 1' "$append" > "$output"
}

## Generates an index.html that indexes the 'posts' folder.
generate_index() {
	echo "Retrieving post list..."
	tree -t -P "*.html" posts --noreport | sed -e "s/└/
└/g" | sed -e "s/├─/
├─/g" | sed -e "s|[^ ]*.html|\0|g" > temp.txt echo "Generating new index file..." append_to_file "temp.txt" "templates/index.html" "index.html" rm temp.txt echo "Done!" }
That's a lot of code! Let's break it down to digest the spaghetti.

The code does the following:
	- Retrieve a list of files in the 'posts' folder through the 'tree' command.
	- The content of the output is then glued over to an HTML file containing '###POST-TAG###' — essentially, you'll be replacing the said tag with your file lists.
	- Generates a brand new index.html to upload in Neocities.

This isn't the most magical code I have ever written, but it sure beats manually editing the index file whenever I upload some new post.

You will also need to contain your posts (in HTML form) in a 'posts' folder, both locally and remotely in Neocities.

So now, whenever I decide to upload a new post, I would generate the post and the index — and voila!


Last updated: Friday, 28 June 2019