-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbml.min.js
More file actions
1 lines (1 loc) · 5.06 KB
/
cbml.min.js
File metadata and controls
1 lines (1 loc) · 5.06 KB
1
!function(e){function t(e){return String(e).replace(/&((quot|lt|gt|amp|nbsp)|#x([a-f\d]+)|#(\d+));/gi,function(e,t,n,s,o){return n?c[n.toLowerCase()]:s?String.fromCharCode(parseInt(s,16)):String.fromCharCode(+o)})}function n(e,t){for(var n=e.length.toString().length,s=e.slice(-t),o=s.length-1;o>=0;o--){var a=(e.length+o-s.length+1).toString();a=new Array(n-a.length+1).join(" ")+a,s[o]=a+(o===s.length-1?" > ":" ")+"| "+s[o]}console.log(s.join("\n"))}function s(e,s){function o(t,n,o,r){if(!(o<=n)){var c=e.slice(n,o),i={type:t,pos:n,endpos:o,value:c};r=r||{};for(var p in r)i[p]=r[p];if(!s.ignoreLine){var g=e.slice(0,n).split("\n");i.line=g.length,i.col=g[g.length-1].length+1,g=null}l.pos=o,a.push(i)}}s=s||{};for(var a=[],l={text:e,pos:0};l.pos<l.text.length;){var r=l.text.substring(l.pos).match(/(<!--|\/\*<|\(\*<|'''<|--\[\[<)(\/?)([\w_]+[\w_-]*[\w_]|[\w_]+)\s*|(<\/)([\w_]+[\w_-]*[\w_]|[\w_]+)(>\*\/|>\*\)|>'''|>\]\]|-->)/);if(!r)break;o("text",l.pos,l.pos+r.index);var c,i,p=r[3],g={},u={},h=r[0].length;if(p){switch(r[1]){case"\x3c!--":i=/^\s*(\/?-->)/,c="xml";break;case"/*<":i=/^\s*(\/?>\*\/)/,c="c";break;case"(*<":i=/^\s*(\/?>\*\))/,c="pascal";break;case"'''<":i=/^\s*(\/?>''')/,c="python";break;case"--[[<":i=/^\s*(\/?>\]\])/,c="lua"}var d,f,v,x;if("/"===r[2]){if(!(r=l.text.substring(l.pos+h).match(i)))throw d=e.slice(0,l.pos+h).split("\n"),f=d.length,v=d[d.length-1].length+1,n(d,5),x="Uncaught SyntaxError: Can't match \""+i+'". (line:'+f+" col:"+v+")",console.error(x),x;h+=r[0].length,o("right",l.pos,l.pos+h,{tag:p,language:c})}else{for(;;){if(!(r=l.text.substring(l.pos+h).match(/^\s*([\w_]+[\w_-]*[\w_]|[\w_]+)\s*/)))break;h+=r[0].length;var m=r[1],b="";switch(r=l.text.substring(l.pos+h).match(/^\s*=\s*('([^']*)'|"([^"]*)"|([^'"\s\/>]+))\s*/),r&&(h+=r[0].length,b=r[1]),b[0]){case'"':case"'":b=b.slice(1,-1),u[m]=b[0]}g[m]=t(b)}switch(c){case"xml":i=/^\s*(\/?-->|>)/;break;case"c":i=/^\s*(\/?>\*\/|>)/;break;case"pascal":i=/^\s*(\/?>\*\)|>)/;break;case"python":i=/^\s*(\/?>'''|>)/;break;case"lua":i=/^\s*(\/?>\]\]|>)/}if(!(r=l.text.substring(l.pos+h).match(i))){if("xml"===c){r=l.text.substring(l.pos+h).match(/^[^]*?-->/),r?(h+=r[0].length,o("text",l.pos,l.pos+h)):(h=l.text.length,o("text",l.pos,h));continue}if("c"===c){var w;if(Object.keys(g).some(function(e){if(!u[e]&&/^\s*\{/.test(g[e]))return w=!0,!0}),w){r=l.text.substring(l.pos+h).match(/^[^]*?>\*\//),r?(h+=r[0].length,o("text",l.pos,l.pos+h)):(h=l.text.length,o("text",l.pos,h));continue}}throw d=e.slice(0,l.pos+h).split("\n"),f=d.length,v=d[d.length-1].length+1,n(d,5),x="Uncaught SyntaxError: Can't match \""+i+'". (line:'+f+" col:"+v+")",console.error(x),x}h+=r[0].length,">"===r[1]?o("left",l.pos,l.pos+h,{comment:!0,tag:p,language:c,attrs:g}):o("/"===r[1][0]?"single":"left",l.pos,l.pos+h,{tag:p,language:c,attrs:g})}}else{switch(p=r[5],r[6]){case"--\x3e":c="xml";break;case">*/":c="c";break;case">*)":c="pascal";break;case">'''":c="python";break;case">]]":c="lua"}o("right",l.pos,l.pos+h,{comment:!0,tag:p,language:c})}}return o("text",l.pos,l.text.length),a}function o(e){var t="";return e&&e.forEach(function(e){return t+=e.value}),t}function a(e,t){if(t&&t.nodes)for(var n=t.nodes.length-1;n>=0;n--){var s=t.nodes[n];if(a(e,s),"left"===s.type){s.type="text";var o=s.nodes[0];if(o){var l=s.nodes[s.nodes.length-1];s.endpos=o.pos,s.value=e.slice(s.pos,s.endpos);var r=[n+1,0];[].push.apply(r,s.nodes),[].splice.apply(t.nodes,r),"cbml"!==t.type&&(t.endpos=l.endpos,t.value=e.slice(t.pos,t.endpos))}delete s.nodes,delete s.tag,delete s.attrs,delete s.language}var c=t.nodes[n+1];c&&"text"===s.type&&"text"===c.type&&(c.pos=s.pos,c.line=s.line,c.col=s.col,c.value=e.slice(c.pos,c.endpos),t.nodes.splice(n,1))}}function l(e,t){if(!e)return{type:"cbml",nodes:[],value:""};e=String(e).replace(/\r\n?|[\n\u2028\u2029]/g,"\n").replace(/^\uFEFF/,"");var l={type:"cbml",nodes:[]},r=l,c=s(e,t),i=[];return c.forEach(function(t){switch(t.type){case"single":case"text":r.nodes.push(t),l!==r&&(r.value+=t.value),r.endpos=t.endpos;break;case"left":t.nodes=[],i.push(t),r.nodes.push(t),r=t;break;case"right":var s,a;if(i.length<=0)throw s=e.slice(0,t.endpos).split("\n"),s.length,s[s.length-1].length+1,n(s,5),a="No start tag. (line:"+t.line+" col:"+t.col+")",console.error(a),a;for(var c=i.length-1;c>=0;c--){var p=i[c],g=i[c-1];if(p.tag===t.tag&&p.language===t.language&&p.comment===t.comment){if(p.type="block",p.value+=t.value,p.endpos=t.endpos,p.content=o(p.nodes),g?(r=g,r.value+=p.value):r=l,r.endpos=p.endpos,p.nodes&&p.nodes.length){var u=p.nodes[0],h=p.nodes[p.nodes.length-1];p.prefix=p.value.slice(0,u.pos-p.pos),p.suffix=p.value.slice(-(p.endpos-h.endpos))}i=i.slice(0,c);break}if(!g)throw s=e.slice(0,t.endpos).split("\n"),s.length,s[s.length-1].length+1,n(s,5),a="No start tag. (line:"+t.line+" col:"+t.col+")",console.error(a),a;p.type="text",delete p.nodes,delete p.tag,delete p.attrs,delete p.language,g.value+=p.value,g.endpos=p.endpos}}}),a(e,l),l}var r=r||{},c={quot:'"',lt:"<",gt:">",amp:"&",nbsp:" "};r.tokenizer=s,r.parse=l,"function"==typeof define?define.amd&&define(function(){return r}):"undefined"!=typeof module&&module.exports?module.exports=r:window.cbml=r}();