lsgmi - create index links of Gemini posts
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
- A post is a file with the ending .gmi and with at least 1 line that contains only "NNNN-NN-NN" (with N being decimal digits).
- The last such line represents the date of the post.
- The title of a file is its first line minus the first 2 characters (typically # and a space).
- The prefix given by a directory is the title of the index.gmi file in the directory (if present).
- The immediate prefix of a post is the prefix given by the directory that immediately contains the post (but only if it is not the current directory).
- The lowest prefix of a post is the prefix given by the lowest subdirectory of the current directory that contains the post (not necessarily immediately).
- The highest prefix of a post is the prefix given by the highest subdirectory of the current directory that contains the post (not necessarily immediately).
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:
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.