@liascript/editor
Version:
An extended Markdown interpreter for the creation of interactive online courses.
2 lines (1 loc) • 4.25 kB
JavaScript
ace.define("ace/ext/beautify",["require","exports","module","ace/token_iterator"],(function(e,t,r){"use strict";function a(e,t){return e.type.lastIndexOf(t+".xml")>-1}var o=e("../token_iterator").TokenIterator;t.singletonTags=["area","base","br","col","command","embed","hr","html","img","input","keygen","link","meta","param","source","track","wbr"],t.blockTags=["article","aside","blockquote","body","div","dl","fieldset","footer","form","head","header","html","nav","ol","p","script","section","style","table","tbody","tfoot","thead","ul"],t.formatOptions={lineBreaksAfterCommasInCurlyBlock:!0},t.beautify=function(e){for(var r,s,n,i=new o(e,0,0),c=i.getCurrentToken(),l=e.getTabString(),m=t.singletonTags,u=t.blockTags,p=t.formatOptions||{},f=!1,h=!1,y=!1,d="",g="",b="",k=0,$=0,w=0,v=0,x=0,T=0,C=0,R=0,B=0,O=!1,I=!1,q=!1,_=!1,j={0:0},A=[],F=!1,S=function(){r&&r.value&&"string.regexp"!==r.type&&(r.value=r.value.replace(/^\s*/,""))},K=function(){for(var e=d.length-1;0!=e&&" "===d[e];)e-=1;d=d.slice(0,e+1)},L=function(){d=d.trimRight(),f=!1};null!==c;){if(R=i.getCurrentTokenRow(),i.$rowTokens,r=i.stepForward(),void 0!==c){if(g=c.value,x=0,q="style"===b||"ace/mode/css"===e.$modeId,a(c,"tag-open")?(I=!0,r&&(_=-1!==u.indexOf(r.value)),"</"===g&&(_&&!f&&B<1&&B++,q&&(B=1),x=1,_=!1)):a(c,"tag-close")?I=!1:a(c,"comment.start")?_=!0:a(c,"comment.end")&&(_=!1),!I&&!B&&"paren.rparen"===c.type&&"}"===c.value.substr(0,1)&&B++,R!==s&&(B=R,s&&(B-=s)),B){for(L();B>0;B--)d+="\n";f=!0,!a(c,"comment")&&!c.type.match(/^(comment|string)$/)&&(g=g.trimLeft())}if(g){if("keyword"===c.type&&g.match(/^(if|else|elseif|for|foreach|while|switch)$/)?(A[k]=g,S(),y=!0,g.match(/^(else|elseif)$/)&&d.match(/\}[\s]*$/)&&(L(),h=!0)):"paren.lparen"===c.type?(S(),"{"===g.substr(-1)&&(y=!0,O=!1,I||(B=1)),"{"===g.substr(0,1)&&(h=!0,"["!==d.substr(-1)&&"["===d.trimRight().substr(-1)?(L(),h=!1):")"===d.trimRight().substr(-1)?L():K())):"paren.rparen"===c.type?(x=1,"}"===g.substr(0,1)&&("case"===A[k-1]&&x++,"{"===d.trimRight().substr(-1)?L():(h=!0,q&&(B+=2))),"]"===g.substr(0,1)&&"}"!==d.substr(-1)&&"}"===d.trimRight().substr(-1)&&(h=!1,v++,L()),")"===g.substr(0,1)&&"("!==d.substr(-1)&&"("===d.trimRight().substr(-1)&&(h=!1,v++,L()),K()):"keyword.operator"!==c.type&&"keyword"!==c.type||!g.match(/^(=|==|===|!=|!==|&&|\|\||and|or|xor|\+=|.=|>|>=|<|<=|=>)$/)?"punctuation.operator"===c.type&&";"===g?(L(),S(),y=!0,q&&B++):"punctuation.operator"===c.type&&g.match(/^(:|,)$/)?(L(),S(),g.match(/^(,)$/)&&C>0&&0===T&&p.lineBreaksAfterCommasInCurlyBlock?B++:(y=!0,f=!1)):"support.php_tag"!==c.type||"?>"!==g||f?a(c,"attribute-name")&&d.substr(-1).match(/^\s$/)?h=!0:a(c,"attribute-equals")?(K(),S()):a(c,"tag-close")?(K(),"/>"===g&&(h=!0)):"keyword"===c.type&&g.match(/^(case|default)$/)&&F&&(x=1):(L(),h=!0):(L(),S(),h=!0,y=!0),f&&(!c.type.match(/^(comment)$/)||g.substr(0,1).match(/^[/#]$/))&&(!c.type.match(/^(string)$/)||g.substr(0,1).match(/^['"@]$/))){if(v=w,k>$)for(v++,n=k;n>$;n--)j[n]=v;else k<$&&(v=j[k]);for($=k,w=v,x&&(v-=x),O&&!T&&(v++,O=!1),n=0;n<v;n++)d+=l}if("keyword"===c.type&&g.match(/^(case|default)$/)?!1===F&&(A[k]=g,k++,F=!0):"keyword"===c.type&&g.match(/^(break)$/)&&A[k-1]&&A[k-1].match(/^(case|default)$/)&&(k--,F=!1),"paren.lparen"===c.type&&(T+=(g.match(/\(/g)||[]).length,C+=(g.match(/\{/g)||[]).length,k+=g.length),"keyword"===c.type&&g.match(/^(if|else|elseif|for|while)$/)?(O=!0,T=0):!T&&g.trim()&&"comment"!==c.type&&(O=!1),"paren.rparen"===c.type)for(T-=(g.match(/\)/g)||[]).length,C-=(g.match(/\}/g)||[]).length,n=0;n<g.length;n++)k--,"}"===g.substr(n,1)&&"case"===A[k]&&k--;"text"==c.type&&(g=g.replace(/\s+$/," ")),h&&!f&&(K(),"\n"!==d.substr(-1)&&(d+=" ")),d+=g,y&&(d+=" "),f=!1,h=!1,y=!1,(a(c,"tag-close")&&(_||-1!==u.indexOf(b))||a(c,"doctype")&&">"===g)&&(B=_&&r&&"</"===r.value?-1:1),r&&-1===m.indexOf(r.value)&&(a(c,"tag-open")&&"</"===g?k--:a(c,"tag-open")&&"<"===g?k++:a(c,"tag-close")&&"/>"===g&&k--),a(c,"tag-name")&&(b=g),s=R}}c=r}d=d.trim(),e.doc.setValue(d)},t.commands=[{name:"beautify",description:"Format selection (Beautify)",exec:function(e){t.beautify(e.session)},bindKey:"Ctrl-Shift-B"}]})),ace.require(["ace/ext/beautify"],(function(e){"object"==typeof module&&"object"==typeof exports&&module&&(module.exports=e)}));