Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active January 10, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruario/4ec4bc02e820a42830e9a8dc05b042e7 to your computer and use it in GitHub Desktop.
Save ruario/4ec4bc02e820a42830e9a8dc05b042e7 to your computer and use it in GitHub Desktop.
Wrap Gemtext to a set line length for use on Gopher
#!/usr/bin/env bash
# To use, make it executable, then you pipe or redirect the Gemtext in.
#
# ./gmi2txt.sh < yourfile.gmi
LINE_LENGTH=70 # Adjust as you see fit ;)
PREFORMAT=OFF # This will get reset during processing.
while IFS= read -r line;
do
if [ "$PREFORMAT" = ON ]; then
case "$line" in
'```'*)
echo "$line"
PREFORMAT=OFF
;;
*)
echo "$line"
;;
esac
else
case "$line" in
'* '*) # Lists
echo "$line" | fmt -w "$(($LINE_LENGTH - 2))" | sed -E 's/^([^\*])/ \1/'
;;
'=>'*) # Links
echo "$line" | sed 's/^=>//' | fmt -w "$(($LINE_LENGTH - 2))" | sed 's/^/ /;1s/^ /=>/'
;;
'>'*) # Quote
echo "$line" | sed -E 's/^>[[:blank:]]?//' | fmt -w "$(($LINE_LENGTH - 2))" | sed 's/^/> /'
;;
'```'*) # Pre-formatted text
echo "$line"
PREFORMAT=ON
;;
*)
echo "$line" | fmt -w "$LINE_LENGTH"
;;
esac
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment