@ryusei/code
Version:
<div align="center"> <a href="https://code.ryuseijs.com"> <img alt="RyuseiCode" src="https://code.ryuseijs.com/images/svg/logo.svg" width="70"> </a>
14 lines (13 loc) • 10.1 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"undefined"!=typeof exports?e():(e(),t.unknown={})}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(){"use strict";function i(t,e){return(i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}
/*!
* RyuseiCode.js
* Version : 0.1.17
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
/*!
* RyuseiLight.js
* Version : 1.2.0
* License : MIT
* Copyright: 2020 Naotoshi Fujita
*/var t="keyword",s="comment",o="tag",a="tag.close",e="selector",n="atrule",u="attr",r="prop",c="value",f="entity",l="cdata",d="prolog",m="identifier",p="string",g="number",h="boolean",b="function",v="class",k="decorator",S="regexp",y="operator",w="bracket",x="delimiter",$="space",C="text";function A(t,e){if(t)for(var n=Object.keys(t),r=0;r<n.length;r++)e(t[n[r]],n[r])}function F(t,e){if(void 0===e&&(e=""),!t)throw new Error(e)}function T(t,e){return t.charAt(0)===e}var _=!(void 0===/x/.sticky),j=/[+-]?(\d+\.?\d*|\d*\.?\d+)([eE][+-]?\d+)?/,z=/\b(?:true|false)\b/,q=/[[\]{}()]/,D=/[ \t]+/,E=/'(?:\\'|.)*?'/,O=/"(?:\\"|.)*?"/,B=/\/\*[\s\S]*?(\*\/|$)/,R=/\/\/.*/,H=/\b(?:break|catch|class|continue|do|else|extends|finally|for|function|if|implements|in|instanceof|interface|new|null|return|throw|try|while)\b/;function J(){return{id:"css",name:"CSS",grammar:{main:[["#common"],["#findSingleAtrule"],["#findBlock"]],findBlock:[["#block",/(?:(?![\t\n\r ;\{\}])[\s\S])(?:(?![;\{\}])[\s\S])*\{[\s\S]*?\}/,"@rest"]],findSingleAtrule:[["#atrule",/@(?:(?![;\{])[\s\S])+?;/]],findAtrule:[["#atrule",/@(?:(?![;\{])[\s\S])*?(?=[;\{])/]],findSelector:[["#selector",/(?:(?![\t\n\r ;\{\}])[\s\S])[\s\S]*?(?=\{)/]],common:[[p,/(["'])[\s\S]*?(?:(?!\\)[\s\S])\1/],[s,B],[$,D]],block:[["#inner",/{/,"@rest"],[w,/}/,"@break"],["#findAtrule"],["#findSelector"],[$,D]],inner:[[w,/{/],["#common"],["#findBlock"],["#props"],["#findAtrule"],["",/}/,"@back"]],atrule:[["#common"],["#url",/\burl\(/,"@rest"],[$,D],[n,/[^\s();]+/],[x,/[:;,]/],["#paren",/\(/,"@rest"]],paren:[[w,/^\(/],["#common"],["#paren",/\(/,"@rest"],[w,/\)/,"@break"],["#props"]],selector:[["#common"],[y,/[>+~]/],[w,/[[\]()]/],[x,/=/],[e,/::?\S+/],[e,/[\W\d]\S+/],[o,/\b[a-z]+|\*/i],[e,/\S+/]],url:[["#common"],[b,/^url/],[w,/\(/],[p,/[^)]+/],[w,/\)/,"@break"]],props:[[r,/[a-z0-9-_\xA0-\uFFFF]+(?=:)/i],["#url",/\burl\(/,"@rest"],[b,/\b[\w-]+(?=\()\b/],[t,/!important|\b(?:initial|inherit|unset)/],[r,/[a-z0-9-]+(?=:)/],[g,/#([0-9a-f]{6}|[0-9a-f]{3})/i],[g,/\bU\+[0-9a-f?-]+/i],[g,/[+-]?(\d+\.?\d*|\d*\.?\d+)/],[x,/[:;,]/],["#paren",/\(/,"@rest"],[w,/[[\])]/],[$,D]]}}}function L(){return{id:"javascript",name:"JavaScript",alias:["js"],source:{func:/[_$a-z\xA0-\uFFFF][_$a-z0-9\xA0-\uFFFF]*/},grammar:{main:[[p,E],[p,O],["#backtick",/`/,"@rest"],[s,B],[s,R],[S,/\/(\[.*?]|\\\/|.)+?\/[gimsuy]*/],[t,H],[t,/\b(?:as|async|await|case|catch|const|debugger|default|delete|enum|export|from|import|let|package|private|protected|public|super|switch|static|this|typeof|undefined|var|void|with|yield)\b/],[t,/\b((get|set)(?=\s+%func))/i],[v,/\b[A-Z][\w$]*\b/],[b,/%func(?=\s*\()/i],[h,z],[k,/@[^\s(@]+/],[m,/\b[a-z_$][\w$]*\b/],[g,j],[y,/=>/],[y,/\+[+=]?|-[-=]?|\*\*?=?|[/%^]=?|&&?=?|\|\|?=?|\?\??=?|<<?=?|>>>=?|>>?=?|[!=]=?=?|[~:^]/],[w,q],[x,/[;.,]+/],[$,D]],backtick:[[p,/^`/],[p,/(\$[^{]|\\[$`]|[^`$])+/],["#expression",/\${/,"@rest"],[p,/`/,"@break"]],expression:[[x,/^\${/],[x,/}/,"@break"],["#main"]]}}}var M=function(t){var e,n;function r(){return t.apply(this,arguments)||this}return n=t,(e=r).prototype=Object.create(n.prototype),i(e.prototype.constructor=e,n),r.prototype.run=function(t,e){for(var n=this.tokenize(t,e),r=0;r<n.length;r++)for(var i=n[r],s=0,o=0;o<i.length;o++){var a=i[o],u=a[1].length,c=a[2],f="rc__token rc__"+a[0].split(".")[0],l=a[1].replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");a[2]={category:a[0],code:a[1],html:'<code class="'+f+'">'+l+"</code>",from:s,to:s+u,index:o,state:c.state,depth:c.depth,head:c.head,tail:c.tail,distance:c.distance,language:c.language,split:c.split},s+=u}return n},r}(function(){function t(t){this.language=t,this.init(t)}var e=t.prototype;return e.init=function(n){var r=this;A(n.grammar,function(t,e){n.grammar[e]=r.merge(n,t)}),A(n.use,this.init.bind(this))},e.merge=function(e,t){for(var r=[],n=0;n<t.length;n++){var i=t[n],s=t[n],o=s[0],a=s[1];T(o,"#")&&!a?r.push.apply(r,this.merge(e,e.grammar[o.slice(1)])):function(){var t=a.toString().match(/[gimsy]*$/)[0].replace(/[gy]/g,""),n=a.source+(_?"":"|()");A(e.source,function(t,e){n=n.replace(new RegExp("%"+e,"g"),t.source)}),i[1]=new RegExp(n,(_?"y":"g")+t),r.push(i)}()}return r},e.parse=function(t,e,n,r){var i=0,s=0;this.depth++;t:for(;i<t.length&&!this.aborted;){for(var o=0;o<n.length;o++){var a=n[o],u=a[1],c=a[2];u.lastIndex=i;u=u.exec(t);if(u&&u[0]){if(s<i&&this.push([C,t.slice(s,i)],e,r),"@back"===c){s=i;break t}s=i+=this.handle(u,e,a,r)||1;if("@break"===c)break t;continue t}}i++}return s<i&&this.push([C,t.slice(s)],e,r),this.depth--,i},e.push=function(t,e,n){for(var r=this.depth,i=t[0],s=t[1],o=this.index,a=0,u=0;-1<a&&!this.aborted;){var a=s.indexOf("\n",u),c=this.lines[this.index],f=u===a&&!c.length,l=f?"\n":s.slice(u,a<0?void 0:a),d={depth:r,language:e.id,state:n};l&&(i!==C&&(d.head=-1<a&&!u,d.tail=a<0&&!!u,d.split=-1<a||!!u,d.distance=this.index-o),c.push([i===C&&f?"lb":i,l,d])),-1<a&&(this.index++,this.aborted=this.limit&&this.index>=this.limit,this.aborted||(u=a+1,this.lines[this.index]=[]))}},e.handle=function(t,e,n,r){var i=n[0];if(!i)return 0;var s=t[0];if("@debug"===n[3]&&console.log(s,n),T(i,"@")){F(e.use);var o=e.use[i.slice(1)];return F(o),this.parse(s,o,o.grammar.main,i)}if(T(i,"#")){o=e.grammar[i.slice(1)];return F(o),"@rest"===n[2]&&(s=t.input.slice(t.index)),this.parse(s,e,o,i)}return this.push([i,s],e,r),s.length},e.tokenize=function(t,e){return this.lines=[[]],this.index=0,this.depth=-1,this.limit=e||0,this.aborted=!1,this.parse(t,this.language,this.language.grammar.main,"#main"),this.lines},t}());function P(t,e){return-1<t.indexOf(e)}function U(r){for(var t=arguments.length,e=new Array(1<t?t-1:0),n=1;n<t;n++)e[n-1]=arguments[n];return e.forEach(function(n){var t;null!==(t=n)&&"object"==typeof t&&function(t,e){if(t)for(var n=Object.keys(t),r=0;r<n.length&&!1!==e(t[n[r]],n[r]);r++);}(n,function(t,e){void 0!==n[e]&&(r[e]=n[e])})}),r}function W(){var t=J();return U({id:t.id,language:t,lexer:new M(t)},{lineComment:["/*","*/"],blockComment:["/*","*/"],multiline:[["/*","*/",s]],indent:[[/{$/,/^}/,t=["!comment","!string"]]],autoClose:[["(",")",t={close:t,skip:t,remove:t}],["[","]",t],["{","}",t],["'","'",t={close:"@quotes",skip:"@quotes",remove:"@quotes"}],['"','"',t]]})}function Y(){var t=L();return U({id:t.id,language:t,lexer:new M(t)},{lineComment:["//",""],blockComment:["/*","*/"],multiline:[["`","`",p,"#backtick"],["/*","*/",s]],indent:[[/\($/,/^\)/,t=["!comment","!string"]],[/\[$/,/^]/,t],[/{$/,/^}/,t]],autoClose:[["(",")",t={close:t,skip:t,remove:t}],["[","]",t],["{","}",t],["'","'",t={close:"@quotes",skip:"@quotes",remove:"@quotes"}],['"','"',t],["`","`",{close:Z,skip:G,remove:G}]]})}function Z(t){var e=t.Components,n=e.Input,t=n.info;if(t&&t.category===p)return!1;n=n.after;return!e.Scope.isIn("comment")&&(!n||/^\s/.test(n))}function G(t){var e=t.Components,t=e.Selection.get().start,e=e.Code.lines.getInfoAt(t);return!(!e||e.category!==p)&&0===(e=[(t=t)[0],e.to-1],t[0]-e[0]||t[1]-e[1])}var I=["area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],K=500;function N(t){var e,n=(t=void 0===t?{}:t).script||{language:Y(),use:{config:Y(),code:"<script>",depth:2}},r=t.style||{language:W(),use:{config:W(),code:"<style>",depth:2}},i=(e=((i=void 0===(i={script:function(){return n.language.language},style:function(){return r.language.language}})?{}:i).script||L)(),t=(i.style||J)(),i=[l,/<!\[CDATA\[[\s\S]*\]\]>/i],e.grammar.main.unshift(i),t.grammar.main.unshift(i),{id:"html",alias:["markup"],name:"HTML",use:{script:e,style:t},grammar:{main:[[s,/<!\x2D\x2D[\s\S]*?\x2D\x2D>/],[d,/<!DOCTYPE[\s\S]*?>/i],i,["#script",/<script[\s\S]*?>[\s\S]*?<\/script>/],["#style",/<style[\s\S]*?>[\s\S]*?<\/style>/],["#tag",/<[\s\S]*?>/],[f,/&[\da-z]+;|&#\d+;/i],[$,D]],script:[["#tag",/^<script[\s\S]*?>/],i,["@script",/[\s\S]+(?=<\/script>)/],["#tag",/<\/script>/]],style:[["#tag",/^<style[\s\S]*?>/],["@style",/[\s\S]+(?=<\/style>)/],["#tag",/<\/style>/]],tag:[["#closeTag",/<\/.+>/],["#tagContent"]],closeTag:[[a,/[^\s/<>"'=]+/],["#tagContent"]],tagContent:[["#attr",/[\t\n\r ]+[\s\S]+(?=[\t\n\r \/>])/],[o,/[^\s/<>"'=]+/],[w,/[<>]/],[x,/[/]/]],attr:[[$,D],[c,/(['"])(\\\1|.)*?\1/],[x,/[/=]/],[u,/[^\s/>"'=]+/]]}});return U({id:i.id,language:i,lexer:new M(i),use:((i={})[n.language.id]=n.use,i[r.language.id]=r.use,i)},{lineComment:["\x3c!--","--\x3e"],blockComment:["\x3c!--","--\x3e"],multiline:[["\x3c!--","--\x3e",s]],indent:[[V,/^<\/\w+/]],autoClose:[["'","'",i={close:tt,skip:et,remove:et}],['"','"',i],[">",rt],["/",it],["=",'""',{close:Q,offset:1}]]})}function Q(t){t=t.Components;return t.Scope.isIn(["#attr","#tag","#openTag","!value"])&&/(^|[ \t])[^\s/<>"'=]+=$/.test(t.Input.before)}function V(t){return!!nt(t)}function X(t,e,n){t=t.Components;if(t.Scope.isIn(["#attr"])){if(n)return!0;t=t.Input;return new RegExp("=\\s*?"+e[0]+"$").test(t.before)}}function tt(t,e){return X(t,e,!1)}function et(t,e){return X(t,e,!0)}function nt(t){var e=t.Components,n=e.Selection.get(),r=n.start[1]-1;if(0<=r){var i=e.Code,t=e.Code.lines,e=n.start[0],n=t.getInfoAt([e,r]);if(n&&n.category===w&&">"===n.code){r=t.scanUp([e,n.from],[w,/</]),t=r.row,r=r.info,n=i.sliceRange([t,r.from],[e,n.to]),n=/<([^\s/<>"'=]+)/.exec(n);if(n&&!P(I,n[1]))return n[1]}}}function rt(t){t=nt(t);return t?"</"+t+">":""}function it(t){var e=t.Components,n=e.Selection.get().start,t=e.Code.lines,e=t.scanUp(n,[a,/./],null,0,K),n=t.scanUp(n,[o,/./],null,0,K);if(n&&e&&(n.row-e.row||n.info.from-e.info.from)<0)return">";n=n.info.code;return P(I,n)?">":n+">"}"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.register(N())});