Notebook word wrap

 

 

-- Wed Nov 23 17:41:25 EST 2022

 

Hello again, I just wrote a bash script that adds the newlines in the notebook on this page. (you can see it on the homepage) I did this because I wanted the notebook to be in monospace text, so the timestamps can line up. This gemini server is managed by bash scripts, I made the scripts to automatically make these posts, and add to the notebook. I will give an explanation, as I'm sure it will be helpful to someone.

 

The full file is:

#!/bin/bash

readarray entries < "$1"
for i in "${entries[@]}"
do
        IFS=$'\n' parts=( $(echo $i | sed -r 's/(^.*>> ) (.*$)/\1\n\2/g') )
        echo "${parts[0]} $(sed ':a;N;$!ba;s/\n/\n           ------------- >>  /g' <<< $(fold -sw40 <<< ${parts[1]}))"
done

Here is a breakdown:

 

readarray entries < "$1"

In the script that generates the gemini page, it calls this script and $1 is a text file with all of the notes. Readarray makes an array from the string, separated by each line.

 

for i in "${entries[@]}"

Loops through each entry. [@] means all, make sure there are curly braces {} and not parentheses.

 

IFS=$'\n' parts=( $(echo $i | sed -r 's/(^.*>> ) (.*$)/\1\n\2/g') )

Starting in the middle, with the sed command. The regex command:

's/(^.*>> ) (.*$)/\1\n\2/g'

separates the timestamp and the text after it. The echo command just gives sed the string. Then the output is saved to the `parts' array. IFS=$'

' just says that the parts array should be split by newline, instead of by spaces.

 

echo "${parts[0]} $(sed ':a;N;$!ba;s/\n/\n ------------- >> /g' <<< $(fold -sw40 <<< ${parts[1]}))"

This one is more complicated. ${parts[0]} writes out the timestamp. The first part of the sed command:

:a;N;$!ba

I found online to be able to replace a newline with a string.

The second part:

s/\n/\n ------------- >> /g

just replaces the newline character with a filler string, that is the same width as the timestamp.

 

fold -sw40 <<< ${parts[1]}

This does the actual word wrapping. It takes the string (${parts[1]}) and splits it if it is more than 40 characters long.

 

I use echo to output the text instead of writing it to a file because I use it as an input in the script that generates the notebook page.

 

back to gemlog

 

EOF