Revision 5b94fd2c12712e04345d4b130358afbb352f6436 authored by vthierry on 28 October 2025, 18:54:37 UTC, committed by vthierry on 28 October 2025, 18:54:37 UTC
1 parent 4ad30c1
wJSON.js
var e={parse:function(t){return{string:""+t,index:0,read:function(){var t=this.read_value();return this.next_space(),this.index<this.string.length?{value:t,trailer:this.string.substr(this.index)}:t},read_value:function(){switch(this.next_space(),this.string[this.index]){case"{":return this.read_tuple_value();case"[":return this.read_list_value();default:return this.string2value(this.read_word(!1))}},read_tuple_value:function(){var e={};this.index++;for(let i=-1;i!=this.index;){if(i=this.index,this.next_space(),this.index>=this.string.length)return e;if("}"==this.string[this.index])return this.index++,e;var n=this.read_word();if(""==n)return e;this.next_space();let t=!0;this.read_punctuation([":","="])&&(t=this.read_value()),e[n]=t,this.next_space(),this.read_punctuation([",",";"])}},read_list_value:function(){var i=[];this.index++;for(let t=-1;t!=this.index;){if(t=this.index,this.next_space(),this.index>=this.string.length)return i;if("]"==this.string[this.index])return this.index++,i;i.push(this.read_value()),this.next_space(),this.read_punctuation([",",";"])}},read_punctuation:function(t){let i=!1;for(;this.index<this.string.length&&(this.next_space(),t.includes(this.string[this.index]));)i=!0,this.index++;return i},read_word:function(t=!1){return'"'==this.string[this.index]||"'"==this.string[this.index]?this.read_quoted_word(this.string[this.index]):this.read_nospace_word(t)},read_quoted_word:function(t){let i="";for(this.index++;this.index<this.string.length&&this.string[this.index]!=t;this.index++)if("\\"==this.string[this.index]&&this.index<this.string.length-1)switch(this.index++,this.string[this.index]){case"'":case'"':case"\\":case"/":i+=this.string[this.index];break;case"n":i+="\n";break;case"b":i+="\b";break;case"r":i+="\r";break;case"t":i+="\t";break;case"f":i+="\f";break;default:i=(i+="\\")+this.string[this.index]}else i+=this.string[this.index];return this.index<this.string.length&&this.index++,i},read_nospace_word:function(t=!1){for(var i=this.index;this.index<this.string.length&&(t?this.no_endofline(this.string[this.index]):this.no_space(this.string[this.index]));this.index++);return this.string.substr(i,this.index-i).trim()},next_space:function(){for(;this.index<this.string.length&&this.isspace(this.string[this.index]);this.index++);if(this.index<this.string.length&&("#"==this.string[this.index]||"/"==this.string[this.index]&&(this.index==this.string.length-1||"/"==this.string[this.index+1]))){for(;this.index<this.string.length&&"\n"!=this.string[this.index];this.index++);this.next_space()}},string2value:function(t){return new RegExp("^(true|false)$").test(t)?"true"==t:new RegExp("^[-+]?[0-9]+$").test(t)?parseInt(t):new RegExp("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$").test(t)?parseFloat(t):t},no_space:function(t){return":"==t?this.index<this.string.length-1&&new RegExp("[^\\s,;:={}[\\]'\"]").test(this.string[this.index+1]):new RegExp("[^\\s,;:=}\\]'\"]").test(t)},no_endofline:function(t){return new RegExp("[^\\n,;:=}\\]]").test(t)},isspace:function(t){return new RegExp("\\s").test(t)}}.read()},stringify:function(t,r=!1){return{write:function(t){return("html"==r?"<style>.wjson {background-color: lightgray; font-weight:bold; display: inline-block; padding:10px; } .wjson .name { font-weight: normal; color: red;} .wjson .value { font-weight: normal; color: green;}</style><div class='wjson'>":"latex"==r?'\\documentclass{article}\n\\usepackage{listings}\n\\usepackage{xcolor}\n\\lstdefinelanguage{json}{basicstyle=\\normalfont\\ttfamily, numbers=left, numberstyle=\\scriptsize, breaklines=true, frame=lines, backgroundcolor=\\color{gray!10}, showstringspaces=false, string=[db]{"}, stringstyle=\\color{green!50!black}, morestring=[s][\\color{black}]{\\ \\ "}{":}, keywordstyle=\\color{blue}, keywords={true,false,null}, literate= *{0}{{{\\color{red}0}}}{1} {1}{{{\\color{red}1}}}{1} {2}{{{\\color{red}2}}}{1} {3}{{{\\color{red}3}}}{1} {4}{{{\\color{red}4}}}{1} {5}{{{\\color{red}5}}}{1} {6}{{{\\color{red}6}}}{1} {7}{{{\\color{red}7}}}{1} {8}{{{\\color{red}8}}}{1} {9}{{{\\color{red}9}}}{1} {.}{{{\\color{red}.}}}{1} {:}{{{\\color{gray}{:}}}}{1} {,}{{{\\color{gray}{,}}}}{1} {\\{}{{{\\color{gray}{\\{}}}}{1} {\\}}{{{\\color{gray}{\\}}}}}{1} {[}{{{\\color{gray}{[}}}}{1} {]}{{{\\color{gray}{]}}}}{1},}\n\n\n\\begin{document}\n\n\\begin{lstlisting}[language=json]\n':"")+this.write_value(t)+(1==r?"\n":"html"==r?"</div>":"latex"==r?"\n\\end{lstlisting}\n\n\\end{document}\n":"")},write_value:function(i){if(i instanceof Object){if(i instanceof Array){this.itab++;var e=[];for(let t=0;t<i.length;t++)e.push(this.write_value(i[t]));return this.write_strings("[","]",e)}this.itab++;var t,n=[];for(t in i){var s=this.write_value(i[t]);n.push(this.write_word(t,"name")+("minified"==r&&"true"==s?"":":"+("minified"==r&&new RegExp('["{}[\\]]').test(s[0])?"":" ")+s))}return this.write_strings("{","}",n)}return this.write_word(i)},write_word:function(t,i="value"){let e=String(t);t=""==e||new RegExp("[\\s,;:={}[\\]]").test(e);return t&&(e=e.replace(new RegExp('(["\\\\])',"g"),"\\$1")),(t?'"':"")+("html"==r?"<span class='"+i+"'>"+e+"</span>":e)+(t?'"':"")},write_strings:function(t,i,e){let n=t;for(var s in e)n+=("minified"!=r||0<s&&"}"!=n[n.length-1]&&"]"!=n[n.length-1]?this.write_line():"")+e[s];return this.itab--,n=n+("minified"!=r?this.write_line():"")+i,n=(1==r||"html"==r||"latex"==r)&&"["==t&&n.length<1200&&!n.substring(1).match(new RegExp("[[{]"))?n.replaceAll(new RegExp("html"==r?"<br/>( )*":"\\s+","g")," "):n},write_line:function(){if(1==r||"latex"==r){let i="\n";for(let t=0;t<this.itab;t++)i+=" ";return i}if("html"!=r)return" ";{let i="<br/>";for(let t=0;t<this.itab;t++)i+=" ";return i}},itab:0}.write(t)},wjson2json:function(t,i=!0){return i?JSON.stringify(e.parse(t),null,2):JSON.stringify(e.parse(t))},wjson2wjson:function(t,i=!0){return e.stringify(e.parse(t),i)}};module.exports=e;

Computing file changes ...