]> git.plutz.net Git - webtxt/blobdiff - index.cgi
switch to markdown
[webtxt] / index.cgi
index 04128e433dedce49468dd00d2a63fac5b3d8ad5f..3db279a222688073640145c8b1e629427668e9ce 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-_EXEC="${_EXEC:-.}"
+_EXEC="${_EXEC:-${0%/*}/}"
 _DATA="${_DATA:-.}"
 
 . "$_EXEC"/cgilite/cgilite.sh
@@ -8,6 +8,8 @@ _DATA="${_DATA:-.}"
 . "$_EXEC"/cgilite/file.sh
 . "$_EXEC"/session_lock.sh
 
+git init "$_DATA" >/dev/null
+
 yield_page(){
   page="$1"
   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
@@ -32,10 +34,13 @@ case ${PATH_INFO##*/} in
   webnote.css) FILE "$_EXEC/webnote.css";;
   '') yield_page <<-EOF
        [form .new action=new [button type=submit New Note]
-         [ul .recent
-         $({ COOKIE pages; echo; } |tr \  \\n |while read page; do
-           [ "$(printf %s "$page" |checkid)" ] && printf '[li [a href="./%s" . %s]]' "$page" "$page"
-         done)]
+          $(if [ "$(COOKIE pages)" ]; then
+           printf '[h2 Recent Pages][ul .recent'
+           for page in $(COOKIE pages); do
+             [ -f "$_DATA/$(printf %s "$page" |checkid)" ] && printf '[li [a href="./%s" . %s]]' "$page" "$page"
+           done
+            printf ']'
+         fi)
        ]
        EOF
     return 0
@@ -43,6 +48,8 @@ case ${PATH_INFO##*/} in
   new)
     newid="$(timeid)"
     touch "$_DATA/$newid"
+    git -C "$_DATA" add "$newid" >/dev/null
+    git -C "$_DATA" commit -m "new note \"${newid}\"" "$newid" >/dev/null
     REDIRECT "./$newid"
     ;;
 esac
@@ -50,7 +57,7 @@ esac
 doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)"
 
 if [ ! -f "$doc" ]; then
-  REDIRECT "${PATH_INFO%%/*}/"
+  REDIRECT "${PATH_INFO%/*}/"
   return 0
 fi
 
@@ -80,6 +87,7 @@ fi
     if temp=$(CHECK_SLOCK "$doc"); then
       RELEASE_SLOCK "$doc"
       POST document >"${doc}"
+      git -C "$_DATA" commit -m "update note ${doc##*/} \"$(sed '1{s;\r;;; q;}' "$doc")\"" "${doc##*/}" >/dev/null
       REDIRECT "$PATH_INFO"
     else
       yield_page <<-EOF
@@ -93,14 +101,12 @@ fi
 esac
 
 COOKIE pages |tr \  \\n |grep -qF "${doc##*/}" \
-|| SET_COOKIE +$((90 * 86400)) pages="$(COOKIE pages && printf ' %s' "${doc##*/}" || printf '%s' "${doc##*/}")"
+|| SET_COOKIE +$((90 * 86400)) pages="$(COOKIE pages && printf ' %s' "${doc##*/}" || printf '%s' "${doc##*/}")" Path="${PATH_INFO%/*}/"
 
 yield_page <<-EOF
        [form method=POST
           [input type=hidden name=session_key value="$SESSION_KEY"]
          [button type=submit name=action value=edit Edit]
        ]
-       [div .text
-       $(HTML <"$doc")
-       ]
+       [div .text . $(markdown <"$doc")]
        EOF