diff s4-funcs.sh @ 942:5d70fa029f8d draft

Support MathJax rendering
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 25 Oct 2021 18:39:44 +0900
parents 79c7e64884c4
children 420ad90116e6
line wrap: on
line diff
--- a/s4-funcs.sh	Wed Sep 22 08:39:36 2021 +0900
+++ b/s4-funcs.sh	Mon Oct 25 18:39:44 2021 +0900
@@ -78,6 +78,8 @@
 whatsnewdays=${WHATS_NEW_DAYS:-14}
 main_session=`date +%F-$$`
 session=$main_session
+mathjax=${MATHJAX:-'<script>MathJax = {tex: {tags: "ams"}};</script>
+<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>'}
 
 tconfs=""
 imgcached=cache/${S4WORLD:+$S4WORLD/}img.`date +%Y/%m`
@@ -92,7 +94,11 @@
 logtag="($$)${S4WORLD:+{$S4WORLD\}}"
 exec 3>> $logdir/debug.out
 err() {
-  echo "[`date +%F-%T%z`]$logtag $@" 1>&3
+  # echo "[`date +%F-%T%z`]$logtag $@" 1>&3
+  # Avoid backslash escape sequences
+  cat<<EOF 1>&3
+[`date +%F-%T%z`]$logtag $@
+EOF
 }
 case "$HTTP_USER_AGENT" in
   *i[Pp]hone*|*[Aa]ndroid*)	touchpanel=1 ;;
@@ -454,8 +460,8 @@
   if [ -z "$1" ]; then
     tee -a $querylog
   else
-    echo "$@" >> $querylog
-    echo "$@"
+    printf '%s\n' "$@" >> $querylog
+    printf '%s\n' "$@"
   fi  >&5
   echo ".output stdout" >&5
   cat $sqo
@@ -801,11 +807,11 @@
       if [ x"$user" = x"$val" ]; then
 	echo $user; return
       elif ismember $user $val; then
-	echo $val; return
+	printf '%s' "$val"; return
       fi ;;
   esac
 ## err getpar/ret: "val=[$val]"
-  echo "$val"
+  printf '%s' "$val"
 }
 setskey() {
   # For quick response...(?)
@@ -845,25 +851,25 @@
      "X'"*)			# quoted hex string
        echo $1 ;;
      *\"*)			# string including dbl-quote"
-       v=`echo "$v"|sed -e 's/\"/\"\"/g'`
-       echo "\"$v\""
+       v=`printf '%s' "$v"|sed -e 's/\"/\"\"/g'`
+       printf '%s' "\"$v\""
        return ;;
      *.*.*|*-*-*|*[Ee]*[Ee]*|[Ee]*|*[\ -,:-df-~]*) # string
-       echo "\"$v\""
+       printf '%s' "\"$v\""
        return ;;
      *)
        if expr "$v" : '[-0-9.Ee][-0-9.Ee]*$' >/dev/null 2>&1; then
-	 echo $v		# MAYBE numeric, maybe...
+	 printf '%s' $v		# MAYBE numeric, maybe...
        else
-	 echo "\"$v\""
+	 printf '%s' "\"$v\""
        fi ;;
    esac)
 }
 sqlquotestr() (
   case "$1" in
-    *\'*)	v=`echo "$1"| sed "s/'/''/g"`
-		echo "'$v'" ;;
-    *)		echo "'$1'" ;;
+    *\'*)	v=`printf '%s' "$1"| sed "s/'/''/g"`
+		printf '%s' "'$v'" ;;
+    *)		printf '%s' "'$1'" ;;
   esac
 )
 mktempd() {
@@ -1002,7 +1008,7 @@
 	   rm -f $td/$c.count
 	   case $type in
 	     file:*)
-	       echo "$val" \
+	       printf '%s\n' "$val" \
 		   | while read fn; do
 		       file=$td/$fn
 		       if [ ! -s "$file" ]; then
@@ -1021,7 +1027,7 @@
 	       ;;
 	   esac
 	 fi
-	 echo "$val"		# Keep newlines by ""
+	 printf '%s' "$val"
 	 return
        fi
      done
@@ -1565,7 +1571,6 @@
 	#echo u=$us
 	#v="`echo ${us#*=}|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`"
 	v="`echo ${us#*=}|unhexize|sed -e 's/\"/\"\"/g'`"
- # err k=$k v=$v
 	case "$k" in
 	  *:filename)
 	    mimetype=`file --mime-type - < "$tmpd/$v"|cut -d' ' -f2`
@@ -4218,9 +4223,9 @@
 	  # Binary update line is TOO LONG to pipelining
 	  sqlfile="$tmpd/sqlf.$$"
 	  if [ -n "$transaction" ]; then
-	    echo "$sql" >> $transaction
+	    printf '%s' "$sql" >> $transaction
 	  else
