#!/usr/bin/env -S /home/phil/build/ngn-k/k /stringify /TODO: split off into separate library file s.k sd:`i!({$x}) str:{sd[@x][x]} sd[`d]:{$x} sd[`c]:{"\"",x,"\""} sd[`s]:{,//"`",$x} sd[`m]:{`TODO} sd[`o]:{$x} sd[`p]:{$x} sd[`q]:{$x} sd[`r]:{$x} sd[`u]:{$x} sd[`v]:{$x} sd[`w]:{$x} sd[`e]:{$x} sd[`A]:{"(",|")",|1_,//{" ",str[x]}'[x]} sd[`I]:sd[`A] sd[`D]:sd[`A] sd[`C]:{"\"",,//x,"\""} sd[`S]:sd[`A] /{1_,//{,//" `",$x}'[x]} sd[`M]:{`TODO} w:{"/dev/stdout"0:x} //stringify tests /wtest1:(5;2.5;"a";`b;`foo;d1;{x};{x+y}[;3];(*|:);(+/);(+:);(+);(/)) /wtest2:{x,x,x}'[wtest] /wt1:{w[str[wtest1[x]]]} /wt2:{w[str[wtest2[x]]]} //get file cgi:~" "~`env[`"SCRIPT_PATH"]@!2 d:$[cgi;"/"/|1_|"/"\`env[`"SCRIPT_PATH"];`env[`PWD]] argv:"\/"\`env[`"PATH_INFO"] f:$[cgi;*argv;`argv[2]] f:$[" "~f@!2;"index.kn";f] f:$[cgi;d,"/",f;f] /p:.[{(,"20 text/gemini"),0:d,"/",f};0N;,"51 text/gemini"] /TODO: proper .[] syntax p:.[{(,"20 text/gemini"),0:f};0N;,"51 text/gemini"] /TODO: proper .[] syntax /process cells put:{?[x;1+(y;y);,z]} out:{$[0=#y;x;o[put[x;*y;*z];1+1_y;1_z]]} cl:&'{3{x~"|||"}':x}'[p] cl:{x[&~2>1_{(x-y)*x-y}':-2,x]}'cl cb:&{x~,0}'cl /cb:&{x~"|||"}'p $[~0=2!#cb;`err "|||";] cb:(_0.5*#cb;2)#cb ci:&{(0<#*x)&1<#x}'cl g:{$[~0=2!#x;`err "|||";]}'cl[ci] /garbage cl[ci]:{(_0.5*#x;2)#x}'cl[ci] ss:{(*x)_|((#y)-*|x)_|y} e:{.[.:;,x;"E:",]} ca:(cb,ci)[<*'cb,ci] cx:{$[1=#x;`i,,cl[x];2=#x;`b,{,1_x+!y-x}.x]}'ca cr:!#cx cr:{$[`b=*cx[x];{`b,e'p[*|cx[x]]}x;`i=*cx[x];{`i,e'3_'ss[;p[ca[x]]]'*|cx[x]}x]}'cr b:&{`b=*x}'cx i:&{`i=*x}'cx p[ca[i]]:{cs:"|||"\p[ca[x]];cs[1+2*!_0.5*#cs]:str'1_cr[x];,//cs}'i p[ca[b]]:(#b)#,("```K source code";"```") ib:{put[y;(|/'ca[b])[x];"```K output\r\n\r\nOutput:\r\n\r\n",|"```\n\r",|"\r\n"/str'1_cr[b[x]]]} p:{$[0=#x;y;0<#x;o[1_x;ib[*x;y]]]}[|!#b;p] /TODO: add some kind of indicator and indent for the output lines /TODO: remove extra \ns from error messages /TODO: generally tidy up the newlines mess /TODO: add a directive to execute but not display a code block /TODO: what about duplicate variable names in source code? may need to prefix vars with e.g. _kral_ /TODO: handle line breaks within inline code? /TODO: make this an SCGI process and IPC calls /ci:{(*x)+1_!-/|x}'cb /il:&3<{+/"|"=x}'[p] /inlines /ci:(ci,,'il)@<*'ci,il /r:{"::> ",x}'str',//'{x[&~`u=@'x]}'.''p[ci] /p[*'cb]:(#cb)#,"```K source code" /p[*'|'cb]:(#cb)#,"```" /p:out[p;*'|'cb;r] /p:out[p;&{"::> "~x[!4]}'p;(#&{"::> "~x[!4]}'p)#,"```"] /p:out[p;-1+&{"::> "~x[!4]}'p;(#&{"::> "~x[!4]}'p)#,"```K output"] /p:out[p;&{x~"```K output"}'p;(#&{x~"```K output"}'p)#""] /w[{x,"\r"}'[p]] w["\r\n"/p]