diff options
author | Steph Enders <steph@senders.io> | 2023-06-29 12:53:18 -0400 |
---|---|---|
committer | Steph Enders <steph@senders.io> | 2023-06-29 12:54:39 -0400 |
commit | 93b8f8ef5245852f3f8cedb82c8d65f3b67c7c54 (patch) | |
tree | f767d5806e9eb8b0b707656d25cbe0ca8aa5ce84 |
Create gmi to html converter script
The usage is fairly simple just do ./gmi2html.sh FILE
I would recommend using tidy on the final product to format things but a
.gmi file is basically tree-less so its no big deal.
-rwxr-xr-x | gmi2html.sh | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/gmi2html.sh b/gmi2html.sh new file mode 100755 index 0000000..165e4c3 --- /dev/null +++ b/gmi2html.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env sh + +function err { + echo "$@" >&2 +} + +if [ $# -ne 1 ]; then + err "./gmi2html.sh /path/to/gmi" + exit 1; +fi + +# main method +GMI=$1 +list=0 +pre=0 +quote=0 + +while read line; do + # pre formatted text + if [ -n "$(sed -nE '/^[`]{3}/p' <<< $line)" ]; then + if [ $pre -eq 0 ]; then + echo "<pre>" + pre=1 + else + echo "</pre>" + pre=0 + fi + continue; + fi + if [ $pre -eq 1 ]; then + echo "$line" + continue; + fi + + # lists + if [ -n "$(sed -nE '/^[*] .+/p' <<< "$line")" ]; then + if [ $list -eq 0 ]; then + echo "<ul>" + fi + list=1; + text=$(sed -E 's/^[*] (.+)$/\1/g' <<< "$line") + echo "<li>${text}</li>" + continue + else + if [ $list -eq 1 ]; then + echo "</ul>" + fi + list=0; + fi + + # quotes + if [ -n "$(sed -nE '/^[>] .+/p' <<< $line)" ]; then + if [ $quote -eq 0 ]; then + echo "<figure><blockquote>" + fi + quote=1; + text=$(sed -E 's/^[>] (.+)$/\1/g' <<< $line) + echo "$text" + continue + else + if [ $quote -eq 1 ]; then + echo "</blockquote></figure>" + fi + quote=0; + fi + + # single line modifiers + if [ -n "$(sed -nE '/^# .+/p' <<< $line)" ]; then + text=$(sed -E 's/^# (.+)$/\1/g' <<< $line) + echo "<h1>$text</h1>" + continue + fi + if [ -n "$(sed -nE '/^## .+/p' <<< $line)" ]; then + text=$(sed -E 's/^## (.+)$/\1/g' <<< $line) + echo "<h2>$text</h2>" + continue + fi + if [ -n "$(sed -nE '/^### .+/p' <<< $line)" ]; then + text=$(sed -E 's/^### (.+)$/\1/g' <<< $line) + echo "<h3>$text</h3>" + continue + fi + if [ -n "$(sed -nE '/^=> .+/p' <<< "$line")" ]; then + href=$(sed -E 's/^=> ([^ ]+)([ ]?.*)/\1/g' <<< "$line") + if [ -n "$(sed -nE '/^=> [^ ]+ .+$/p' <<< "$line")" ]; then + text=$(sed -E 's/^=> [^ ]+ (.+)$/\1/g' <<< "$line") + else + text=$href + fi + + echo "<p><a href='${href}' rel='noopener' target='_blank'>$text</a></p>" + continue + fi + + # skip blank lines + if [ -z "$line" ]; then + continue + fi + # default paragraphs + echo "<p>$line</p>" +done < $GMI |