lsgmi - create index links of Gemini posts

lsgmi.sh

Suppose you are editing an index.gmi file and you want to link to all posts in the current directory and below, like so:

This script does that.

Definitions

Behavior

The script recursively looks for all posts in the current directory and below. The script lists them in the format given above: pathnames are padded with spaces and left-aligned. The output is printed to stdout. (This means that in GNU nano, for example, you can insert the output with ^T.)

The posts are sorted by their dates from newest to oldest. Posts with the same date are sorted by their pathnames in reverse order (Z to A).

If specified and if the prefix exists, the title of a post is preceded by the prefix of the post, a colon, and a space in the listing. The script accepts an optional argument to specify which prefix should be printed: "i" or "immediate", "l" or "lowesŧ", "h" or "highest", or "n" or "none". Default is "highest".

Caveats

The script doesn't check whether pathnames are sane. There can be problems with pathnames that contain whitespace, for example.

The script also doesn't check whether the first line of a file actually begins with # and a space when a title is fetched.

Tips

Suppose you append a line with the current date every time you edit a post. If you want to list (and sort by) the first (publication) date only, you just have to change "tail" to "head" in the pipeline that assigns the date in "ls_date_path". You can also insert "sort" before tail (or head) in the pipeline if you want to make sure to have the newest (or oldest, respectively) date, independent of their order in the file.

Note that if you don't want to list a file with a line that contains only a date, you have to escape the date somehow (e.g. by adding characters to the line).

Update

In 2023-01, I wrote the following modification of the script:

lsgmi2.sh

It contains more verbose comments than the original script, but it behaves differently. I will not cover the differences here because I no longer use any of the 2 scripts regularly.