]> git.plutz.net Git - shellwiki/commitdiff
Merge commit 'cfc3dbcd2e724953a001d0ad189df08072295d62'
authorPaul Hänsch <paul@plutz.net>
Tue, 15 Mar 2022 14:52:06 +0000 (15:52 +0100)
committerPaul Hänsch <paul@plutz.net>
Tue, 15 Mar 2022 14:52:06 +0000 (15:52 +0100)
25 files changed:
Makefile [new file with mode: 0644]
cgilite/.gitignore [moved from .gitignore with 100% similarity]
cgilite/cgilite.sh [moved from cgilite.sh with 100% similarity]
cgilite/common.css [moved from common.css with 100% similarity]
cgilite/file.sh [moved from file.sh with 100% similarity]
cgilite/html-sh.sed [moved from html-sh.sed with 100% similarity]
cgilite/logging.sh [moved from logging.sh with 100% similarity]
cgilite/markdown.awk [moved from markdown.awk with 100% similarity]
cgilite/session.sh [moved from session.sh with 100% similarity]
cgilite/storage.sh [moved from storage.sh with 100% similarity]
cgilite/users.sh [moved from users.sh with 100% similarity]
index.cgi [new file with mode: 0755]
macros/toc [new file with mode: 0755]
macros/wikiform [new file with mode: 0755]
md_macros.awk [new file with mode: 0755]
page_edit.sh [new file with mode: 0755]
pages/#attachments/favicon.ico [new file with mode: 0644]
pages/#page.md [new file with mode: 0644]
pages/[wiki]/404/#page.md [new file with mode: 0644]
pages/[wiki]/footer/#page.md [new file with mode: 0644]
pages/[wiki]/header/#page.md [new file with mode: 0644]
pages/[wiki]/login/#page.md [new file with mode: 0644]
pages/[wiki]/register/#page.md [new file with mode: 0644]
session_lock.sh [new file with mode: 0755]
themes/default.sh [new file with mode: 0755]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..24781a9
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+.PHONY: _subtrees
+
+_subtrees: _cgilite
+
+cgilite:
+       git subtree add --squash -P $@ https://git.plutz.net/git/$@ master
+
+_cgilite: cgilite
+       git subtree pull --squash -P $< https://git.plutz.net/git/$< master
similarity index 100%
rename from .gitignore
rename to cgilite/.gitignore
similarity index 100%
rename from cgilite.sh
rename to cgilite/cgilite.sh
similarity index 100%
rename from common.css
rename to cgilite/common.css
similarity index 100%
rename from file.sh
rename to cgilite/file.sh
similarity index 100%
rename from html-sh.sed
rename to cgilite/html-sh.sed
similarity index 100%
rename from logging.sh
rename to cgilite/logging.sh
similarity index 100%
rename from markdown.awk
rename to cgilite/markdown.awk
similarity index 100%
rename from session.sh
rename to cgilite/session.sh
similarity index 100%
rename from storage.sh
rename to cgilite/storage.sh
similarity index 100%
rename from users.sh
rename to cgilite/users.sh
diff --git a/index.cgi b/index.cgi
new file mode 100755 (executable)
index 0000000..2d4954f
--- /dev/null
+++ b/index.cgi
@@ -0,0 +1,113 @@
+#!/bin/sh
+
+. "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
+set -- nocookie
+. "${_EXEC}/cgilite/session.sh"
+. "${_EXEC}/cgilite/file.sh"
+. "${_EXEC}/cgilite/users.sh"
+
+. "${_EXEC}/themes/default.sh"
+
+CACHE_AGE=${CACHE_AGE:-1800}
+export MD_MACROS="$_EXEC/macros"
+export MD_HTML="${MD_HTML:-false}"
+
+if [ "$(which awk)" ]; then
+  md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+elif [ "$(which busybox)" ]; then
+  md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+else
+  md() { cat; }
+fi
+
+wiki_text() {
+  # Print source text of a wiki page
+  # Get page from data or underlay dir
+  local page="$(PATH "$1")"
+
+  if [ -f "$_DATA/pages/$page/#page.md" ]; then
+    cat -- "$_DATA/pages/$page/#page.md"
+  elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
+    cat -- "$_EXEC/pages/$page/#page.md"
+  else
+    return 1
+  fi
+}
+
+wiki() {
+  # Print content of a wiki page
+  # Get page from data or underlay dir, handle caching
+  local page="$(PATH "$1")" md cache cachetime
+
+  cache="$_DATA/pages/$page/#page.cache"
+  if [ -f "$_DATA/pages/$page/#page.md" ]; then
+    md="$_DATA/pages/$page/#page.md"
+  elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
+    md="$_EXEC/pages/$page/#page.md"
+  else
+    return 1
+  fi
+
+  cachetime="$(stat -c %Y -- "$md" "$cache" 2>/dev/null)"
+
+  if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
+    -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
+    cat "${cache}"
+  else
+    mkdir -p -- "$_DATA/pages/$page/"
+    # Macros expect to find page directory as working dir
+    ( cd -- "$_DATA/pages/$page/";
+      md <"$md" |tee -- "${cache}.$$"
+    )
+    mv -- "${cache}.$$" "${cache}"
+  fi
+}
+
+attachment() {
+  local file="$(PATH "$1")"
+
+  # TODO: deliver downscaled images, etc.
+  if [ -f "$_DATA/pages/${file%/*}/#attachments/${file#*/}" ]; then
+    FILE "$_DATA/pages/${file%/*}/#attachments/${file#*/}"
+  elif [ -f "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" ]; then
+    FILE "$_EXEC/pages/${file%/*}/#attachments/${file#*/}"
+  elif [ -d "$_DATA/pages/${file}/" -o -d "$_EXEC/pages/${file}" ]; then
+    # path looks like a rogue page name (without trailing slash), so redirect
+    REDIRECT "$_BASE/${file}/"
+  else
+    return 1
+  fi
+}
+
+. "$_EXEC/page_edit.sh"
+
+case "${PATH_INFO}" in
+  /"[.]"/*)
+    FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
+    ;;
+  */)
+    if [ -f "$_DATA/pages/$PATH_INFO/#page.md" \
+      -o -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then
+      theme_page "${PATH_INFO}"
+    else
+      theme_404
+    fi
+    ;;
+  */"[login]")
+    [ "$USER_NAME" ] \
+    && REDIRECT "./" \
+    || theme_login
+    ;;
+  */"[register]")
+    theme_register
+    ;;
+  */"[attachment]"/*)
+    :  # TODO: Original attachment file
+    ;;
+  */"#"*)
+    :  # TODO: Invalid page name
+    ;;
+  *)
+    attachment "${PATH_INFO}"
+    ;;
+esac
diff --git a/macros/toc b/macros/toc
new file mode 100755 (executable)
index 0000000..0d708a4
--- /dev/null
@@ -0,0 +1,299 @@
+#!/bin/awk -f
+#!/opt/busybox/awk -f
+
+# Table of Content generator
+# For finding Headlines within a markdown document one needs to be context
+# aware. I.e. Headlines within verbatim blocks must be ignored, etc.
+#
+# Basically this program is a slightly stripped down version onf the main
+# markdown parser.
+# Most of the inline parser is also included to allow stylig of headlines.
+
+# TODO: Accept Arguments for controlling toc depth
+# TODO Maybe: Somehow enable section TOCs
+
+function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
+
+function HTML ( text ) {
+  gsub( /&/,  "\\&amp;",  text );
+  gsub( /</,  "\\&lt;",   text );
+  gsub( />/,  "\\&gt;",   text );
+  gsub( /"/,  "\\&quot;", text );
+  gsub( /'/,  "\\&#x27;", text );
+  gsub( /\\/, "\\&#x5C;", text );
+  return text;
+}
+
+function inline( line, LOCAL, len, code, href, guard ) {
+  nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*"    # not underline (except when escaped)
+  na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*"  # not asterisk (except when escaped)
+  ieu =  "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_"                 # inner <em> (underline)
+  isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__"                # inner <strong> (underline)
+  iea =    "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*"     # inner <em> (asterisk)
+  isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*"  # inner <strong> (asterisk)
+
+  if ( line ~ /^$/ ) {  # Recursion End
+    return "";
+
+  #  omit processing of escaped characters
+  } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
+    return substr(line, 2, 1) inline( substr(line, 3) );
+
+  # hard brakes <TOC MODIFIED>
+  } else if ( match(line, /^  \n/) ) {
+    return inline( substr(line, RLENGTH + 1) );
+
+  #  ``code spans``
+  } else if ( match( line, /^`+/) ) {
+    len = RLENGTH
+    guard = substr( line, 1, len )
+    if ( match(line, guard ".*" guard) ) {
+      code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
+      len = 2 * length(guard) + length(code)
+      #  strip single surrounding white spaces
+      code = gensub( / (.*) /, "\\1", "1" , code)
+      #  escape HTML within code span
+      gsub( /&/, "\\&amp;", code ); gsub( /</, "\\&lt;", code ); gsub( />/, "\\&gt;", code );
+      return "<code>" code "</code>" inline( substr( line, len + 1 ) )
+    }
+
+  #  quick links ("automatic links" in md doc) <TOC MODIFIED>
+  } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
+    len = RLENGTH;
+    href = HTML( substr( line, 2, len - 2) );
+    return "<span class=\"a\">" href "</span>" inline( substr( line, len + 1) );
+
+  # inline links <TOC MODIFIED>
+  } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
+    len = RLENGTH;
+    text  = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
+    return "<span class=\"a\">" inline( text ) "</span>" inline( substr( line, len + 1) );
+
+  # reference style links <TOC MODIFIED>
+  } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
+    len = RLENGTH;
+    text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
+    #   id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
+    return "<span class=\"a\">" inline(text) "</span>" inline( substr( line, len + 1) );
+
+  # inline images <TOC MODIFIED>
+  } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
+    len = RLENGTH;
+    text  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
+    # href  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
+    title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
+    if ( title ) {
+      return "<span class=\"img\">" HTML(title) "</span>" inline( substr( line, len + 1) );
+    } else {
+      return "<span class=\"img\">" HTML(text) "</span>" inline( substr( line, len + 1) );
+    }
+
+  # reference style images <TOC MODIFIED>
+  } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
+    len = RLENGTH;
+    text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
+    #  id = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
+    return "<span class=\"img\">" HTML(text) "</span>" inline( substr( line, len + 1) );
+
+  #  ~~strikeout~~ (pandoc)
+  } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
+    len = RLENGTH;
+    return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
+
+  #  ^superscript^ (pandoc)
+  } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
+    len = RLENGTH;
+    return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
+
+  #  ~subscript~ (pandoc)
+  } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
+    len = RLENGTH;
+    return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
+
+  # ignore embedded underscores (pandoc, php md)
+  } else if ( match(line, "^[[:alnum:]](__|_)") ) {
+    return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
+
+  #  __strong__$
+  } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
+    len = RLENGTH;
+    return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
+
+  #  __strong__
+  } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
+    len = RLENGTH;
+    return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
+
+  #  **strong**
+  } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
+    len = RLENGTH;
+    return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
+
+  #  _em_$
+  } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
+    len = RLENGTH;
+    return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
+
+  #  _em_
+  } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
+    len = RLENGTH;
+    return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
+
+  #  *em*
+  } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
+    len = RLENGTH;
+    return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
+
+  # # Macros  <TOC MODIFIED: never allow recursion>
+  # } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
+  #   len = RLENGTH;
+  #   return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
+
+  # Verbatim inline HTML
+  } else if ( AllowHTML && match( line, /^(<!--([^-]|-[^-]|--[^>])*-->|<\?([^\?]|\?[^>])*\?>|<![A-Z][^>]*>|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*\]\]>|<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)/) ) {
+    len = RLENGTH;
+    return substr( line, 1, len) inline(substr(line, len + 1));
+
+  # Literal HTML entities
+  } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
+    len = RLENGTH;
+    return substr( line, 1, len ) inline(substr(line, len + 1));
+
+  # Escape lone HTML character
+  } else if ( match( line, /^[&<>"']/) ) {
+    return HTML(substr(line, 1, 1)) inline(substr(line, 2));
+
+  #  continue walk over string
+  } else {
+    return substr(line, 1, 1) inline( substr(line, 2) );
+  }
+}
+
+function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
+  gsub( /^\n+|\n+$/, "", block );
+
+  if ( block == "" ) {
+    return "";
+
+  # HTML #2 #3 #4 $5
+  } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block(substr(block, 1, st - 1)) _block(substr(block, st + len));
+
+  # HTML #6
+  } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)([[:space:]\n>]|\/>)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block(substr(block, 1, st - 1)) _block(substr(block, st + len));
+
+  # HTML #1
+  } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
+    len = RLENGTH; st = RSTART;
+    match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
+    len = RSTART + RLENGTH;
+    return _block(substr(block, 1, st - 1)) _block(substr(block, st + len));
+
+  # HTML #7
+  } else if ( AllowHTML && match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)([[:space:]]*\n)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block(substr(block, st + len));
+  # Blockquote (leading >)
+  } else if ( match( block, /^> /) ) {
+    match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
+    len = RLENGTH; st = RSTART;
+    return _block( substr(block, st + len) );
+
+  # Line Blocks (pandoc)
+  } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block( substr( block, len + 1) );
+
+  # Indented Code Block
+  } else if ( match(block, /^(    |\t)[^\n]+(\n|$)((    |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block( substr( block, len + 1 ) );
+
+  # Fenced Code Block (pandoc)
+  } else if ( match( block, /^(~~~+|```+)/ ) ) {
+    guard = substr( block, 1, RLENGTH );
+    code = gensub(/^[^\n]+\n/, "", 1, block);
+    if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
+      len = RLENGTH; st = RSTART;
+      return _block( substr( code, st + len ) );
+    } else {
+      match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
+      len = RLENGTH; st = RSTART;
+      return _block( substr(block, st + len) );
+    }
+
+  # Unordered list
+  } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
+                            "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
+                            "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
+                            "|[^\n]+(\n|$))*" ) ) {
+  block = substr( block, RLENGTH + 1);
+  return _block( block );
+
+  # Ordered list
+  } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
+                            "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
+                            "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
+                            "|[^\n]+(\n|$))*" ) ) {
+  block = substr( block, RLENGTH + 1);
+  return _block( block );
+
+  # First Order Heading
+  } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
+    len = RLENGTH;
+    HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
+    return "<a class=\"toc h1\" href=\"#" HL[1] " - " HTML(gensub( /\n.*$/, "", "g", block )) "\">" inline( gensub( /\n.*$/, "", "g", block ) ) "</a>\n" \
+           _block( substr( block, len + 1 ) );
+
+  # Second Order Heading
+  } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
+    len = RLENGTH;
+    HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
+    return "<a class=\"toc h2\" href=\"#" HL[1] "." HL[2] " - " HTML(gensub( /\n.*$/, "", "g", block )) "\">" inline( gensub( /\n.*$/, "", "g", block ) ) "</a>\n" \
+           _block( substr( block, len + 1) );
+
+  # Nth Order Heading
+  } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
+    len = RLENGTH;
+    hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
+    htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
+    HL[hlvl]++; for ( n = hlvl + 1; n < 7; n++) { HL[n] = 0;}
+    hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
+    return "<a class=\"toc h" hlvl  "\" href=\"#" hid " - " HTML(htxt) "\">" inline( htxt ) "</a>\n" \
+           _block( substr( block, len + 1) );
+
+  # Horizontal rule
+  } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block(substr(block, 1, st - 1)) _block(substr(block, st + len));
+
+  # Plain paragraph
+  } else {
+    match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
+    len = RLENGTH; st = RSTART;
+    return _block( substr(block, st + len) );
+  }
+}
+
+BEGIN {
+  # Global Vars
+  file = ""; rl_href[""] = ""; rl_title[""] = "";
+  if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
+  HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
+
+  # Buffering of full file ist necessary, e.g. to find reference links
+  while (getline) { file = file $0 "\n"; }
+  # Clean up MS-DOS line breaks
+  gsub(/\r\n/, "\n", file);
+
+  # Clear reflinks from File
+  re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
+  while( gsub(re_reflink, "\n", file ) );
+
+  # Run Block Processing -> The Actual Markdown!
+  printf "%s", "<div class=\"macro toc\">\n" _block( file ) "</div>";
+}
diff --git a/macros/wikiform b/macros/wikiform
new file mode 100755 (executable)
index 0000000..37d3996
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+action="$1"
+
+. "$_EXEC/cgilite/cgilite.sh"
+set -- nocookie
+. "$_EXEC/cgilite/users.sh"
+
+case $action in
+  login)
+    w_user_login |"$_EXEC/cgilite/html-sh.sed"
+    ;;
+  register)
+    w_user_register |"$_EXEC/cgilite/html-sh.sed"
+    ;;
+esac
diff --git a/md_macros.awk b/md_macros.awk
new file mode 100755 (executable)
index 0000000..7e15482
--- /dev/null
@@ -0,0 +1,66 @@
+#!/bin/awk -f
+#!/opt/busybox/awk -f
+
+function sh_escape(arg){
+  return "'" gensub(/'/, "'\"'\"'", "g", arg) "'";
+}
+
+function argsplit(line, args, LOCAL, c, n, ctx) {
+  ctx="space"; n=0;
+
+  while ( length(line) > 0 ) {
+    c = substr(line, 1, 1);
+    line = substr(line, 2);
+    if (ctx == "space" )
+           if (c ~ /[ \t]/) ctx = "space";
+      else if (c ~ /\\/) { n++; ctx = "escbare"; }
+      else if (c ~ /"/)  { n++; ctx = "dquot"; }
+      else if (c ~ /'/)  { n++; ctx = "squot"; }
+      else   { n++; args[n] = c; ctx = "bare"; }
+    else if (ctx == "bare")
+           if (c ~ /[ \t]/) ctx = "space";
+      else if (c ~ /\\/)  ctx = "escbare";
+      else if (c ~ /"/)   ctx = "dquot";
+      else if (c ~ /'/)   ctx = "squot";
+      else args[n] = args[n] c;
+    else if (ctx == "dquot")
+           if (c ~ /"/)  ctx = "bare";
+      else if (c ~ /\\/) ctx = "escdquot";
+      else args[n] = args[n] c;
+    else if (ctx == "squot")
+      if (c ~ /'/)  ctx = "bare";
+      else args[n] = args[n] c;
+    else if (ctx == "escbare") {
+      args[n] = args[n] c;
+      ctx = "bare";
+    }
+    else if (ctx == "escdquot") {
+      args[n] = args[n] c;
+      ctx = "dquot";
+    }
+  }
+} 
+
+function macro(call, LOCAL, line, files, n, args) {
+  "cd " sh_escape(ENVIRON["MD_MACROS"]) "; printf '%s/' *" |getline line;
+  split(line, files, "/");
+  for (n in files) { files[files[n]] = ""; delete files[n]; }
+  delete files[""];
+
+  argsplit(call, args);
+  call="";
+
+  for (n = 1; n in args; n++) call = call sh_escape(args[n]) " ";
+
+  if (args[1] in files) {
+    RS=""; ORS=""; line="";
+    "printf '%s' " sh_escape(file) " | " sh_escape(ENVIRON["MD_MACROS"]) "/" call | getline line;
+    return line;
+  } else {
+    return HTML("<<" call ">>");
+  }
+}
+
+BEGIN {
+  if (ENVIRON["MD_MACROS"]) { AllowMacros = "true"; }
+}
diff --git a/page_edit.sh b/page_edit.sh
new file mode 100755 (executable)
index 0000000..9bb9f5f
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+. "${_EXEC}/themes/default.sh"
+. "${_EXEC}/session_lock.sh"
+
+wiki_text() {
+  # Print source text of a wiki page
+  # Get page from data or underlay dir
+  local page="$(PATH "$1")"
+
+  if [ -f "$_DATA/pages/$page/#page.md" ]; then
+    cat -- "$_DATA/pages/$page/#page.md"
+  elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
+    cat -- "$_EXEC/pages/$page/#page.md"
+  else
+    return 1
+  fi
+}
+
+edit_page="${PATH_INFO%\[edit\]}"
+edit_file="$_DATA/pages/$edit_page/#page.md"
+[ "$REQUEST_METHOD" = POST ] && edit_action="$(POST action)"
+
+debug "RM: $REQUEST_METHOD ea: $edit_action"
+
+if [ "$edit_page" = "$PATH_INFO" ]; then
+  unset edit_page edit_action edit_file
+  # END EDIT SCRIPT, continue in index.cgi
+
+elif [ "$edit_action" = update ]; then
+  mkdir -p -- "${edit_file%/#page.md}"
+
+  if S_LOCK "$edit_file"; then
+    POST pagetext >"$edit_file"
+    S_RELEASE "$edit_file"
+    REDIRECT "${_BASE}${PATH_INFO%\[edit\]}" |debug
+  else
+    export ERRMSG="ERR_NOLOCK"
+    REDIRECT "${_BASE}${PATH_INFO%\[edit\]}/[edit]" |debug
+  fi
+
+elif [ "$edit_action" = cancel ]; then
+  S_RELEASE "$edit_file"
+  REDIRECT "${_BASE}${PATH_INFO%\[edit\]}" |debug
+
+elif mkdir -p -- "${edit_file%/#page.md}" && S_LOCK "$edit_file"; then
+  # Display editor page
+  SESSION_COOKIE
+  theme_editor "$edit_page"
+
+else
+  export ERRMSG="ERR_NOLOCK"
+  theme_page "$edit_page"
+
+fi
diff --git a/pages/#attachments/favicon.ico b/pages/#attachments/favicon.ico
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/pages/#page.md b/pages/#page.md
new file mode 100644 (file)
index 0000000..4dd6aeb
--- /dev/null
@@ -0,0 +1,2 @@
+It Works!
+=========
diff --git a/pages/[wiki]/404/#page.md b/pages/[wiki]/404/#page.md
new file mode 100644 (file)
index 0000000..7596bdd
--- /dev/null
@@ -0,0 +1,4 @@
+404
+===
+
+**page not found**
diff --git a/pages/[wiki]/footer/#page.md b/pages/[wiki]/footer/#page.md
new file mode 100644 (file)
index 0000000..da2b20f
--- /dev/null
@@ -0,0 +1,3 @@
+----
+Shellwiki  
+Edit the Footer [here](/[wiki]/footer/[edit])
diff --git a/pages/[wiki]/header/#page.md b/pages/[wiki]/header/#page.md
new file mode 100644 (file)
index 0000000..7d50e32
--- /dev/null
@@ -0,0 +1,10 @@
+# Shellwiki
+
+::: { .menu }
+ * [Login]([login])
+ * [Register]([register])
+:::
+
+Edit the Header [here](/[wiki]/header/[edit])
+
+----
diff --git a/pages/[wiki]/login/#page.md b/pages/[wiki]/login/#page.md
new file mode 100644 (file)
index 0000000..2e4f544
--- /dev/null
@@ -0,0 +1,4 @@
+Login
+-----
+<<wikiform login>>  
+[Account registration]([register] "Sign up for a new user account")
diff --git a/pages/[wiki]/register/#page.md b/pages/[wiki]/register/#page.md
new file mode 100644 (file)
index 0000000..d6ba4e4
--- /dev/null
@@ -0,0 +1,3 @@
+Set up an account
+-----------------
+<<wikiform register>>
diff --git a/session_lock.sh b/session_lock.sh
new file mode 100755 (executable)
index 0000000..f55c9cd
--- /dev/null
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+. "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/cgilite/session.sh"
+
+LOCK_TIMEOUT="${LOCK_TIMEOUT:-1200}"
+
+S_LOCK(){
+  local file="$1" timeout="${2:-$LOCK_TIMEOUT}"
+  local date sid
+
+  printf "%i %s\n" "$_DATE" "$SESSION_ID" >>"${file}.lock"
+
+  if ! read date sid <"${file}.lock"; then
+    debug "Unable to access lock: ${file}.lock"
+
+  elif [ $((date + timeout)) -lt $_DATE ]; then
+    # Override stale lock
+    if LOCK "${file}.lock" 1; then
+      debug "Overriding stale lock: ${file}.lock"
+      printf "%i %s\n" "$_DATE" "$SESSION_ID" >"${file}.lock"
+      RELEASE "${file}.lock"
+      return 0
+    else
+      return 1
+    fi
+
+  elif [ "$sid" = "$SESSION_ID" -a "$date" -ne "$_DATE" ]; then
+    # Refresh aged lock
+    printf "%i %s\n" "$_DATE" "$SESSION_ID" >"${file}.lock"
+    return 0
+
+  elif [ "$sid" = "$SESSION_ID" ]; then
+    # Simple success
+    return 0
+
+  else
+    return 1
+  fi
+}
+
+S_RELEASE(){
+  local file="$1" timeout="${2:-$LOCK_TIMEOUT}"
+  local date sid
+
+  if ! read date sid <"${file}.lock"; then
+    # File was not locked
+    return 0
+
+  elif [ "$sid" = "$SESSION_ID" -a  $((date + timeout)) -lt $_DATE ]; then
+    # if lock is stale, protect against stale override before release
+    if LOCK "${file}.lock" 1; then
+      rm -- "${file}.lock"
+      RELEASE "${file}.lock"
+      return 0
+    else
+      return 1
+    fi
+
+  elif [ "$sid" = "$SESSION_ID" ]; then
+    # Simple success
+    rm -- "${file}.lock"
+    return 0
+
+  else
+    return 1
+  fi
+}
diff --git a/themes/default.sh b/themes/default.sh
new file mode 100755 (executable)
index 0000000..c096f82
--- /dev/null
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+theme_head(){
+  printf '
+  <meta name="viewport" content="width=device-width"/>
+  <link rel="stylesheet" type="text/css" href="%s/[.]/cgilite/common.css">
+  ' "$_BASE"
+}
+
+theme_header(){
+  printf '<header>%s</header>' "$(wiki '[wiki]/header/')"
+}
+
+theme_footer(){
+  printf '<footer>%s</footer>' "$(wiki '[wiki]/footer/' ||echo No footer)"
+}
+
+theme_page(){
+  local page="$1" title
+  title="${page%/}"; title="${title##*/}"
+
+  # Important! Web Server response including newline newline
+  printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
+
+  cat <<-EOF
+       <!DOCTYPE HTML>
+       <html><head>
+         $(theme_head)
+         <title>$(HTML "${title}")</title>
+       </head><body id="$(HTML "$page")">
+         $(theme_header)
+         <main>$(wiki "$page" || printf 'Page not found')</main>
+         $(theme_footer)
+       </body></html>
+       EOF
+}
+
+theme_editor(){
+  local page="$1" title
+  title="${page%/}"; title="${title##*/}"
+
+  # Important! Web Server response including newline
+  printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
+
+  cat <<-EOF
+       <!DOCTYPE HTML>
+       <html><head>
+         $(theme_head)
+         <title>$(HTML "${title}")</title>
+       </head><body id="$(HTML "$page")" class="editor">
+         $(theme_header)
+         <main><form method=POST>
+           <input type=hidden name=session_key value="${SESSION_KEY}"/>
+            <textarea name=pagetext>$(wiki_text "$page" |HTML)</textarea>
+            <button type=submit name=action value=update>Update</button>
+            <button type=submit name=action value=cancel>Cancel</button>
+          </form></main>
+         $(theme_footer)
+       </body></html>
+       EOF
+}
+
+theme_login(){
+  theme_page '[wiki]/login/'
+}
+
+theme_register(){
+  theme_page '[wiki]/register/'
+}
+
+theme_404(){
+  printf "%s\r\n" "Status: 404 Not Found"
+
+  theme_page '[wiki]/404/'
+}