-
-
Save ruario/4ec4bc02e820a42830e9a8dc05b042e7 to your computer and use it in GitHub Desktop.
Wrap Gemtext to a set line length for use on Gopher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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