@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) • 7.29 kB
JavaScript
!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 r="keyword",e="comment",o="tag",s="selector",u="atrule",f="prop",a="variable",c="string",l="number",d="function",h="operator",m="bracket",p="delimiter",b="space",v="text";function S(t){return void 0===t}function g(t,n){if(t)for(var r=Object.keys(t),e=0;e<r.length;e++)n(t[r[e]],r[e])}function k(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];return n.forEach(function(r){var t;null!==(t=r)&&"object"==typeof t&&g(r,function(t,n){S(r[n])||(e[n]=r[n])})}),e}function w(t,n){if(void 0===n&&(n=""),!t)throw new Error(n)}function y(t,n,r){n=function(t,n){for(var r=0;r<t.length;r++)if(t[r][0]===n)return r;return-1}(t,n);-1<n?t.splice.apply(t,[n,0].concat(r)):t.push.apply(t,r)}function A(t,n){return t.charAt(0)===n}var x=!S(/x/.sticky),F=/[ \t]+/,I=/\/\*[\s\S]*?(\*\/|$)/,_=/\/\/.*/;function n(){var t=k({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:[[c,/(["'])[\s\S]*?(?:(?!\\)[\s\S])\1/],[e,I],[b,F]],block:[["#inner",/{/,"@rest"],[m,/}/,"@break"],["#findAtrule"],["#findSelector"],[b,F]],inner:[[m,/{/],["#common"],["#findBlock"],["#props"],["#findAtrule"],["",/}/,"@back"]],atrule:[["#common"],["#url",/\burl\(/,"@rest"],[b,F],[u,/[^\s();]+/],[p,/[:;,]/],["#paren",/\(/,"@rest"]],paren:[[m,/^\(/],["#common"],["#paren",/\(/,"@rest"],[m,/\)/,"@break"],["#props"]],selector:[["#common"],[h,/[>+~]/],[m,/[[\]()]/],[p,/=/],[s,/::?\S+/],[s,/[\W\d]\S+/],[o,/\b[a-z]+|\*/i],[s,/\S+/]],url:[["#common"],[d,/^url/],[m,/\(/],[c,/[^)]+/],[m,/\)/,"@break"]],props:[[f,/[a-z0-9-_\xA0-\uFFFF]+(?=:)/i],["#url",/\burl\(/,"@rest"],[d,/\b[\w-]+(?=\()\b/],[r,/!important|\b(?:initial|inherit|unset)/],[f,/[a-z0-9-]+(?=:)/],[l,/#([0-9a-f]{6}|[0-9a-f]{3})/i],[l,/\bU\+[0-9a-f?-]+/i],[l,/[+-]?(\d+\.?\d*|\d*\.?\d+)/],[p,/[:;,]/],["#paren",/\(/,"@rest"],[m,/[[\])]/],[b,F]]}},{id:"scss",name:"SCSS"}),n=t.grammar;return k(n,{findBlock:[["#block",/([\*-_a-z]|#\{(?:(?!;)[\s\S])*?\}|((#\{(?:(?!;)[\s\S])*?\}|(?:(?![\t-\r ;\{\}\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])[\s\S]))(#\{(?:(?!;)[\s\S])*?\}|(?:(?![#;\{\}])[\s\S])|#(?:(?!\{)[\s\S]))+?))(?!#)\{[\s\S]*?\}/i,"@rest"]],findSingleAtrule:[["#atrule",/@(#\{|(?:(?![;\{])[\s\S]))+?;/]],findAtrule:[["#atrule",/@(#\{|(?:(?![;\{])[\s\S]))*?(?=[;\{])/]],findSelector:[["#selector",/(?:(?!;)[\s\S])*?(?:(?!#)[\s\S])(?=\{)/,""]],findInterp:[["#interp",/#{/,"@rest"]],common:[["#string"],[e,I],[e,_],[p,/;/],[b,F]],string:[["#singleQuote",/'/,"@rest"],["#doubleQuote",/"/,"@rest"]],singleQuote:[[c,/^'/],["#findInterp"],[c,/(\\'|#[^{]|[^'#])+/],[c,/'/,"@break"]],doubleQuote:[[c,/^"/],["#findInterp"],[c,/(\\"|#[^{]|[^"#])+/],[c,/"/,"@break"]],selector:[["#common"],["#findInterp"],[h,/[>+~]/],[m,/[[\]()]/],[p,/=/],[s,/::?\S+(?=#{)/],[s,/[\W\d]\S+(?=#{)/],[o,/\b[a-zA-Z]+\b|\*/],[s,/([^#\s]|#[^{\s])+/]],url:[["#common"],["#findInterp"],[d,/^url/],[m,/\(/],[c,/[^)]+(?=#{)/],[c,/[^)]+/],[m,/\)/,"@break"]],interp:[[p,/#{/],[p,/}/,"@break"],["#common"],["#props"]]}),n.inner.unshift(["#findInterp"]),y(n.atrule,"#url",[["#findInterp"]]),y(n.props,f,[["#findInterp"],[a,/\$[\w-_]+/]]),t}var j=function(t){var n,r;function e(){return t.apply(this,arguments)||this}return r=t,(n=e).prototype=Object.create(r.prototype),i(n.prototype.constructor=n,r),e.prototype.run=function(t,n){for(var r=this.tokenize(t,n),e=0;e<r.length;e++)for(var i=r[e],o=0,s=0;s<i.length;s++){var u=i[s],f=u[1].length,a=u[2],c="rc__token rc__"+u[0].split(".")[0],l=u[1].replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");u[2]={category:u[0],code:u[1],html:'<code class="'+c+'">'+l+"</code>",from:o,to:o+f,index:s,state:a.state,depth:a.depth,head:a.head,tail:a.tail,distance:a.distance,language:a.language,split:a.split},o+=f}return r},e}(function(){function t(t){this.language=t,this.init(t)}var n=t.prototype;return n.init=function(r){var e=this;g(r.grammar,function(t,n){r.grammar[n]=e.merge(r,t)}),g(r.use,this.init.bind(this))},n.merge=function(n,t){for(var e=[],r=0;r<t.length;r++){var i=t[r],o=t[r],s=o[0],u=o[1];A(s,"#")&&!u?e.push.apply(e,this.merge(n,n.grammar[s.slice(1)])):function(){var t=u.toString().match(/[gimsy]*$/)[0].replace(/[gy]/g,""),r=u.source+(x?"":"|()");g(n.source,function(t,n){r=r.replace(new RegExp("%"+n,"g"),t.source)}),i[1]=new RegExp(r,(x?"y":"g")+t),e.push(i)}()}return e},n.parse=function(t,n,r,e){var i=0,o=0;this.depth++;t:for(;i<t.length&&!this.aborted;){for(var s=0;s<r.length;s++){var u=r[s],f=u[1],a=u[2];f.lastIndex=i;f=f.exec(t);if(f&&f[0]){if(o<i&&this.push([v,t.slice(o,i)],n,e),"@back"===a){o=i;break t}o=i+=this.handle(f,n,u,e)||1;if("@break"===a)break t;continue t}}i++}return o<i&&this.push([v,t.slice(o)],n,e),this.depth--,i},n.push=function(t,n,r){for(var e=this.depth,i=t[0],o=t[1],s=this.index,u=0,f=0;-1<u&&!this.aborted;){var u=o.indexOf("\n",f),a=this.lines[this.index],c=f===u&&!a.length,l=c?"\n":o.slice(f,u<0?void 0:u),d={depth:e,language:n.id,state:r};l&&(i!==v&&(d.head=-1<u&&!f,d.tail=u<0&&!!f,d.split=-1<u||!!f,d.distance=this.index-s),a.push([i===v&&c?"lb":i,l,d])),-1<u&&(this.index++,this.aborted=this.limit&&this.index>=this.limit,this.aborted||(f=u+1,this.lines[this.index]=[]))}},n.handle=function(t,n,r,e){var i=r[0];if(!i)return 0;var o=t[0];if("@debug"===r[3]&&console.log(o,r),A(i,"@")){w(n.use);var s=n.use[i.slice(1)];return w(s),this.parse(o,s,s.grammar.main,i)}if(A(i,"#")){s=n.grammar[i.slice(1)];return w(s),"@rest"===r[2]&&(o=t.input.slice(t.index)),this.parse(o,n,s,i)}return this.push([i,o],n,e),o.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 C(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];return n.forEach(function(r){var t;null!==(t=r)&&"object"==typeof t&&function(t,n){if(t)for(var r=Object.keys(t),e=0;e<r.length&&!1!==n(t[r[e]],r[e]);e++);}(r,function(t,n){void 0!==r[n]&&(e[n]=r[n])})}),e}function t(){var t=n();return C({id:t.id,language:t,lexer:new j(t)},C(function(){var t=["!comment","!string"],n={close:t,skip:t,remove:t},r={close:"@quotes",skip:"@quotes",remove:"@quotes"};return{lineComment:["/*","*/"],blockComment:["/*","*/"],multiline:[["/*","*/",e]],indent:[[/{$/,/^}/,t]],autoClose:[["(",")",n],["[","]",n],["{","}",n],["'","'",r],['"','"',r]]}}(),{lineComment:["//",""]}))}"undefined"!=typeof window&&window.RyuseiCode&&window.RyuseiCode.register(t())});