changeset 942:5d70fa029f8d draft

Support MathJax rendering
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 25 Oct 2021 18:39:44 +0900
parents 79c7e64884c4
children b8d09525a0b4
files examples/sns/form/blog.def s4-blog.sh s4-cgi.sh s4-funcs.sh
diffstat 4 files changed, 61 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/examples/sns/form/blog.def	Wed Sep 22 08:39:36 2021 +0900
+++ b/examples/sns/form/blog.def	Mon Oct 25 18:39:44 2021 +0900
@@ -3,6 +3,7 @@
 タイトル:title:s:text:maxlength="200"
 コメント書込通知:notify:s:select:しない=no 管理者のみに通知(グループの場合のみ)=admin 所有者全員に通知=all
 稼動状態:state:s:select:稼動=active 凍結(新規書込停止)=frozen
+数式使用(MathJax):mathjax:s:checkbox:記事中 \(数式\) または \[数式\] を使う場合はチェック=yes
 所有者:owner:s:owner:
 筆者:author:s:author:
 時刻:ctime:s:stamp:
--- a/s4-blog.sh	Wed Sep 22 08:39:36 2021 +0900
+++ b/s4-blog.sh	Mon Oct 25 18:39:44 2021 +0900
@@ -183,6 +183,7 @@
   blog_notify=`getvalbyid blog notify "$rowid"`
   blog_team=`blog_getteam "$rowid"`
   blog_mode=`getvalbyid blog mode "$rowid"`
+  blog_math=`getvalbyid blog mathjax "$rowid"`
   case "$blog_notify" in # "all", "admin" or "no" (or NULL)
     admin)	notifyto=adm ;;
     *)		notifyto="" ;;
@@ -269,7 +270,7 @@
 	;;
     esac
   fi
-  href4="<a href=\"#bottom\" accesskey=\"b\" title=\"Shortcut: B${nl}to the Bottom\"> 末尾へ</a>"
+  href4="${blog_math:+Math} <a href=\"#bottom\" accesskey=\"b\" title=\"Shortcut: B${nl}to the Bottom\"> 末尾へ</a>"
   $isgrpadmin &&
       href5="<a href=\"?blogseen+$rowid\" accesskey=\"s\" title=\"Shortcut: S${nl}State of Accesses\"> 読刻</a>"
   quizmodefile=$tmpd/quiz; rm -f "$quizmodefile"	# XXX: Global state
@@ -416,6 +417,7 @@
     echo "時間をおいてください(Visit later please)." | html p
     return
   fi
+  echo "${blog_math:+$mathjax}"
   echo '<table class="blog_replies"> <!-- blog:blog_showentry() main table -->'
   # If, nLimit = 50
   # show article:1, hide(2, 3), show(4, ...)
@@ -447,7 +449,7 @@
   err "blog_showentry  Started: `gdate +%S.%03N` ${fetch_ajax:+ajax}"
   # Start blog_replies table
   $CAT $midfile |
-  while IFS='|' read id edit notify uid author uname icon aid \
+  while IFS='|' read -r id edit notify uid author uname icon aid \
 	   tm reki new hte fa imgids
   do
     if [ -n "$omitline" ]; then
@@ -661,7 +663,7 @@
   owner=`getvalbyid blog owner $1`
   title=`getvalbyid blog title $1`
   ge=`gecos "$owner"`
-  htmlowner=`echo ${ge:-$owner}|htmlescape`
+  htmlowner=`printf '%s' "${ge:-$owner}"|htmlescape`
   fh=$tmpd/formhead
   echo "$time [$title]@$htmlowner" > $fh
   lshandoutsub "$owner" "$@" \
@@ -670,7 +672,7 @@
 	   -D_FORMHEAD_="syscmd(cat $fh)" \
 	   -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
 	   $layout/html.m4.html $layout/form+dump-whead.m4.html
