-
Notifications
You must be signed in to change notification settings - Fork 0
/
cml.paste
28 lines (28 loc) · 22.6 KB
/
cml.paste
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if (globalThis.window) globalThis.module ||= { exports: {} }
"use strict";var w=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var j=(o,t)=>{for(var e in t)w(o,e,{get:t[e],enumerable:!0})},q=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of H(t))!_.call(o,s)&&s!==e&&w(o,s,{get:()=>t[s],enumerable:!(r=F(t,s))||r.enumerable});return o};var B=o=>q(w({},"__esModule",{value:!0}),o);var V={};j(V,{default:()=>K});module.exports=B(V);function x(o,t=[]){if(!o)throw new Error("No object provided to eobj.");o?.name&&t.push(o.name),t.push("default");let e={};for(let r of t){e[r]=o;try{window[r]=o}catch{}}try{module.exports=e}catch{}return e}var u=x(x,["eobj"]).default;var A=globalThis,Q=A.eval,W=()=>{},U=async()=>{},D=()=>{throw void 0};try{D=require}catch{}var G=new class{chaosGl=u(A,["chaosGl"]).default;chaosEval=u(Q,["chaosEval"]).default;NOOP=u(W,["NOOP"]).default;ANOOP=u(U,["ANOOP"]).default;R=u(D,["R"]).default},f=u(G).default;var C=class extends Error{constructor(t,e){super(Array.isArray(t)?t.join(`
`):t),this.name="CowErr"+(e?` (${e})`:"")}toss=(...t)=>console.error(this,...t);throw(){throw this}},d=u(C).default;var{chaosGl:lt,ANOOP:ct,NOOP:ht}=f,E=class o{static CML_Static=o;CML_Static=o;static errorList={eqspl3:new d(["You can't have 3 Equals characters (`=`)!","Try to shorten it please, use `|e`","it is the escaped version of `=`.",,'Use: "meta %name=viewport %content=width|edevice-width;"',"=> `|e` replaced with `=`",'=> "meta %name=viewport %content=width=device-width;"']),scripterror:new d(["Apparently, you made an error loading or executing your script.","Look back and take a gander.",,"Potentially:","=> Check if you loaded the right file.","=> Check if you made a typo.","=> Check if you did not add the directory to the file.","=> Check if you did every thing else right.",,"=> If all else.cry :("])};static DisplayErrors={noBeam:`
c;
beam;
"Beam Failed!
<br>
Try to fix the simple Fetch error.
<br>
Shouldn't take long.";
`};static Rexps={quoteExept:/\n(?=(?:(?:[^"]*"){2})*[^"]*$)/gm,colExept:/\n(?=(?:(?:[^:]*:){2})*[^:]*$)/gm,betweenQuote:/"([a-zA-Z\s\S]+)"/gm,betweenCol:/^:([a-zA-Z\S\s]+):/gm,script:/\{\=([a-zA-Z\S\s][^;]+)\=\}/gm,comment:/\/\/(.*)\n{0,1}/gm,lineWithComment:/[^a-zA-Z0-9:-■\n]+((?:[\t ]{0,})\/\/(?:.*)\n{0,1})/gm,formatspace1:/\n{1,}/gm,formatspace2:/\n[\t \n]{0,}\n/gm};$(t){return document.querySelector(t)}arrMatch(t,e){let r=0,s=[];for(let n=0;n<e.length;n++)t.includes(e[n])&&(r++,s.push(e[n]));return{count:r,list:s}}},v=u(E).default;var{chaosGl:z}=f,k=class{constructor(t){this.event=t;this.#r=new this.#o(t),this.#p()}#e=[];#n=t=>{this.#e.push(t)};#t=t=>this.#e.splice(this.#e.indexOf(t),1);#d=()=>this.#e=[];#f=t=>this.#e.push(t);private=!1;#i=()=>({event:this.event,globals:this.#e,remove:this.#t,clear:this.#d,add:this.#f,_:()=>this.private?null:this,[Symbol.toStringTag]:"CustomEventHandle"});#a=t=>({set:this.#n,get:this.#i});#b=()=>Object.defineProperty(z,this.event,this.#a(this.event));#p(){this.#b()}get detail(){return Reflect.has(this.#r,"detail")?this.#r.detail:null}set detail(t){this.makeEvent(this.event,t)}#r;#o=globalThis.CustomEvent?CustomEvent:Event;makeEvent(t,e={}){return this.#r=new this.#o(t,{detail:e})}activate(){for(let t of this.#e)t(this.#r);dispatchEvent(this.#r)}},L=u(k).default;var{chaosEval:X}=f,Z=()=>{let o=!1;try{X("import.meta"),o=!0}catch{}return o?"ES Module":typeof module<"u"&&module?.exports&&typeof window>"u"?"Node":typeof window<"u"&&typeof window?.document<"u"?"Browser":"Unknown"},P=u(Z,["checkEnvironment"]).default;var S=class o{constructor(t,e=t.documentElement){this.document=t;this.documentElement=e}args=[];init(...t){this.document.open(...this.args),this.document.write(...t),this.document.close()}initScripts(t=this.document){let e=Array.from(t.scripts);return e.forEach(r=>{let s=document.createElement("script");for(let n of r.attributes)s.setAttribute(n.name,n.value);s.textContent=r.textContent,document.body.appendChild(s)}),e}static d=globalThis.document?.implementation;static makeStaticPage=function(t){if(!o.d)throw new Error("No document implementation");let e=o.d.createHTMLDocument(t);return new o(e)};static writePageToPage(t){let e=t.document.documentElement;document.documentElement.replaceWith(e)}static manualWrite=function(...t){let e;new.target&&([e,...t]=t);let r=o.makeStaticPage(e);return r.init(...t),r.initScripts(),o.writePageToPage(r),r}},T=class o{static d=globalThis.document?.implementation;static makeDoc(t,e,r){if(!this.d)throw new Error("No document implementation");let s=this.d.createDocument(t,e,r);return new o(s)}document;constructor(t){this.document=t}addElement(t,e={}){let r=this.document.createElement(t);for(let s in e)r.setAttribute(s,e[s]);return this.document.appendChild(r),r}getElementsByTagName(t){return this.document.getElementsByTagName(t)}serialize(){return new XMLSerializer().serializeToString(this.document)}treewalk(t,e=NodeFilter.SHOW_ALL){let r=document.createTreeWalker(this.document,e),s;for(;s=r.nextNode();)t(s,r)}},R={CowHTMLDocCtx:u(S).default,CowXMLDocCtx:u(T).default};var{chaosGl:h,chaosEval:m,NOOP:kt,ANOOP:J,R:M}=f,{CowHTMLDocCtx:O,CowXMLDocCtx:Dt}=R,N=class o extends v{static ChubML=o;ChubML=o;static#e=new L("chubstart");static#n=new L("chubinjected");static#t(t,...e){h.chubDev&&console.log("ChubML: "+t,...e)}static#d(t,e){let r=e.attributes;switch(t.name.toLowerCase()){case"chubexp":case"chubexps":for(let i of t.value.split(";")){let a=Function("self",i);if(a)var s=a.apply(i,[e]);this.#t(`${i} -> ${s}`)}r.removeNamedItem(t.name);break;case"chublive":let n=parseFloat(t.value);isNaN(n)?this.#t(`Invalid value for ${t.name} attribute.`):n=this.#f(n,e,t),r.removeNamedItem(t.name);break}this.#t(t.name,t.value)}static#f(t,e,r){let s=setInterval(()=>{this.#t("Requesting Frame for ",e,"...",t),t=this.#b(t,e,r)},t);return Reflect.set(e,"chubLiveReload",s),t}static#i=new WeakMap;static#a=new WeakMap;static#b(t,e,r){return h.requestAnimationFrame&&h.requestAnimationFrame(async s=>{isNaN(t)||this.#i.has(e)||(this.#i.set(e,s),await this.handleLiveReload(e),this.#t(`HTML updated and scripts executed every ${t}ms`,e,s),this.#i.delete(e),t=parseFloat(r.value))}),t}static async handleLiveReload(t){let e=t.innerHTML;t.innerHTML=e;let r=t.querySelectorAll("script"),s=Array.from(r).filter(n=>!n.hasAttribute("data-doneBefore")||n.hasAttribute("data-forceReload"));this.#t("scripts found",r.length,s);for(let n of s){let i=document.createElement("script");for(let a of n.attributes)i.setAttribute(a.name,a.value);n.src?await this.#r(i,n):this.#p(i,n),n.parentNode?.replaceChild(i,n),this.#t(i.src,"script src updated")}}handleLiveReload=o.handleLiveReload.bind(o);static#p(t,e){t.textContent=e.textContent,t.setAttribute("data-doneBefore","true"),this.#t(t.src,"script src updated loose")}static async#r(t,e){let[r,s]=await this.#h(e.src);if(!r)return this.#t("Failed to load script",e.src);this.#t("Script loaded",e.src);let n=this.#a.get(e),i=n&&n===e.textContent||e.dataset.forceReload||s.status==304;if(i)return this.#t("Script is 304",e.src);this.#t("Script is not 304",e.src);let a=await(await fetch(e.src)).text();t.textContent=a,this.#a.set(e,a),this.#t("script source updated",t.src,a,i,n,s,r)}static#o(){let t=document.querySelectorAll("*");for(let e of t)for(let r of e.attributes)this.#d(r,e);h.chubDev&&console.log("Custom attrs handled.",t.length,"elements.",t)}static{h.lastChub||="",h.cbMode||="",h.chubLocation||="",h.chubDev||=!1,h.__lastQD=null,h.__lastQM=null,h.onload&&(window.onload=()=>{this.#e.detail=this,this.#e.activate()}),h.chubinjected.globals.push(()=>{this.#o()})}#C=t=>({tag:"",id:"",class:"",content:"",data:"",style:"",attr:"",indent:0,[Symbol.unscopables]:{_:t||null,atBucket:[],data:{}}});static _(t){return Reflect.get(t,Symbol.unscopables)}un=o._.bind(o);static _b(t){return this._(t).atBucket}ab=o._b.bind(o);static _d(t){return this._(t).data}dt=o._d.bind(o);#g=()=>({str:0,tmp:0});#M=["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr","!DOCTYPE","?xml"];static#O(){return h.__lastQM}static get __lastQM(){return this.#O()}static#I(t){h.__lastQM=t}static set __lastQM(t){this.#I(t)}get __lastQM(){return o.__lastQM}set __lastQM(t){o.__lastQM=t}static#$(){return h.__lastQD}static get __lastQD(){return this.#$()}static#F(t){h.__lastQD=t}static set __lastQD(t){this.#F(t)}get __lastQD(){return o.__lastQD}set __lastQD(t){o.__lastQD=t}get doc(){return document}#E(t,e){typeof t=="string"?this.body.innerHTML=t:this.body.appendChild(t)}get body(){return document.body}set body(t){this.#E(t,this.body)}get head(){return document.head}set head(t){this.#E(t,this.head)}get html(){return document.documentElement}get title(){return document.title}set title(t){document.title=t}get chubLocation(){return h.chubLocation}set chubLocation(t){h.chubLocation=t}get chubDev(){return h.chubDev}set chubDev(t){h.chubDev=t}#H(t,e=""){return t||e}#l(t){return t?t+" ":""}s=v;styled={};#m=()=>this.s.Rexps;#_(t){let e=this.#m;return t.replace(e().lineWithComment,"").replace(e().formatspace2,`
`).replace(e().formatspace1,`
`)}#j(t){let e=t.split(/;/),r=[];for(let s of e)r.push({c:s.trim(),i:s.search(/\S/)});return r}#q(t){let e=[],r=[];for(let s of t){let n=s.i,i={c:s.c,i:n,children:[],parent:null};for(;r.length>0&&n<=r[r.length-1].i;)r.pop();if(r.length>0){let a=r[r.length-1];i.parent=a,a.children.push(i)}else e.push(i);r.push(Object.assign(i))}return e}#B(t){let e=this.#m,r=t[1],s=r.includes("src="),n=r.includes(`"execafter";
`);var i=!0;let a=async l=>{let b=await(await fetch(l)).text();try{let p=document.createElement("script");p.type="text/javascript",p.text=b,document.body.appendChild(p)}catch(p){console.log(p,this.s.errorList.scripterror)}};if(s&&t){let l=r.match(/src="(.*)"/);l&&(a(l[1]),i=!1)}if(t!==null&&i)try{m(r)}catch(l){console.error(l,this.s.errorList.scripterror)}}#c(t){let e=this.attrSyn(t).join("");return this.#H(e)}async#Q(t,e){window?.location?.origin&&(e=`${e}`.includes("{{ORIG}}")?e.replace("{{ORIG}}",window.location.origin):e),t.tag=t.tag?t.tag:"fetcherBlock",t.data=`${this.#l(t.data)}data-fetchw="${e}" data-instance="${new Date().getTime()}"`;let s=await(await fetch(await this.findFile([e]))||{text:()=>e}).text();if(t.content=`${s||""}`,window?.location?.origin){let n=this.$(`[${t.data.split(" ").join("][")}]`);n&&(n.innerHTML=t.content.replace(/\n/g,`
</br>
`))}return e}#It(t,e){let r=t[e];return t[e]="",r}#v(t,e,r=" "){let s=t[e],[n,...i]=(s+"").split(r);return t[e]=i.join(r),n}#s(t,e=!0){return e?this.#W(t,"id"):this.#v(t,"id")}#k(t){return t?m(t):{}}#W(t,e,r=void 0){if(!t[e])return r;try{var s=this.#k(this.#v(t,e))}catch{}return s||r}#U=Object.getPrototypeOf(J).constructor;promiseBucket=[];async#G(t){let e=this.promiseBucket.push(t);await t,this.promiseBucket[e]===t&&this.promiseBucket.splice(e,1)}#y(t,e,r=!1){let s=this.attrSyn(e).join(";"),n=this.#s(t);try{let c=(r?this.#U:Function).apply(t,["param","ctx","self","promiseBucket",s])(n,this.un(t),t,()=>Promise.all(this.promiseBucket));c instanceof Promise&&this.#G(c);var i=r?void 0:c?.toString?.()}catch(a){return console.error(a,this.s.errorList.scripterror)}this.chubDev&&console.log("AtEval: ",i),i&&this.#w(t,i)}#z(t,e){this.#y(t,"return "+e),this.chubDev&&console.log("AtRet: ",e)}#w(t,e){let r=this.#c(e);t.content+=r,this.chubDev&&console.log("AtPutCont: ",e)}#X(t,e){let r=this.#c(e),s=m(r);if(!s)throw new d("CowErr","No Call: "+r);if(typeof s!="function")throw new d("CowErr","Not a function: "+r);let n=this.#s(t),i=s.bind(t)(t,n);i&&this.#w(t,i),this.chubDev&&console.log("Call: ",i)}#Z(t,e){let r=this.#c(e),s=this.$(r);if(!s)throw new d("CowErr","No Template: "+r);let n=s.innerHTML;t.content+=n,this.chubDev&&console.log("AtFrom: ",n)}#J(t,e){let r=this.#s(t),s=this.beamMake(e,!0),n=this.dt(t),i;n.doImport=a=>(delete n.doImport,i=a),s.then(({doc:a})=>{if(n.onImport?.(t),r){let l=this.$(r);if(!l)throw new d("CowErr","No Storage: "+r);l.innerHTML+=a}i&&(i.innerHTML+=a)})}#Y(t,e){let r=this.#c(e);Object.defineProperty(h,r,{get:()=>t,configurable:!0}),this.chubDev&&console.log("AtGlobalize: ",e)}#L(t,e,r,s){t[e]=r,Reflect.defineProperty(h,s,{get:()=>t[e],configurable:!0,enumerable:!0})}#K(t,e,r){this.#L(t,e,r,"__lastQD")}#V(t,e,r){this.#L(t,e,r,"__lastQM")}#tt(t,e){let[r,...s]=e.split("."),n=s.pop(),i=h[r];if(!n)throw new d("CowErr","No property name: "+e);if(!i)throw new d("CowErr","No Global Object: "+r);let a=i;for(let l of s)if(a=a[l],!a)throw new d("CowErr","No Property: "+l);this.#V(a,n,this.#s(t)),this.chubDev&&console.log("MkPrp: ",e,n,i,a,a[n])}#et(t,e){let r=this.#s(t);this.#K(h,e,this.#k(r)),this.chubDev&&console.log("AtDef: ",e)}#rt(t,e){let r=this.attrSyn(e),s=this.#s(t),n=this.#s(t,!1),i=m(r.join(";"));if(!s)throw new d("CowErr","No Parent: "+i);if(!n)throw new d("CowErr","No Property: "+i);Reflect.set(s,n,i),this.chubDev&&console.log("AtAsg: ",t,i,s)}#st(t,e){let r=this.#s(t)||globalThis,s=this.attrSyn(e)[0];if(!s)throw new d("CowErr","No Property: "+r);Reflect.deleteProperty(r,s),this.chubDev&&console.log("AtDelete: ",t,s,r)}#nt(t,e){if(typeof e!="string")return;this.chubDev&&console.log("using @: ",`${e}`.slice(1).split(/[=]/));let r=e.slice(1);function s(i){let a=r.slice(i+1);return o._b(t).push(a),a}function n(i){return r.startsWith(i)}switch(!0){case n("fetchw"):this.#Q(t,s(6));break;case n("eval"):this.#y(t,s(4));break;case n("*eval"):this.#y(t,s(5),!0);break;case n("ret"):this.#z(t,s(3));break;case n(":"):this.#w(t,s(0));break;case n("call"):this.#X(t,s(4));break;case n("from"):this.#Z(t,s(4));break;case n("import"):this.#J(t,s(6));break;case n("gl"):this.#Y(t,s(2));break;case n("mkprp"):this.#tt(t,s(5));break;case n("def"):this.#et(t,s(3));break;case n("asg"):this.#rt(t,s(3));break;case n("del"):this.#st(t,s(3));break;default:console.log("no valid @ found...?",e)}return e}#S(t,e,r,s=1){let n=r.slice(s);t[e]=this.#l(t[e].toString())+n}#it(t,e){this.#S(t,"class",e)}#at(t,e){this.#S(t,"id",e)}#ot(t,e){this.#T(t,e,"data",!0)}#lt(t,e){this.#T(t,e,"attr",!1)}#T(t,e,r,s){let n=this.attrSyn(e);if(!n)return;let i=n[0].slice(1),a=n[1],l=`${i}="${a}"`;s&&(l=`data-${l}`),t[r]=this.#l(t[r])+l}#ct(t,e,r){switch(t[0]){case"#":this.#at(e,t);break;case".":this.#it(e,t);break;case"$":this.#ot(e,t);break;case"%":this.#lt(e,t);break;case"@":t=this.#nt(e,t);break;default:e.tag=this.#l(e.tag)+t}return t}#N=(t,e)=>e.forEach((r,s)=>{r=this.#ct(r,t,s)});#x(t,e,r,s=""){let n=this.#m,i=" ",a=t.c,l=a.search(n().betweenQuote),c=a.search(n().betweenCol),b=a.match(n().script),p=this.#C(t),g=this.#C(t);if(b!==null&&this.#B(n().script.exec(a)),l!==-1){let y=a.split(n().betweenCol),$=a.split(n().betweenQuote)[1];if(c==null)g=p;else{var I=y[1]||"";this.#N(g,I.split(" "))}r.str++,g.content=$}else this.#N(g,a.split(" "));return t.o=g,t.i=i.repeat(e),t.children&&t.children.forEach(y=>this.#x(y,e+1,r,s)),[t,r]}#ht(t,e=this.#g()){let{o:r}=t,s=!1;if(!r)throw new d("No CIL object found!");switch(r.tag){}return{isTemplate:s}}#ut(t,e){for(let r in this.styled)if(this.styled[r]===!0&&this.styled.styles[r]){let s=e?"":`
`;t=t.replace("<head>","<head>"+s+this.styled.styles[r]),this.styled[r]="has"}return t}#dt(t,e){return t.includes("head")&&(t=this.#ut(t,e)),t}#ft(t,e){let r=n=>!!n,s=n=>t+=n;return r(e.class)&&s(` class="${e.class}"`),r(e.id)&&s(` id="${e.id}"`),r(e.style)&&s(` style="${e.style}"`),r(e.data)&&s(` ${e.data}`),r(e.attr)&&s(` ${e.attr}`),t}#bt(t,e,r,s,n,i){let a=i?"":`
`;t=a+`${i?"":e.i}<${r.tag}`,t=this.#ft(t,r),s>0&&(n=!0);let l=">"+a,c=" /",b=r.tag.toLowerCase();switch(!0){case b.startsWith("!"):c="";break;case b.startsWith("?"):c=" ?";break}return n&&(t+=c),t+=l,{html:t,shorter:n}}#pt(t,e,r,s){let n=s?"":t.i;switch(t.c[0]){case'"':e+=n+t.c.slice(1,t.c.length-1);break;default:e+=n+this.#A(t,r,s)}return e}#gt(t,e,r,s){for(let n of t.children)e=this.#pt(n,e,r,s);return e}#mt(t,e,r,s,n){let i=n?"":`
`;return t+=i,e||(t+=`${n?"":r.i}</${s.tag}>${i}`),t.includes("<>")&&(t=t.replace("<>","").replace("</>","")),t}#yt(t,e,r){let s=r?"":t.i+" ";return t.o?.content&&(e+=s+t.o.content),e}#A(t,e=this.#g(),r=!1){let s=t.o;if(!s)throw new d("No CIL object found!");let n=!1,i=this.arrMatch(s.tag,this.#M),a=i.count,l=i.list,c="",{isTemplate:b}=this.#ht(t,e);return{html:c,shorter:n}=this.#bt(c,t,s,a,n,r),c=this.#yt(t,c,r),c=this.#gt(t,c,e,r),c=this.#mt(c,n,t,s,r),c=this.#dt(c,r),c}#wt(t,e,r=""){let s=this.#g();return[t,s]=this.#x(t,0,s,r),this.#A(t,s,e)}parse(t,e){let r=this.#_(t),s=this.#j(r),n=this.#q(s);return this.#wt(n[0],e)}async findFile(t){for(let r of t){try{var e=await fetch(r)}catch(s){console.error(`Error fetching file from '${r}':`,s)}if(e?.ok)return r}return null}ChubRep(t,e="<!DOCTYPE html>"){return t=this.parse(t,!0),O.manualWrite(e+`
`+t)}injectChub(t){var e=this.parse(t,!0);this.chubDev==!0&&console.log(e);let r=this.chubLocation||"chub",s=this.$(r);s?s.innerHTML=e:r="body",o.#n.detail=s,o.#n.activate()}Router=class{__env__=P();constructor(){switch(this.__env__){case"Node":break;case"Browser":break}}};CHUBfax(t,e=" "){let r=t||"";return r=r.replaceAll("=","|e|").replaceAll(";","|c|").replaceAll('"',"|q|").replaceAll(e,"|"),r}attrSyn(t){if(`${t}`.match(/=/gm)?.length>1)throw this.s.errorList.eqspl3;let e=" spcfork.Equals.Token ",r=" spcfork.Pipe.Token ";return t.replaceAll("=",e).replaceAll("\\|",r).replaceAll("|e|","=").replaceAll("|c|",";").replaceAll("|q|",'"').replaceAll("|"," ").replaceAll(r,"|").split(e)}async CHUBWFetch(t){let e=await fetch(t);if(!e?.ok)throw new Error(`HTTP error! Status: ${e.status}`);let r=await e.text();return this.HTMLToChub(r)}getURLbit(){var t=window.location.href,e=t.split("/"),r=e[e.length-1];return r}CHUBsanitize(t){var e=document.createElement("div");e.innerText=t;var r=e.innerHTML;return r}DupeCollection={};CHUBduper(t="p;"){this.DupeCollection||(this.DupeCollection={}),this.DupeCollection[t]!==void 0?this.DupeCollection[t]+=1:this.DupeCollection[t]=0;let e;if(t.includes(";")){let s=t.split(";");s[0]+=` #${this.DupeCollection[t]}`,e=s.join("")}else e=t+";";let r={editedDupe:e,D:this.DupeCollection,s:()=>JSON.stringify(this.DupeCollection),c:()=>JSON.parse(r.s())};return r}CHUBstrprep(t){return t.replace(/[.*+?^${}()|[\]\\"';:]/g,"\\$&").replace(/[;]/g,"|c|")}CHUBunmess(t){let r=t.replace(/\\\"/g,'"').replace(/\\\|c|/g,",");return[r,JSON.parse(r)]}MLTextToNodes(t,e="text/html"){return new DOMParser().parseFromString(t,e)}Chubify(t){let e=this.parse(t,!0);return this.MLTextToNodes(e,"text/html")}HTMLToChub=(t,e="")=>{t instanceof Element&&(t=t.outerHTML);let r=this.MLTextToNodes(t,"text/html");return this.getChubML(r.documentElement,"",e)};getChubML=(t,e="",r="")=>{let s="";if(s+=`${e}${t.nodeName.toLowerCase()}`,t.attributes.length>0){let n=Array.from(t.attributes);s=this.#vt(n,s)}return t.childNodes.length>0?s=this.#Et(s,t,e,r):s+=`;
`,s};#Ct(t,e){if(!t.textContent)return"";let r=t.textContent.trim();return r!=""?`${e} "${r}";
`:""}#Et(t,e,r,s){t+=`;
`;let n=Array.from(e.childNodes);for(let i of n)switch(i.nodeType){case Node.TEXT_NODE:t+=this.#Ct(i,r);break;case Node.ELEMENT_NODE:t+=this.getChubML(i,r+" ",s);break}return t+=`${r}${s}
`,t}#vt(t,e){let r=s=>this.CHUBfax(s.value);for(let s of t)switch(s.name.toLowerCase()){case"class":e+=` .${r(s)}`;break;case"id":e+=` #${r(s)}`;default:e+=` %${s.name}=${r(s)}`}return e}aliasIndexes=["beam.lmc","beam.chub","index.lmc","index.chub"];static async#h(t,e={}){let r=await fetch(t,{method:"HEAD",...e});return[r.ok,r]}#D=o.#h;static async#kt(t){let[e,r]=await this.#h(t);if(!e)throw new Error("File not found!");return{req:await fetch(t),okRes:r}}#Lt=o.#kt.bind(o);static async#St(t){for(let e of t){let[r,s]=await this.#h(e);if(r)return e}return null}#Tt=o.#St.bind(o);#Nt=(t,e)=>new Promise(async(r,s)=>{try{await t(e,r)}catch(n){s(n)}});async#xt(t){let e=[],r=new AbortController,s=async(i,a)=>{if(r.signal.aborted)throw new Error("Aborted");let[l]=await this.#D(i,{signal:r.signal});l&&r.abort(),a([i,l])};for(let i of t)e.push(this.#Nt(s,i));let n=await Promise.all(e.map(i=>i.catch(a=>["",!1])));for(let[i,a]of n)if(a)return i;return null}async#At(t,e=!1){let[r,s]=await this.#D(t);return r?t:await(e?this.#Tt:this.#xt)(this.aliasIndexes)}async beamPrep(t,e){let r=await this.#At(t,e);if(!r)throw new Error("File not found!");let s=await this.#Lt(r);return h.lastChub=s,{...s,location:t}}async beamParse(t,e=""){let r=await t.text(),s=this.parse(r,!0);return{text:r,doc:s,location:e}}async beamMake(t,e){let{req:r}=await this.beamPrep(t,e);return await this.beamParse(r,t)}async beamDo(t,e){let{text:r,doc:s,location:n}=await this.beamParse(t);this.beamRender(s,e,{location:n,text:r})}beamRender(t,e,r={}){this.chubDev&&console.log(t);let s=e||this.chubLocation||"chub",n=typeof s=="string"?this.$(s)||document.body:s;n.innerHTML=t,o.#n.detail={nodes:n,text:t,...r},o.#n.activate()}async beamSync(t,e,r=!1){let{req:s}=await this.beamPrep(t,r);return async()=>await this.beamDo(s,e)}async beamChub(t,e,r=!1){return await(await this.beamSync(t,e,r))()}constructor(){super();try{this.#Dt()}catch{}try{this.#Pt(process.argv)}catch{}}#Dt(){}#u;#P;async#Pt(t){if(t.length==0)return;t[0]=="node"&&t.shift();let[,e,...r]=t;this.#u=M("fs"),this.#P=M("readline"),r[0]=="parse"&&this.#Rt(r),r[0]=="transpile"&&this.#Mt(r),r[0]=="write"&&await this.#Ot(r,this.#P.createInterface({input:process.stdin,output:process.stdout}))}#R(t,e){let r=this.#u.readFileSync(t,"utf-8");return this.parse(r,e)}#Rt(t){let e=t[1];if(!e)throw new Error("No file specified!");console.log("Parsing file:",e);let r=this.#R(e);console.log(r)}#Mt(t){let[,e,r,...s]=t;if(!e)throw new Error("No file specified!");if(!r)throw new Error("No export specified!");console.log("Transpiling...",e);let n=!!s.find(l=>l=="--strict"||l=="-s");console.log("Strict mode:",n);let i=this.#R(e,n);console.log("Transpiled!");let a=(l,c)=>`export default (new DOMParser).parseFromString(\`${l}\`, "${c}")`;switch(!0){case r.endsWith(".html"):break;case r.endsWith(".p.js"):i=a(i,"text/html");break;case r.endsWith(".x.js"):i=a(i,"text/xml");break;case r.endsWith(".xh.js"):i=a(i,"application/xhtml+xml");break;case r.endsWith(".w.js"):i=["{",O,"{","const d = document;","d.open();",`try { d.write(\`${i}\`) } catch {};`,"d.close();","}","}"].join("");break;case r.endsWith(".js"):i=`export default \`${i}\``;break}this.#u.writeFileSync(r,i),console.log("Wrote!",r)}async#Ot(t,e){let r=[],s=()=>r.join(`
`),n=t[1],i="",a="";t:for(;i.toLowerCase()!=="eof";){console.clear(),process.stdin.write("Write to file until EOF or CTRL+C"),process.stdin.write(`
`),process.stdin.write(s()),process.stdin.write(`
`);let l=new Promise(c=>e.question("",c));switch(e.write(a),i=await l,i.toLowerCase()){case"back":a=r.pop()||"";break;case"clr":a="";break;case"new":a="",r=[];break;case"eof":console.clear();break t;default:r.push(i),a=""}}console.log("Writing to file... ",n),this.#u.writeFileSync(n,s()),console.log("Done!"),process.exit(0)}},Y=new N,K=u(Y,["ChubML","ChubMLMod"]).default;
//# sourceMappingURL=cml.js.map