File: dist/parse-min.js

Recommend this page to a friend!
  Classes of Dom Hastings   terminal-preview   dist/parse-min.js   Download  
File: dist/parse-min.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: terminal-preview
Render ANSI formatting characters as HTML
Author: By
Last change: Update of dist/parse-min.js
Date: 2 years ago
Size: 6,798 bytes
 

Contents

Class file image Download
var parse=function(t){var e={bg:"49",fg:"39",bold:!1,dim:!1,italic:!1,underline:!1,blink:!1,overline:!1,invert:!1,hidden:!1,strikethrough:!1},n=t.extend({},e),a=function(a){var r=t.extend({},e);return a=a.replace(/\b48;5;(\d+)\b/,"48_5_$1").replace(/\b38;5;(\d+)\b/,"38_5_$1").replace(/\b48;2;(\d+);(\d+);(\d+)\b/,"48_2_$1_$2_$3").replace(/\b38;2;(\d+);(\d+);(\d+)\b/,"38_2_$1_$2_$3"),a.split(/;/).forEach(function(e){0==e&&(r=t.extend({},n)),1==e&&(r.bold=!0),2==e&&(r.dim=!0),3==e&&(r.italic=!0),4==e&&(r.underline=!0),5==e&&(r.blink=!0),6==e&&(r.overline=!0),7==e&&(r.invert=!0),8==e&&(r.hidden=!0),9==e&&(r.strikethrough=!0),21==e&&(r.bold=!1),22==e&&(r.dim=!1),23==e&&(r.italic=!1),24==e&&(r.underline=!1),25==e&&(r.blink=!1),26==e&&(r.overline=!1),27==e&&(r.invert=!1),28==e&&(r.hidden=!1),29==e&&(r.strikethrough=!1),(match=e.match(/^38_2_(\d+)_(\d+)_(\d+)$/))?r.fg=c("true-"+match.slice(1).join("-")):(match=e.match(/^38_5_(\d+)$/))?r.fg=c("256-"+match[1]):(match=e.match(/^(3|9)[0-79]$/))&&(r.fg=e),(match=e.match(/^^48_2_(\d+)_(\d+)_(\d+)$/))?r.bg=c("true-"+match.slice(1).join("-"),!0):(match=e.match(/^48_5_(\d+)$/))?r.bg=c("256-"+match[1],!0):(match=e.match(/^(4|10)[0-79]$/))&&(r.bg=e)}),r},c=function(t,e){var n,a=e?"4":"3",c=e?"10":"9";return(n=t.match(/^true-(\d+)-(\d+)-(\d+)/))&&(n=h(n.slice(1),!0))&&(t="256-"+n),(n=t.match(/^256-(\d+)/))&&(n[1]<16?t=n[1]<8?a+n[1]:c+n[1]%8:16==n[1]?t=a+"0":231==n[1]&&(t=c+"7")),t},r=function(t){var e="\\e[";return[{key:"bg",n:48},{key:"fg",n:38}].forEach(function(n){var a;(a=t[n.key].match(/true-(\d+)-(\d+)-(\d+)/))?e+=n.n+";2;"+a.slice(1).join(";")+";":(a=t[n.key].match(/256-(\d+)/))?e+=n.n+";5;"+a[1]+";":t[n.key]&&(e+=t[n.key]+";")}),["bold","dim","italic","underline","blink","overline","invert","hidden","strikethrough"].forEach(function(n,a){t[n]&&(e+=++a+";")}),e=e.replace(/;$/,""),e+"m"},s=function(t){for(var e,n,a,c={stylingReset:/^(?:\\\[)?(?:\\033|\\e|\\x1[bB]|\x1b)\[(0?)m(?:\\\])?/,styling:/^(?:\\\[)?(?:\\033|\\e|\\x1[bB]|\x1b)\[([^m]*)m(?:\\\])?/,command:/^(?:\\\[)?(?:\\?`([^`]+)\\?`|\\?\$\(([^)]+)\))(?:\\\])?/,octal:/^\\(\d{3})/,hex:/^\\x([0-9a-fA-F]{2})/,token:/^( |\\[!#$@\\0aAdehHjlnsTtuvVWw])/,variable:/^\$\{?(\w+|\?)\}?/,text:/^([\S\s])/},r=[];t;)a=!1,Object.keys(c).forEach(function(s){!a&&(e=t.match(c[s]))&&(a=!0,"text"===s&&r.length&&"text"===(n=r[r.length-1]).type?(n.content+=e[0],n.value+=e[0]):"command"==s?r.push({type:s,content:e[0],value:e[1]||e[2]||"",wrap:e[0].match(/^\\\[/)&&e[0].match(/\\\]$/)}):r.push({type:s,content:e[0],value:e[1],wrap:e[0].match(/^\\\[/)&&e[0].match(/\\\]$/)}),t=t.replace(c[s],""))});return r},o=function(c){var r="",s=function(){var t=[];return e.invert?(t.push("bg-"+e.fg),t.push("fg-"+e.bg)):(t.push("bg-"+e.bg),t.push("fg-"+e.fg)),["bold","dim","italic","underline","overline","strikethrough","blink","hidden"].forEach(function(n){e[n]&&t.push(n)})," "+t.join(" ")};return e=t.extend({},n),c.forEach(function(n){var c=function(t){return t=t||"",t.replace(/"/g,"&quot;")};n.type.match(/styling/)?(r.match(/<span class="block styling/)&&(r+="</span>"),t.extend(e,n.style=a(n.value)),r+='<span class="block styling'+s()+'" data-content="'+c(n.content)+'" data-value="'+c(n.value)+'" data-type="'+n.type+'" style="'+((n.style.fg+"").match(/true-/)?(n.style.fg+"").replace(/true-/,"color: rgb(").replace(/$/,");").replace(/\-/g,","):"")+((n.style.bg+"").match(/true-/)?(n.style.bg+"").replace(/\-/g,",").replace(/true,/,"background-color: rgb(").replace(/$/,")"):"")+'"><span class="content">'+n.content+"</span>"):"command"===n.type?r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(n.value)+'"><span class="content">'+(n.wrap?'<span class="wrap">\\[</span>':"")+"$("+n.value+")"+(n.wrap?'<span class="wrap">\\]</span>':"")+"</span></span>":"token"===n.type?r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(n.value)+'" data-type="'+n.type+'"><span class="content">'+n.content+"</span></span>":"text"===n.type?r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(n.value)+'" data-type="'+n.type+'">'+n.content+"</span>":"variable"===n.type?r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(n.value)+'" data-type="'+n.type+'"><span class="content">'+n.content+"</span></span>":"hex"===n.type?r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(String.fromCharCode(parseInt(n.value,16)))+'" data-type="'+n.type+'"><span class="content">'+n.content+"</span></span>":"octal"===n.type&&(r+='<span class="block '+n.type+'" data-content="'+c(n.content)+'" data-value="'+c(String.fromCharCode(parseInt(n.value,8)))+'" data-type="'+n.type+'"><span class="content">'+n.content+"</span></span>")}),r+='<span class="cursor"></span>',r.match(/<span class="block styling/)&&(r+="</span>"),r},l=function(t){return o(s(t))},p=function(){var t=[];return t.push([0,0,0,0]),t.push([128,0,0,1]),t.push([0,128,0,2]),t.push([128,128,0,3]),t.push([0,0,128,4]),t.push([128,0,128,5]),t.push([0,128,128,6]),t.push([192,192,192,7]),t.push([128,128,128,8]),t.push([255,0,0,9]),t.push([0,255,0,10]),t.push([255,255,0,11]),t.push([0,0,255,12]),t.push([255,0,255,13]),t.push([0,255,255,14]),t.push([255,255,255,15]),[0,95,135,175,215,255].forEach(function(e){[0,95,135,175,215,255].forEach(function(n){[0,95,135,175,215,255].forEach(function(a){t.push([e,n,a,16+parseInt(""+Math.floor(e/255*5)+Math.floor(n/255*5)+Math.floor(a/255*5),6)])})})}),[8,18,28,38,48,58,68,78,88,98,108,118,128,138,148,158,168,178,188,198,208,218,228,238].forEach(function(e){t.push([e,e,e,232+Math.floor(e/10)])}),t}(),u=function(t,e){return t.slice(0).sort(function(t,n){return Math.abs(t[0]-e[0])+Math.abs(t[1]-e[1])+Math.abs(t[2]-e[2])-(Math.abs(n[0]-e[0])+Math.abs(n[1]-e[1])+Math.abs(n[2]-e[2]))||t[3]-n[3]})[0]},i=function(t,e){return c("256-"+(u(p.slice(0,16),t)||[])[3],e)},h=function(t,e){return e?(e=p.filter(function(e){return e[0]==t[0]&&e[1]==t[1]&&e[2]===t[2]})[0]||[],e[3]):(u(p,t)||[])[3]},d=function(t){return 49==t?p[0].slice(0,3):39==t?p[7].slice(0,3):50>t?p[t%10].slice(0,3):p[t%10+8].slice(0,3)},f=function(e){var n=[];return p[e][3]==e?p[e].slice(0,3):(t.each(p,function(t){t[3]==e&&(n=t.slice(0,3))}),n)},b=function(t){var e=0,n=0,a=0;return t=t.replace(/^#/,""),3===t.length?(e=parseInt(t[0]+t[0],16),n=parseInt(t[1]+t[1],16),a=parseInt(t[2]+t[2],16)):6===t.length&&(e=parseInt(t[0]+t[1],16),n=parseInt(t[2]+t[3],16),a=parseInt(t[4]+t[5],16)),[e,n,a]},m=function(t){return"#"+t.map(function(t){return(0+t.toString(16)).substr(-2)}).join("")};return l.extract=s,l.process=o,l.rgbToTerm256=h,l.rgbToTerm16=i,l.term256ToRgb=f,l.term16ToRgb=d,l.simplifyColour=c,l.hexToRgb=b,l.rgbToHex=m,l.parseStyles=a,l.buildStyles=r,l.defaultStyle=n,l}(jQuery);