Added icon and improved template code

This commit is contained in:
Ben Ashton 2022-09-25 16:53:41 -06:00
parent fd59f9bf19
commit 4de568ae77
3 changed files with 125 additions and 111 deletions

BIN
images/food.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

2
images/food.svg Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 122.88 115.21" style="enable-background:new 0 0 122.88 115.21" xml:space="preserve"><g><path d="M29.03,100.46l20.79-25.21l9.51,12.13L41,110.69C33.98,119.61,20.99,110.21,29.03,100.46L29.03,100.46z M53.31,43.05 c1.98-6.46,1.07-11.98-6.37-20.18L28.76,1c-2.58-3.03-8.66,1.42-6.12,5.09L37.18,24c2.75,3.34-2.36,7.76-5.2,4.32L16.94,9.8 c-2.8-3.21-8.59,1.03-5.66,4.7c4.24,5.1,10.8,13.43,15.04,18.53c2.94,2.99-1.53,7.42-4.43,3.69L6.96,18.32 c-2.19-2.38-5.77-0.9-6.72,1.88c-1.02,2.97,1.49,5.14,3.2,7.34L20.1,49.06c5.17,5.99,10.95,9.54,17.67,7.53 c1.03-0.31,2.29-0.94,3.64-1.77l44.76,57.78c2.41,3.11,7.06,3.44,10.08,0.93l0.69-0.57c3.4-2.83,3.95-8,1.04-11.34L50.58,47.16 C51.96,45.62,52.97,44.16,53.31,43.05L53.31,43.05z M65.98,55.65l7.37-8.94C63.87,23.21,99-8.11,116.03,6.29 C136.72,23.8,105.97,66,84.36,55.57l-8.73,11.09L65.98,55.65L65.98,55.65z"/></g></svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

202
index.n0m
View File

