site

My personal website.

Date: Sun, 11 Jul 2021 23:46:04 +0200

Add html special character escaping to generate

Diffstat:

M generate                            |      23 +++++++++++++++--------

1 file changed, 15 insertions(+), 8 deletions(-)

@@ -8,6 +8,11 @@ dir="$2"
 [ $output = "html" ] && extension="html"
 [ $output = "gopher" ] && extension="gph"
 
+escape() {
+    [ $output = "html" ] && printf "%s" "$1" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
+    [ $output = "gopher" ] && printf "%s" "$1"
+}
+
 rm -rf "$dir"
 cp -rf . "$dir"
 files="$(find "$dir" -name '*.gmi')"
@@ -24,6 +29,7 @@ while read -r file <&4; do
                     [ $output = "html" ] && echo "</pre></code>"
                     ;;
                 *)
+                    line="$(escape "$line")"
                     if [ "$preformatted" = "first" ]; then
                         printf "%s" "$line"
                         preformatted="yes"
@@ -44,30 +50,30 @@ while read -r file <&4; do
             "### "*)
                 [ -n "$end" ] && echo "$end"
                 end=""
-                line="$(printf "$line" | cut -c 5- | tr -d '\n')"
+                line="$(escape "$line" | cut -c 5- | tr -d '\n')"
                 [ $output = "html" ] && printf "<h3>%s</h3>\n" "$line"
                 [ $output = "gopher" ] && printf "i### %s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;
             "## "*)
                 [ -n "$end" ] && echo "$end"
                 end=""
-                line="$(printf "$line" | cut -c 4- | tr -d '\n')"
+                line="$(escape "$line" | cut -c 4- | tr -d '\n')"
                 [ $output = "html" ] && printf "<h2>%s</h2>\n" "$line"
                 [ $output = "gopher" ] && printf "i## %s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;
             "# "*)
                 [ -n "$end" ] && echo "$end"
                 end=""
-                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
                 [ $output = "html" ] && printf "<h1>%s</h1>\n" "$line"
                 [ $output = "gopher" ] && printf "i# %s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;
             "=>"*)
                 [ -n "$end" ] && echo "$end"
                 end=""
-                line="$(printf "%s" "$line" | cut -c 3- | tr -d '\n')"
-                url="$(printf "%s" "$line" | awk '{print $1}')"
-                text="$(printf "%s" "$line" | awk '{$1="";print}' | awk '{$1=$1;print}')"
+                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
+                url="$(escape "$line" | awk '{print $1}')"
+                text="$(escape "$line" | awk '{$1="";print}' | awk '{$1=$1;print}')"
                 [ -z "$text" ] && text="$url"
                 if [ $output = "html" ]; then
                     printf "%s\n" "<p><a href=\"$url\">$text</a></p>"
@@ -112,7 +118,7 @@ while read -r file <&4; do
             "> "*)
                 [ -n "$end" ] && echo "$end"
                 end=""
-                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
                 [ $output = "html" ] && printf "<blockquote>%s</blockquote>\n" "$line"
                 [ $output = "gopher" ] && printf "i    %s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;
@@ -121,13 +127,14 @@ while read -r file <&4; do
                     echo "<ul>"
                     end="</ul>"
                 fi
-                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
                 [ $output = "html" ] && printf "<li>%s</li>\n" "$line"
                 [ $output = "gopher" ] && printf "i* %s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;
             *)
                 [ -n "$end" ] && echo "$end"
                 end=""
+                line="$(escape "$line")"
                 [ $output = "html" ] && printf "<p>%s</p>\n" "$line"
                 [ $output = "gopher" ] && printf "i%s\tfake\t(NULL)\t0\r\n" "$line"
                 ;;