-	    echo "$sql" > $sqlfile
+	    printf '%s' "$sql" > $sqlfile
 	    query ".read $sqlfile"
 	  fi
 	fi
@@ -4235,7 +4240,8 @@
 	limit="limit 1 offset $i"
 	i=$((i+1))		# increase beforehand against continue
 	val=`getvalquote $tbl $col "$limit"`
-	[ -z "$val" -o x"$val" = x'""' -o x"$val" = x"NULL" ] && continue
+	##XXX [ -z "$val" -o x"$val" = x'""' -o x"$val" = x"NULL" ] && continue
+	[ -z "$val" -o x"$val" = x'""' ] && continue
 	## err $col=$val
 	bin=NULL
 	## err partype$col=`getpartype $col "$limit"`
@@ -4251,6 +4257,9 @@
 	  *)    type=\"string\" ;;
 	esac
 	case `gettbl_coltype $tbl/$col` in
+	  [Cc][Hh][Ee][Cc][Kk][Bb][Oo][Xx]|[Tt][Ee][Xx][Tt])
+	    test x"$val" = x"NULL" && val="''"
+	    ;;
 	  password)		# special care for password
 	    # name={password,pswd1,pswd2}
 	    p1=`getpar pswd1 "$limit"`
@@ -4284,7 +4293,7 @@
 	if [ x"$bin" = x"NULL" ]; then
 	  ## err Normal-query: `echo $sql`
 	  if [ -n "$transaction" ]; then
-	    echo "$sql" >> $transaction
+	    printf '%s' "$sql" >> $transaction
 	  else
 	    query "$sql"
 	  fi
@@ -4292,9 +4301,9 @@
 	  sqlfile="$tmpd/query.$$"
 	  ## err sqlfile=`ls -lF $sqlfile`
 	  if [ -n "$transaction" ]; then
-	    echo "$sql" >> $transaction
+	    printf '%s' "$sql" >> $transaction
 	  else
-	    echo "$sql" > $sqlfile
+	    printf '%s' "$sql" > $sqlfile
 	    query ".read $sqlfile"
 	  fi
 	fi
@@ -4347,14 +4356,14 @@
   # Image Cache dir
   ## err genform: getcache=$2/$rowid
   td=`getcachedir "$2/$rowid"`
-  while IFS=: read prompt name keytype type args; do
+  while IFS=: read -r prompt name keytype type args; do
     [ -z "${prompt%%\#*}" ] && continue # skip comment line(#)
     sp="${args:+ }"
     form="" val=""
     if [ -n "$rowid" ]; then
       # err genform2a: Seeking for "$2.$name, type=$type"
       rawval=`getvalbyid $2 $name $rowid $td`
-      val=`echo "$rawval"|htmlescape`
+      val=`printf '%s\n' "$rawval"|htmlescape`
 ## err genform3a: getvalbyid $2 $name $rowid $td
 ## err genform3b: val="[$val]" type="$type"
     fi
@@ -4366,7 +4375,7 @@
 	cgiform=cgi_multi_$type
 	if [ -s $td/$name.count -a -n "$val" ]; then
 	  form=`$cgiform $name $td`
-	  val=$(echo "$val"|
+	  val=$(printf '%s\n' "$val"|
 		     while read fn; do
 		       echo "<tr><td>`cat $td/$fn|htmlescape|hreflink`
 			</td></tr>$nl"
@@ -4374,6 +4383,7 @@
 	  val="<table>$nl$val$nl</table>"
 	else
 	  #form="<input name=\"$name\" value=\"$val\" type=\"$type\"$sp$args>$nl"
+
 	  form=`cgi_$type $name "$rawval" "$args"`
 	fi
 	;;
@@ -4383,7 +4393,8 @@
 \"s,\([^ =][^=]*\)=\([^= ][^= ]*\),$fh value=\\"\2\\">\1</label>,g\"`"
 	;;
       [Cc][Hh][Ee][Cc][Kk][Bb][Oo][Xx])
-	form="<label><input type=\"checkbox\" name=\"$name\" value=\"${args#*=}\">${args%=*}</label>"
+	checked=${val:+ checked}
+	form="<label><input type=\"checkbox\" name=\"$name\" value=\"${args#*=}\"$checked>${args%=*}</label>"
 	;;
       [Ss][Ee][Ll][Ee][Cc][Tt])
 	fh="<select name=\"$name\">$nl"
@@ -4484,9 +4495,11 @@
     fi
   done < $1
   # enctype="multipart/form-data"
-  cat<<EOF
+  cat<<-EOF
 <form action="${GF_ACTION:-$myname}" method="POST" enctype="multipart/form-data">
-${rowid:+$rm}
+	EOF
+  test -n "$rowid" && printf '%s\n' "$rm" # Workaround for utf8 buggy NetBSD sh
+  cat<<EOF
  <table class="b $2">
 $forms
  </table>$hiddens

yatex.org