@ -2,30 +2,37 @@
$ #!/bin/bash $ #!/bin/bash
Content-Type: text/html Content-Type: text/html
<{
# Constants # Constants
# ========= # =========
$ true=1; true=1;
$ false=0; false=0;
#
$ restaurant_file="restaurants.txt"; restaurant_file="restaurants.txt";
$ title="FeedMe"; title="FeedMe";
#
# Components # Components
# ========== # ==========
#
# $1: string: input value # $1: string: input value
# $2: number: 0=existing restaurant, 1=new restaurant # $2: number: 0=existing restaurant, 1=new restaurant
$ restaurant_form () { restaurant_form () {
$ enforce_arguments "${FUNCNAME[0]}" "$#" 2; enforce_arguments "${FUNCNAME[0]}" "$#" 2;
$ local icon_class; local icon_class;
$ if [ "$2" -eq 0 ]; then if [ "$2" -eq 0 ]; then
$ icon_class="ri-save-2-fill"; icon_class="ri-save-2-fill";
$ else else
$ icon_class="ri-add-line"; icon_class="ri-add-line";
$ fi fi
}>
<form class="restaurant-form" method="POST" action=""> <form class="restaurant-form" method="POST" action="">
<input type="hidden" name="old" value="<% $1 %>" /> <input type="hidden" name="old" value="<% $1 %>" />
<input type="text" placeholder="Restaurant" name="new" value="<% $1 %>" /> <input
type="text"
placeholder="Restaurant"
name="new"
value="<% $1 %>"
/>
<label class="button <% $icon_class %>"> <label class="button <% $icon_class %>">
$ local action; $ local action;
$ if [ "$2" -eq 0 ]; then $ if [ "$2" -eq 0 ]; then
@ -41,105 +48,110 @@ $ fi
</label> </label>
$ fi $ fi
</form> </form>
$ } <{
# }
# Utility Functions # Utility Functions
# ================= # =================
#
# $1: string: function name # $1: string: function name
# $2: number: $# # $2: number: $#
# $3: number: expected number of arguments # $3: number: expected number of arguments
$ enforce_arguments () { enforce_arguments () {
$ if [ "$2" -lt "$3" ]; then if [ "$2" -lt "$3" ]; then
$ printf '%s %s %s\n' 'Function:' "$1" 'called with too few arguments'; printf '%s %s %s\n' 'Function:' "$1" 'called with too few arguments';
$ exit 1; exit 1;
$ elif [ "$2" -gt "$3" ]; then elif [ "$2" -gt "$3" ]; then
$ printf '%s %s %s\n' 'Function:' "$1" 'called with too many arguments'; printf '%s %s %s\n' 'Function:' "$1" 'called with too many arguments';
$ exit 1; exit 1;
$ fi; fi;
$ } }
#
$ decode_uri () { decode_uri () {
$ local i="${*//+/ }"; local i="${*//+/ }";
$ echo -e "${i//%/\\x}"; echo -e "${i//%/\\x}";
$ } }
#
#
# Restaurant List Functions # Restaurant List Functions
# ========================= # =========================
#
# $1: string: restaurant to remove # $1: string: restaurant to remove
$ remove_restaurant () { remove_restaurant () {
$ enforce_arguments "${FUNCNAME[0]}" "$#" 1; enforce_arguments "${FUNCNAME[0]}" "$#" 1;
$ local temp_file; local temp_file;
$ temp_file=$(mktemp); temp_file=$(mktemp);
$ grep -Fxv "$1" "$restaurant_file" > "$temp_file"; grep -Fxv "$1" "$restaurant_file" > "$temp_file";
$ mv "$temp_file" "$restaurant_file"; mv "$temp_file" "$restaurant_file";
$ } }
#
# $1: string: restuarant to add # $1: string: restuarant to add
$ add_restaurant () { add_restaurant () {
$ enforce_arguments "${FUNCNAME[0]}" "$#" 1; enforce_arguments "${FUNCNAME[0]}" "$#" 1;
$ local temp_file; local temp_file;
$ temp_file=$(mktemp); temp_file=$(mktemp);
$ (grep -Fxv "$1" "$restaurant_file"; printf '%s\n' "$1") > "$temp_file"; (grep -Fxv "$1" "$restaurant_file"; printf '%s\n' "$1") > "$temp_file";
$ mv "$temp_file" "$restaurant_file"; mv "$temp_file" "$restaurant_file";
$ } }
#
# $1 (optional): number: 0=don't overwrite, 1=overwrite # $1 (optional): number: 0=don't overwrite, 1=overwrite
$ todays_restaurant () { todays_restaurant () {
$ local today; local today;
$ today="$(date +'%Y%m%d')"; today="$(date +'%Y%m%d')";
$ local filename="$today.pick"; local filename="$today.pick";
$ if [ -n "$1" ] && [ "$1" -ne 0 ]; then if [ -n "$1" ] && [ "$1" -ne 0 ]; then
$ rm "$filename"; rm "$filename";
$ fi; fi;
$ if [ ! -f "$filename" ]; then if [ ! -f "$filename" ]; then
$ shuf -n 1 "$restaurant_file" > "$filename"; shuf -n 1 "$restaurant_file" > "$filename";
$ fi; fi;
$ cat "$filename"; cat "$filename";
$ } }
#
# Initialization # Initialization
# ============== # ==============
#
# Make sure restaurant file eixsts # Make sure restaurant file eixsts
$ touch -a "$restaurant_file"; touch -a "$restaurant_file";
#
# Parse body # Parse body
# ---------- # ----------
$ declare -A body; declare -A body;
$ while IFS= read -d '&' -r pair || [ "$pair" ]; do while IFS= read -d '&' -r pair || [ "$pair" ]; do
$ name=$(decode_uri "${pair%%=*}"); name=$(decode_uri "${pair%%=*}");
$ value=$(decode_uri "${pair#*=}"); value=$(decode_uri "${pair#*=}");
$ if [ -n "$name" ]; then if [ -n "$name" ]; then
$ body["$name"]="$value"; body["$name"]="$value";
$ fi; fi;
$ done #> done
#
# Perform requested action # Perform requested action
# ------------------------ # ------------------------
$ case "${body['action']}" in case "${body['action']}" in
$ add) add)
$ add_restaurant "${body['new']}"; add_restaurant "${body['new']}";
$ ;; ;;
$ delete) delete)
$ remove_restaurant "${body['old']}"; remove_restaurant "${body['old']}";
$ ;; ;;
$ save) save)
$ remove_restaurant "${body['old']}"; remove_restaurant "${body['old']}";
$ add_restaurant "${body['new']}"; add_restaurant "${body['new']}";
$ ;; ;;
$ new_restaurant) new_restaurant)
$ todays_restaurant $true >/dev/null; todays_restaurant $true >/dev/null;
$ ;; ;;
$ esac esac
#
# }><!doctype html>
<!doctype html>
<html> <html>
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="FeedMe">
<meta property="og:type" content="website">
<meta property="og:url" content="https://n0m.org/feedme">
<meta property="og:image" content="https://n0m.org/feedme/images/food.png">
<meta property="og:description" content="Today's Restaurant: <$% todays_restaurant %>">
<title><% $title %></title> <title><% $title %></title>
<style> <style>
:root { :root {