UNPKG

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