-  gn=`echo $owner|htmlescape`
+  gn=`printf '%s' "$owner"|htmlescape`
   echo "<p><a href=\"?lshandoutall+$1\">グループ $gn すべてのレポート板集計</a></p>"
 }
 gethandoutcsv() {
@@ -756,7 +758,7 @@
     _m4 -D_TITLE_="提出状況" $layout/html.m4.html
     ge=`gecos "$owner"`
     tbls=""
-    grptxt=`echo "${ge:-$owner}"|htmlescape`
+    grptxt=`printf '%s' "${ge:-$owner}"|htmlescape`
     echo "<h1>$grptxt 書き込み状況一覧</h1>"
   fi
   if [ -z "$SQL" ]; then
@@ -766,7 +768,7 @@
       [ $brid = 0 ] && continue
       time=`getvalbyid blog ctime $brid|colrm 11`
       title=`getvalbyid blog title $brid`
-      titleH=`echo "$title"|htmlescape`
+      titleH=`printf '%s' "$title"|htmlescape`
       state=`getvalbyid blog state $brid|htmlescape`
       tt="handout_$brid"
       [ "$state" = "frozen" ] && frozen=" $FROZEN_TAG" || frozen=""
@@ -1053,7 +1055,7 @@
     cat <<-EOF
 	<tr>
 	 <td><a href="?home+$uid">`echo "$hexge"|unhexize|htmlescape`</a></td>
-	 <td>`echo ${u%%@*}|htmlescape`</td>
+	 <td>`printf '%s' "${u%%@*}"|htmlescape`</td>
 	 $td${time:----}</td></tr>
 	EOF
   done
@@ -1180,7 +1182,7 @@
     rowid=$((${kwd#\#} + 0))	# Force to be a number
     kc="ar.rowid = $rowid"
   else
-    for k in `echo "$kwd" | sed "s/'/''/g"`; do		# With wrap quotes
+    for k in `printf '%s' "$kwd" | sed "s/'/''/g"`; do	# With wrap quotes
       ctime=""
       if expr x"$k" : 'x@[><= ]*[1-9][][0-9]*-[][0-9:-]*$' >/dev/null >&2; then
 	# '@<2016-10-10'	-> ctime < '2016-10-10'
@@ -1218,14 +1220,14 @@
 	e=""
 	case "$k" in
 	  *${likeesc}*) e="" ;;		# Giving up char-escaping
-	  *%*|*_*) k=`echo "$k"|sed "s/\([%_]\)/${likeesc}\1/g"`
+	  *%*|*_*) k=`printf '%s' "$k"|sed "s/\([%_]\)/${likeesc}\1/g"`
 	       e=" ESCAPE '$likeesc'" ;;
 	esac
 	kc=$kc${kc:+" AND "}"content LIKE '%$k%'$e"
       fi
     done
   fi
-  kwd=`echo "$kwd"|htmlescape`
+  kwd=`printf '%s' "$kwd"|htmlescape`
   owner=`getpar owner`
   owner=${owner:-$1}
   grid=`getpar grid`
@@ -1359,11 +1361,11 @@
   newval=`getvalbyid blog "$2" "$1"`
   alert="${msg:+, \"alert\": \"$msg\"}"
   json=$(cat <<-EOF
-	{"code": $code, "$2": "`echo "$newval"|sed 's/"/\\\\"/g'`"$alert}
+	{"code": $code, "$2": "`printf '%s' "$newval"|sed 's/"/\\\\"/g'`"$alert}
 	EOF
       )
   err blog_setval: returning JSON: "$json"
-  echo "$json"
+  printf '%s\n' "$json"
   exit
 }
 
@@ -1387,7 +1389,7 @@
   else
     owner=`getpar owner`
   fi
-  htmlowner=`echo $owner|htmlescape`
+  htmlowner=`printf '%s' $owner|htmlescape`
   err blog-add: \$1=$grprowid rowid=$rowid owner=$owner
   if isgroup "$owner"; then
     if [ -z "$grprowid" ]; then
@@ -1481,7 +1483,7 @@
   fi
   title=`getvalbyid blog title $rowid`
   owner=`getvalbyid blog owner $rowid`
-  htmlowner=`echo $owner|htmlescape`
+  htmlowner=`printf '%s' $owner|htmlescape`
   qowner=`sqlquotestr "$owner"`
   if [ -z "$title" ]; then
     echo "日記番号指定が無効です。" | html p
--- a/s4-cgi.sh	Wed Sep 22 08:39:36 2021 +0900
+++ b/s4-cgi.sh	Mon Oct 25 18:39:44 2021 +0900
@@ -44,11 +44,11 @@
 EOF
 }
 cgi_text() {
-  _v=`echo "$2"|htmlescape`
+  _v=`printf '%s' "$2"|htmlescape`
   echo "<input type=\"text\" name=\"$1\" value=\"$_v\" $3>"
 }
 cgi_textarea() {
-  _v=`echo "$2"|htmlescape`
+  _v=`printf '%s' "$2"|htmlescape`
   cat<<EOF
 <textarea name="$1" $3>$_v</textarea>
 EOF
@@ -79,7 +79,7 @@
   # $1=name $2=dir $3=func $4=args...
   # `dir' should contain $name.count and $name.N where N is 1 upto N
   i=1 name=$1 dir=$2 func=$3
-  argnomulti=`echo "$4"|sed 's/multiple//'` # No multiple for Replaced entres
+  argnomulti=`printf '%s' "$4"|sed 's/multiple//'` # No multiple for Replaced entries
   n=`cat $dir/$name.count`
   case "$func" in
     *file) addrename=yes ;;
--- 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