Python index updater script

2022-02-07

I've whipped up a quick Python script to generate my gemlog index page. I was going to do this with shell script at first, since that seemed like the more appropriate way to efficiently deal with text files. But then I realized I know basically nothing about `sed` and `awk` which would be necessary. So, Python it is!

view the source code

In order for the script to work correctly, the user must stick to a specific file name format for entries. Every file in the `entries/` directory must follow this format: `YYYY-MM-DD_-_title_with_underscore_spaces.gmi`. This allows me to use just two simple functions to get a list of file names, with the latest date first. A specific format must also be adhered to for each entry. The title must be on the first line, and the date for the post on the 3rd line. Anything else, and you won't wind up with the correct information on the index links.

# get a list of files in `entries_path` in reverse order.
entries = sorted(os.listdir(entries_path), reverse=True)

Next, I use a shell command copy and overwrite the index file with a template. Effectively erasing any of the entries that were already linked on the index.

os.system(f'cp ./index.template.gmi {index_path})

Now the script iterates over each entry file. It gets the title for the post by reading the first line and stripping the newline and `#` symbol. Then the date is copied from the 3rd line with the same method. Following this, the index file is open in append mode. A gemtext link string is constructed with the path, the date, and the title, and then appended to the index file. Repeat the process for the rest of the files in the list.

I can see that this approach would not be ideal when dealing with a large number of files. The more files, the larger the `entries` list will get and the more memory it will occupy. But I plan on archiving the gemlog every year. So even if I write one entry every day for an entire year (not likely) the maximum number of entry files would be 365. That's small enough to stuff everything into a list at once, I think.

I will, of course update this in the future. I would especially like to make a tag system. Entries can have tags listed in them, and the same tags will be listed on the index. When a visitor clicks on a tag, say a GNU tag for example, It will generate a page with links to every entry with the GNU tag. But for now I'm satisfied with it as a very simple way to update the gemlog without having to manually type new links into the index.

Suggestions for improvement are always welcome.