From 12556f1bbca0a2a973c3cb305b191f3844f197cc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 18 May 2022 22:02:09 +0200 Subject: [PATCH] newpage macro, allow template in editor --- handlers/30_newpage.sh | 59 ++++++++++++++++++++++++++++++++++++++++++ macros/newpage | 29 +++++++++++++++++++++ themes/default.sh | 9 ++++--- 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100755 handlers/30_newpage.sh create mode 100755 macros/newpage diff --git a/handlers/30_newpage.sh b/handlers/30_newpage.sh new file mode 100755 index 0000000..ad9f7eb --- /dev/null +++ b/handlers/30_newpage.sh @@ -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 index 0000000..2502b00 --- /dev/null +++ b/macros/newpage @@ -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 +
+ + + $([ ! "${pattern##*%%s*}" ] \ + && printf '' + ) +
+ EOF +fi diff --git a/themes/default.sh b/themes/default.sh index 0d60fb3..3c5f2a1 100755 --- a/themes/default.sh +++ b/themes/default.sh @@ -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(){ $(HTML "${title}") $(theme_header) -
+
- +
-- 2.39.2