]> git.plutz.net Git - shellwiki/commitdiff
newpage macro, allow template in editor
authorPaul Hänsch <paul@plutz.net>
Wed, 18 May 2022 20:02:09 +0000 (22:02 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 18 May 2022 20:02:09 +0000 (22:02 +0200)
handlers/30_newpage.sh [new file with mode: 0755]
macros/newpage [new file with mode: 0755]
themes/default.sh

diff --git a/handlers/30_newpage.sh b/handlers/30_newpage.sh
new file mode 100755 (executable)
index 0000000..ad9f7eb
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+case $PATH_INFO in
+  */\[newpage\]):;;
+  *) return 1;;
+esac
+
+if [ "$(POST action)" != newpage ]; then
+  printf 'Refresh: %i; url=%s\r\n' 4 ./
+  export ERROR_MSG="Formdata invalid"
+  theme_error 400
+  return 0
+fi
+
+pattern="$(POST pattern)"
+template="$(POST template)"
+page="$(POST page)"
+
+if [ "$page" ]; then
+  pattern="$(date +"$pattern")"
+  page="$(printf -- "$pattern" "$page")"
+else
+  printf 'Refresh: %i; url=%s\r\n' 4 ./
+  export ERROR_MSG="Page name required"
+  theme_error 400
+  return 0
+fi
+
+page="$(page_abs "$page")"
+[ "$template" ] \
+&& template="$(page_abs "$template")" \
+|| template="$page"
+
+if [ -f "$_DATA/pages/$page/#page.md" -o \
+     -f "$_EXEC/pages/$page/#page.md" ]; then
+  printf 'Refresh: %i; url=%s\r\n' 4 ./
+  export ERROR_MSG="Page exists already"
+  theme_error 409
+  return 0
+
+elif ! acl_write "$page"; then
+  printf 'Refresh: %i; url=%s\r\n' 4 ./
+  export ERROR_MSG="You don't have permission to write to this page"
+  theme_error 403
+  return 0
+
+elif mkdir -p -- "$_DATA/pages/${page}" \
+     && S_LOCK "$_DATA/pages/$page/#page.md"; then
+  theme_editor "$page" "$template"
+  return 0
+
+else
+  printf 'Refresh: %i; url=%s\r\n' 4 ./
+  export ERROR_MSG="Unable to lock page for editing"
+  theme_error 409
+  return 0
+fi
+
+return 1
diff --git a/macros/newpage b/macros/newpage
new file mode 100755 (executable)
index 0000000..2502b00
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+. "$_EXEC/cgilite/cgilite.sh"
+. "$_EXEC/acl.sh"
+. "$_EXEC/tools.sh"
+
+pattern=./%%s
+template=''
+label='New Page'
+
+while [ $# -gt 0 ]; do case $1 in
+  template=*) template="${1#*=}"; shift 1;;
+  --template) template="$2"; shift 2;;
+  label=*) label="${1#*=}"; shift 1;;
+  --label) label="$2"; shift 2;;
+  *) pattern="$1"; shift 1;;
+esac; done
+
+if acl_write "$(page_abs "$pattern")"; then
+  cat <<-EOF
+       <form class="macro newpage" method=POST action="[newpage]">
+         <input type=hidden name=pattern value="$(HTML "$pattern")">
+         <input type=hidden name=template value="$(HTML "$template")">
+         $([ ! "${pattern##*%%s*}" ] \
+           && printf '<input name=page placeholder="page name">'
+         )<button type=submit name=action value=newpage>$(HTML "$label")</button>
+       </form>
+       EOF
+fi
index 0d60fb34a4144846925a50dbc54ebd716381e602..3c5f2a1819a000277c06dc31451a2013f1335812 100755 (executable)
@@ -57,7 +57,7 @@ theme_page(){
 }
 
 theme_editor(){
-  local page="$1" title
+  local page="$1" template="$2" title
   title="${page%/}"; title="${title##*/}"
 
   if ! acl_write "$page"; then
@@ -65,6 +65,8 @@ theme_editor(){
     return 0
   fi
 
+  [ "$template" ] && acl_read "$template" || template="$page"
+
   # Important! Web Server response including newline
   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
 
@@ -75,9 +77,10 @@ theme_editor(){
          <title>$(HTML "${title}")</title>
        </head><body id="$(HTML "$page")" class="editor">
          $(theme_header)
-         <main><form method=POST>
+         <main><form method=POST action="$(HTML "${page%/}/[edit]")">
            <input type=hidden name=session_key value="${SESSION_KEY}"/>
-            <textarea name=pagetext>$(wiki_text "$page" |HTML)</textarea>
+            <textarea name=pagetext>$({ wiki_text "$page" \
+                                     || wiki_text "$template"; } |HTML)</textarea>
             <button type=submit name=action value=update>Update</button>
             <button type=submit name=action value=cancel>Cancel</button>
           </form></main>