diff --git a/src/gha_logs.rs b/src/gha_logs.rs index cb1dfd5f..47e59363 100644 --- a/src/gha_logs.rs +++ b/src/gha_logs.rs @@ -8,7 +8,7 @@ use std::str::FromStr; use std::sync::Arc; use uuid::Uuid; -pub const ANSI_UP_URL: &str = "/gha_logs/ansi_up@6.0.6.min.js"; +pub const ANSI_UP_URL: &str = "/gha_logs/ansi_up@0.0.1-custom.js"; pub const SUCCESS_URL: &str = "/gha_logs/success@1.svg"; pub const FAILURE_URL: &str = "/gha_logs/failure@1.svg"; @@ -243,36 +243,58 @@ async fn process_logs( {job_name} - {owner}/{repo}@{short_sha} {icon_status} - + "###, ); @@ -358,7 +379,7 @@ async fn process_logs( } pub fn ansi_up_min_js() -> anyhow::Result, hyper::Error> { - const ANSI_UP_MIN_JS: &str = include_str!("gha_logs/ansi_up@6.0.6.min.js"); + const ANSI_UP_MIN_JS: &str = include_str!("gha_logs/ansi_up@0.0.1-custom.js"); Ok(Response::builder() .status(StatusCode::OK) diff --git a/src/gha_logs/ansi_up@0.0.1-custom.js b/src/gha_logs/ansi_up@0.0.1-custom.js new file mode 100644 index 00000000..1b20b2eb --- /dev/null +++ b/src/gha_logs/ansi_up@0.0.1-custom.js @@ -0,0 +1,467 @@ +// SPDX-License-Identifier: MIT +/** + * https://github.com/drudru/ansi_up + * + * (The MIT License) + * + * Copyright (c) 2011 github.com/drudru + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * 'Software'), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +"use strict"; +var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { + if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } + return cooked; +}; +var PacketKind; +(function (PacketKind) { + PacketKind[PacketKind["EOS"] = 0] = "EOS"; + PacketKind[PacketKind["Text"] = 1] = "Text"; + PacketKind[PacketKind["Incomplete"] = 2] = "Incomplete"; + PacketKind[PacketKind["ESC"] = 3] = "ESC"; + PacketKind[PacketKind["Unknown"] = 4] = "Unknown"; + PacketKind[PacketKind["SGR"] = 5] = "SGR"; + PacketKind[PacketKind["OSCURL"] = 6] = "OSCURL"; +})(PacketKind || (PacketKind = {})); +export class AnsiUp { + constructor() { + this.VERSION = "6.0.6"; + this.setup_palettes(); + this._use_classes = false; + this.bold = false; + this.faint = false; + this.italic = false; + this.underline = false; + this.fg = this.bg = null; + this._buffer = ''; + this._url_allowlist = { 'http': 1, 'https': 1 }; + this._escape_html = true; + this.boldStyle = 'font-weight:bold'; + this.faintStyle = 'opacity:0.7'; + this.italicStyle = 'font-style:italic'; + this.underlineStyle = 'text-decoration:underline'; + } + set use_classes(arg) { + this._use_classes = arg; + } + get use_classes() { + return this._use_classes; + } + set url_allowlist(arg) { + this._url_allowlist = arg; + } + get url_allowlist() { + return this._url_allowlist; + } + set escape_html(arg) { + this._escape_html = arg; + } + get escape_html() { + return this._escape_html; + } + set boldStyle(arg) { this._boldStyle = arg; } + get boldStyle() { return this._boldStyle; } + set faintStyle(arg) { this._faintStyle = arg; } + get faintStyle() { return this._faintStyle; } + set italicStyle(arg) { this._italicStyle = arg; } + get italicStyle() { return this._italicStyle; } + set underlineStyle(arg) { this._underlineStyle = arg; } + get underlineStyle() { return this._underlineStyle; } + setup_palettes() { + this.ansi_colors = + [ + [ + { rgb: [0, 0, 0], class_name: "ansi-black" }, + { rgb: [187, 0, 0], class_name: "ansi-red" }, + { rgb: [0, 187, 0], class_name: "ansi-green" }, + { rgb: [187, 187, 0], class_name: "ansi-yellow" }, + { rgb: [0, 0, 187], class_name: "ansi-blue" }, + { rgb: [187, 0, 187], class_name: "ansi-magenta" }, + { rgb: [0, 187, 187], class_name: "ansi-cyan" }, + { rgb: [255, 255, 255], class_name: "ansi-white" } + ], + [ + { rgb: [85, 85, 85], class_name: "ansi-bright-black" }, + { rgb: [255, 85, 85], class_name: "ansi-bright-red" }, + { rgb: [0, 255, 0], class_name: "ansi-bright-green" }, + { rgb: [255, 255, 85], class_name: "ansi-bright-yellow" }, + { rgb: [85, 85, 255], class_name: "ansi-bright-blue" }, + { rgb: [255, 85, 255], class_name: "ansi-bright-magenta" }, + { rgb: [85, 255, 255], class_name: "ansi-bright-cyan" }, + { rgb: [255, 255, 255], class_name: "ansi-bright-white" } + ] + ]; + this.palette_256 = []; + this.ansi_colors.forEach(palette => { + palette.forEach(rec => { + this.palette_256.push(rec); + }); + }); + let levels = [0, 95, 135, 175, 215, 255]; + for (let r = 0; r < 6; ++r) { + for (let g = 0; g < 6; ++g) { + for (let b = 0; b < 6; ++b) { + let col = { rgb: [levels[r], levels[g], levels[b]], class_name: 'truecolor' }; + this.palette_256.push(col); + } + } + } + let grey_level = 8; + for (let i = 0; i < 24; ++i, grey_level += 10) { + let gry = { rgb: [grey_level, grey_level, grey_level], class_name: 'truecolor' }; + this.palette_256.push(gry); + } + } + escape_txt_for_html(txt) { + if (!this._escape_html) + return txt; + return txt.replace(/[&<>"']/gm, (str) => { + if (str === "&") + return "&"; + if (str === "<") + return "<"; + if (str === ">") + return ">"; + if (str === "\"") + return """; + if (str === "'") + return "'"; + }); + } + append_buffer(txt) { + var str = this._buffer + txt; + this._buffer = str; + } + get_next_packet() { + var pkt = { + kind: PacketKind.EOS, + text: '', + url: '' + }; + var len = this._buffer.length; + if (len == 0) + return pkt; + var pos = this._buffer.indexOf("\x1B"); + if (pos == -1) { + pkt.kind = PacketKind.Text; + pkt.text = this._buffer; + this._buffer = ''; + return pkt; + } + if (pos > 0) { + pkt.kind = PacketKind.Text; + pkt.text = this._buffer.slice(0, pos); + this._buffer = this._buffer.slice(pos); + return pkt; + } + if (pos == 0) { + if (len < 3) { + pkt.kind = PacketKind.Incomplete; + return pkt; + } + var next_char = this._buffer.charAt(1); + if ((next_char != '[') && (next_char != ']') && (next_char != '(')) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + if (next_char == '[') { + if (!this._csi_regex) { + this._csi_regex = rgx(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n \u001B[ # CSI\n ([<-?]?) # private-mode char\n ([d;]*) # any digits or semicolons\n ([ -/]? # an intermediate modifier\n [@-~]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n \u001B[ # CSI\n [ -~]* # anything legal\n ([\0-\u001F:]) # anything illegal\n )\n "], ["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n \\x1b\\[ # CSI\n ([\\x3c-\\x3f]?) # private-mode char\n ([\\d;]*) # any digits or semicolons\n ([\\x20-\\x2f]? # an intermediate modifier\n [\\x40-\\x7e]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n \\x1b\\[ # CSI\n [\\x20-\\x7e]* # anything legal\n ([\\x00-\\x1f:]) # anything illegal\n )\n "]))); + } + let match = this._buffer.match(this._csi_regex); + if (match === null) { + pkt.kind = PacketKind.Incomplete; + return pkt; + } + if (match[4]) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + if ((match[1] != '') || (match[3] != 'm')) + pkt.kind = PacketKind.Unknown; + else + pkt.kind = PacketKind.SGR; + pkt.text = match[2]; + var rpos = match[0].length; + this._buffer = this._buffer.slice(rpos); + return pkt; + } + else if (next_char == ']') { + if (len < 4) { + pkt.kind = PacketKind.Incomplete; + return pkt; + } + if ((this._buffer.charAt(2) != '8') + || (this._buffer.charAt(3) != ';')) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + if (!this._osc_st) { + this._osc_st = rgxG(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n (?: # legal sequence\n (\u001B\\) # ESC | # alternate\n (\u0007) # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\0-\u0006] # anything illegal\n | # alternate\n [\b-\u001A] # anything illegal\n | # alternate\n [\u001C-\u001F] # anything illegal\n )\n "], ["\n (?: # legal sequence\n (\\x1b\\\\) # ESC \\\n | # alternate\n (\\x07) # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\\x00-\\x06] # anything illegal\n | # alternate\n [\\x08-\\x1a] # anything illegal\n | # alternate\n [\\x1c-\\x1f] # anything illegal\n )\n "]))); + } + this._osc_st.lastIndex = 0; + { + let match = this._osc_st.exec(this._buffer); + if (match === null) { + pkt.kind = PacketKind.Incomplete; + return pkt; + } + if (match[3]) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + } + { + let match = this._osc_st.exec(this._buffer); + if (match === null) { + pkt.kind = PacketKind.Incomplete; + return pkt; + } + if (match[3]) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + } + if (!this._osc_regex) { + this._osc_regex = rgx(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n ^ # beginning of line\n #\n \u001B]8; # OSC Hyperlink\n [ -:<-~]* # params (excluding ;)\n ; # end of params\n ([!-~]{0,512}) # URL capture\n (?: # ST\n (?:\u001B\\) # ESC | # alternate\n (?:\u0007) # BEL (what xterm did)\n )\n ([ -~]+) # TEXT capture\n \u001B]8;; # OSC Hyperlink End\n (?: # ST\n (?:\u001B\\) # ESC | # alternate\n (?:\u0007) # BEL (what xterm did)\n )\n "], ["\n ^ # beginning of line\n #\n \\x1b\\]8; # OSC Hyperlink\n [\\x20-\\x3a\\x3c-\\x7e]* # params (excluding ;)\n ; # end of params\n ([\\x21-\\x7e]{0,512}) # URL capture\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n ([\\x20-\\x7e]+) # TEXT capture\n \\x1b\\]8;; # OSC Hyperlink End\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n "]))); + } + let match = this._buffer.match(this._osc_regex); + if (match === null) { + pkt.kind = PacketKind.ESC; + pkt.text = this._buffer.slice(0, 1); + this._buffer = this._buffer.slice(1); + return pkt; + } + pkt.kind = PacketKind.OSCURL; + pkt.url = match[1]; + pkt.text = match[2]; + var rpos = match[0].length; + this._buffer = this._buffer.slice(rpos); + return pkt; + } + else if (next_char == '(') { + pkt.kind = PacketKind.Unknown; + this._buffer = this._buffer.slice(3); + return pkt; + } + } + } + ansi_to_html(txt) { + this.append_buffer(txt); + var blocks = []; + while (true) { + var packet = this.get_next_packet(); + if ((packet.kind == PacketKind.EOS) + || (packet.kind == PacketKind.Incomplete)) + break; + if ((packet.kind == PacketKind.ESC) + || (packet.kind == PacketKind.Unknown)) + continue; + if (packet.kind == PacketKind.Text) + blocks.push(this.transform_to_html(this.with_state(packet))); + else if (packet.kind == PacketKind.SGR) + this.process_ansi(packet); + else if (packet.kind == PacketKind.OSCURL) + blocks.push(this.process_hyperlink(packet)); + } + return blocks.join(""); + } + with_state(pkt) { + return { bold: this.bold, faint: this.faint, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text }; + } + process_ansi(pkt) { + let sgr_cmds = pkt.text.split(';'); + while (sgr_cmds.length > 0) { + let sgr_cmd_str = sgr_cmds.shift(); + let num = parseInt(sgr_cmd_str, 10); + if (isNaN(num) || num === 0) { + this.fg = null; + this.bg = null; + this.bold = false; + this.faint = false; + this.italic = false; + this.underline = false; + } + else if (num === 1) { + this.bold = true; + } + else if (num === 2) { + this.faint = true; + } + else if (num === 3) { + this.italic = true; + } + else if (num === 4) { + this.underline = true; + } + else if (num === 21) { + this.bold = false; + } + else if (num === 22) { + this.faint = false; + this.bold = false; + } + else if (num === 23) { + this.italic = false; + } + else if (num === 24) { + this.underline = false; + } + else if (num === 39) { + this.fg = null; + } + else if (num === 49) { + this.bg = null; + } + else if ((num >= 30) && (num < 38)) { + this.fg = this.ansi_colors[0][(num - 30)]; + } + else if ((num >= 40) && (num < 48)) { + this.bg = this.ansi_colors[0][(num - 40)]; + } + else if ((num >= 90) && (num < 98)) { + this.fg = this.ansi_colors[1][(num - 90)]; + } + else if ((num >= 100) && (num < 108)) { + this.bg = this.ansi_colors[1][(num - 100)]; + } + else if (num === 38 || num === 48) { + if (sgr_cmds.length > 0) { + let is_foreground = (num === 38); + let mode_cmd = sgr_cmds.shift(); + if (mode_cmd === '5' && sgr_cmds.length > 0) { + let palette_index = parseInt(sgr_cmds.shift(), 10); + if (palette_index >= 0 && palette_index <= 255) { + if (is_foreground) + this.fg = this.palette_256[palette_index]; + else + this.bg = this.palette_256[palette_index]; + } + } + if (mode_cmd === '2' && sgr_cmds.length > 2) { + let r = parseInt(sgr_cmds.shift(), 10); + let g = parseInt(sgr_cmds.shift(), 10); + let b = parseInt(sgr_cmds.shift(), 10); + if ((r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)) { + let c = { rgb: [r, g, b], class_name: 'truecolor' }; + if (is_foreground) + this.fg = c; + else + this.bg = c; + } + } + } + } + } + } + transform_to_html(fragment) { + let txt = fragment.text; + if (txt.length === 0) + return txt; + txt = this.escape_txt_for_html(txt); + if (!fragment.bold && !fragment.italic && !fragment.faint && !fragment.underline && fragment.fg === null && fragment.bg === null) + return txt; + const styles = []; + const classes = []; + const fg = fragment.fg; + const bg = fragment.bg; + if (!this._use_classes) { + if (fragment.bold) + styles.push(this._boldStyle); + if (fragment.faint) + styles.push(this._faintStyle); + if (fragment.italic) + styles.push(this._italicStyle); + if (fragment.underline) + styles.push(this._underlineStyle); + if (fg) + styles.push(`color:rgb(${fg.rgb.join(',')})`); + if (bg) + styles.push(`background-color:rgb(${bg.rgb})`); + } + else { + if (fragment.bold) + classes.push("bold"); + if (fragment.faint) + classes.push("faint"); + if (fragment.italic) + classes.push("italic"); + if (fragment.underline) + classes.push("underline"); + if (fg) { + if (fg.class_name !== 'truecolor') { + classes.push(`${fg.class_name}-fg`); + } + else { + styles.push(`color:rgb(${fg.rgb.join(',')})`); + } + } + if (bg) { + if (bg.class_name !== 'truecolor') { + classes.push(`${bg.class_name}-bg`); + } + else { + styles.push(`background-color:rgb(${bg.rgb.join(',')})`); + } + } + } + let class_string = ''; + let style_string = ''; + if (classes.length) + class_string = ` class="${classes.join(' ')}"`; + if (styles.length) + style_string = ` style="${styles.join(';')}"`; + return `${txt}`; + } + ; + process_hyperlink(pkt) { + let parts = pkt.url.split(':'); + if (parts.length < 1) + return ''; + if (!this._url_allowlist[parts[0]]) + return ''; + let result = `${this.escape_txt_for_html(pkt.text)}`; + return result; + } +} +function rgx(tmplObj, ...subst) { + let regexText = tmplObj.raw[0]; + let wsrgx = /^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm; + let txt2 = regexText.replace(wsrgx, ''); + return new RegExp(txt2); +} +function rgxG(tmplObj, ...subst) { + let regexText = tmplObj.raw[0]; + let wsrgx = /^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm; + let txt2 = regexText.replace(wsrgx, ''); + return new RegExp(txt2, 'g'); +} +var templateObject_1, templateObject_2, templateObject_3; diff --git a/src/gha_logs/ansi_up@6.0.6.min.js b/src/gha_logs/ansi_up@6.0.6.min.js deleted file mode 100644 index eca73051..00000000 --- a/src/gha_logs/ansi_up@6.0.6.min.js +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-License-Identifier: MIT -/** - * https://github.com/drudru/ansi_up - * - * (The MIT License) - * - * Copyright (c) 2011 github.com/drudru - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * 'Software'), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -var e,t,n,i,s=function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e};!function(e){e[e.EOS=0]="EOS",e[e.Text=1]="Text",e[e.Incomplete=2]="Incomplete",e[e.ESC=3]="ESC",e[e.Unknown=4]="Unknown",e[e.SGR=5]="SGR",e[e.OSCURL=6]="OSCURL"}(e||(e={}));class l{constructor(){this.VERSION="6.0.6",this.setup_palettes(),this._use_classes=!1,this.bold=!1,this.faint=!1,this.italic=!1,this.underline=!1,this.fg=this.bg=null,this._buffer="",this._url_allowlist={http:1,https:1},this._escape_html=!0,this.boldStyle="font-weight:bold",this.faintStyle="opacity:0.7",this.italicStyle="font-style:italic",this.underlineStyle="text-decoration:underline"}set use_classes(e){this._use_classes=e}get use_classes(){return this._use_classes}set url_allowlist(e){this._url_allowlist=e}get url_allowlist(){return this._url_allowlist}set escape_html(e){this._escape_html=e}get escape_html(){return this._escape_html}set boldStyle(e){this._boldStyle=e}get boldStyle(){return this._boldStyle}set faintStyle(e){this._faintStyle=e}get faintStyle(){return this._faintStyle}set italicStyle(e){this._italicStyle=e}get italicStyle(){return this._italicStyle}set underlineStyle(e){this._underlineStyle=e}get underlineStyle(){return this._underlineStyle}setup_palettes(){this.ansi_colors=[[{rgb:[0,0,0],class_name:"ansi-black"},{rgb:[187,0,0],class_name:"ansi-red"},{rgb:[0,187,0],class_name:"ansi-green"},{rgb:[187,187,0],class_name:"ansi-yellow"},{rgb:[0,0,187],class_name:"ansi-blue"},{rgb:[187,0,187],class_name:"ansi-magenta"},{rgb:[0,187,187],class_name:"ansi-cyan"},{rgb:[255,255,255],class_name:"ansi-white"}],[{rgb:[85,85,85],class_name:"ansi-bright-black"},{rgb:[255,85,85],class_name:"ansi-bright-red"},{rgb:[0,255,0],class_name:"ansi-bright-green"},{rgb:[255,255,85],class_name:"ansi-bright-yellow"},{rgb:[85,85,255],class_name:"ansi-bright-blue"},{rgb:[255,85,255],class_name:"ansi-bright-magenta"},{rgb:[85,255,255],class_name:"ansi-bright-cyan"},{rgb:[255,255,255],class_name:"ansi-bright-white"}]],this.palette_256=[],this.ansi_colors.forEach((e=>{e.forEach((e=>{this.palette_256.push(e)}))}));let e=[0,95,135,175,215,255];for(let t=0;t<6;++t)for(let n=0;n<6;++n)for(let i=0;i<6;++i){let s={rgb:[e[t],e[n],e[i]],class_name:"truecolor"};this.palette_256.push(s)}let t=8;for(let e=0;e<24;++e,t+=10){let e={rgb:[t,t,t],class_name:"truecolor"};this.palette_256.push(e)}}escape_txt_for_html(e){return this._escape_html?e.replace(/[&<>"']/gm,(e=>"&"===e?"&":"<"===e?"<":">"===e?">":'"'===e?""":"'"===e?"'":void 0)):e}append_buffer(e){var t=this._buffer+e;this._buffer=t}get_next_packet(){var l={kind:e.EOS,text:"",url:""},a=this._buffer.length;if(0==a)return l;var h=this._buffer.indexOf("");if(-1==h)return l.kind=e.Text,l.text=this._buffer,this._buffer="",l;if(h>0)return l.kind=e.Text,l.text=this._buffer.slice(0,h),this._buffer=this._buffer.slice(h),l;if(0==h){if(a<3)return l.kind=e.Incomplete,l;var f=this._buffer.charAt(1);if("["!=f&&"]"!=f&&"("!=f)return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l;if("["==f){this._csi_regex||(this._csi_regex=r(t||(t=s(["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n [ # CSI\n ([<-?]?) # private-mode char\n ([d;]*) # any digits or semicolons\n ([ -/]? # an intermediate modifier\n [@-~]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n [ # CSI\n [ -~]* # anything legal\n ([\0-:]) # anything illegal\n )\n "],["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n \\x1b\\[ # CSI\n ([\\x3c-\\x3f]?) # private-mode char\n ([\\d;]*) # any digits or semicolons\n ([\\x20-\\x2f]? # an intermediate modifier\n [\\x40-\\x7e]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n \\x1b\\[ # CSI\n [\\x20-\\x7e]* # anything legal\n ([\\x00-\\x1f:]) # anything illegal\n )\n "]))));let n=this._buffer.match(this._csi_regex);if(null===n)return l.kind=e.Incomplete,l;if(n[4])return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l;""!=n[1]||"m"!=n[3]?l.kind=e.Unknown:l.kind=e.SGR,l.text=n[2];var _=n[0].length;return this._buffer=this._buffer.slice(_),l}if("]"==f){if(a<4)return l.kind=e.Incomplete,l;if("8"!=this._buffer.charAt(2)||";"!=this._buffer.charAt(3))return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l;this._osc_st||(this._osc_st=function(e){let t=e.raw[0],n=/^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm,i=t.replace(n,"");return new RegExp(i,"g")}(n||(n=s(["\n (?: # legal sequence\n (\\) # ESC | # alternate\n () # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\0-] # anything illegal\n | # alternate\n [\b-] # anything illegal\n | # alternate\n [-] # anything illegal\n )\n "],["\n (?: # legal sequence\n (\\x1b\\\\) # ESC \\\n | # alternate\n (\\x07) # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\\x00-\\x06] # anything illegal\n | # alternate\n [\\x08-\\x1a] # anything illegal\n | # alternate\n [\\x1c-\\x1f] # anything illegal\n )\n "])))),this._osc_st.lastIndex=0;{let t=this._osc_st.exec(this._buffer);if(null===t)return l.kind=e.Incomplete,l;if(t[3])return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l}{let t=this._osc_st.exec(this._buffer);if(null===t)return l.kind=e.Incomplete,l;if(t[3])return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l}this._osc_regex||(this._osc_regex=r(i||(i=s(["\n ^ # beginning of line\n #\n ]8; # OSC Hyperlink\n [ -:<-~]* # params (excluding ;)\n ; # end of params\n ([!-~]{0,512}) # URL capture\n (?: # ST\n (?:\\) # ESC | # alternate\n (?:) # BEL (what xterm did)\n )\n ([ -~]+) # TEXT capture\n ]8;; # OSC Hyperlink End\n (?: # ST\n (?:\\) # ESC | # alternate\n (?:) # BEL (what xterm did)\n )\n "],["\n ^ # beginning of line\n #\n \\x1b\\]8; # OSC Hyperlink\n [\\x20-\\x3a\\x3c-\\x7e]* # params (excluding ;)\n ; # end of params\n ([\\x21-\\x7e]{0,512}) # URL capture\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n ([\\x20-\\x7e]+) # TEXT capture\n \\x1b\\]8;; # OSC Hyperlink End\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n "]))));let t=this._buffer.match(this._osc_regex);if(null===t)return l.kind=e.ESC,l.text=this._buffer.slice(0,1),this._buffer=this._buffer.slice(1),l;l.kind=e.OSCURL,l.url=t[1],l.text=t[2];_=t[0].length;return this._buffer=this._buffer.slice(_),l}if("("==f)return l.kind=e.Unknown,this._buffer=this._buffer.slice(3),l}}ansi_to_html(t){this.append_buffer(t);for(var n=[];;){var i=this.get_next_packet();if(i.kind==e.EOS||i.kind==e.Incomplete)break;i.kind!=e.ESC&&i.kind!=e.Unknown&&(i.kind==e.Text?n.push(this.transform_to_html(this.with_state(i))):i.kind==e.SGR?this.process_ansi(i):i.kind==e.OSCURL&&n.push(this.process_hyperlink(i)))}return n.join("")}with_state(e){return{bold:this.bold,faint:this.faint,italic:this.italic,underline:this.underline,fg:this.fg,bg:this.bg,text:e.text}}process_ansi(e){let t=e.text.split(";");for(;t.length>0;){let e=t.shift(),n=parseInt(e,10);if(isNaN(n)||0===n)this.fg=null,this.bg=null,this.bold=!1,this.faint=!1,this.italic=!1,this.underline=!1;else if(1===n)this.bold=!0;else if(2===n)this.faint=!0;else if(3===n)this.italic=!0;else if(4===n)this.underline=!0;else if(21===n)this.bold=!1;else if(22===n)this.faint=!1,this.bold=!1;else if(23===n)this.italic=!1;else if(24===n)this.underline=!1;else if(39===n)this.fg=null;else if(49===n)this.bg=null;else if(n>=30&&n<38)this.fg=this.ansi_colors[0][n-30];else if(n>=40&&n<48)this.bg=this.ansi_colors[0][n-40];else if(n>=90&&n<98)this.fg=this.ansi_colors[1][n-90];else if(n>=100&&n<108)this.bg=this.ansi_colors[1][n-100];else if((38===n||48===n)&&t.length>0){let e=38===n,i=t.shift();if("5"===i&&t.length>0){let n=parseInt(t.shift(),10);n>=0&&n<=255&&(e?this.fg=this.palette_256[n]:this.bg=this.palette_256[n])}if("2"===i&&t.length>2){let n=parseInt(t.shift(),10),i=parseInt(t.shift(),10),s=parseInt(t.shift(),10);if(n>=0&&n<=255&&i>=0&&i<=255&&s>=0&&s<=255){let t={rgb:[n,i,s],class_name:"truecolor"};e?this.fg=t:this.bg=t}}}}}transform_to_html(e){let t=e.text;if(0===t.length)return t;if(t=this.escape_txt_for_html(t),!(e.bold||e.italic||e.faint||e.underline||null!==e.fg||null!==e.bg))return t;let n=[],i=[],s=e.fg,l=e.bg;e.bold&&n.push(this._boldStyle),e.faint&&n.push(this._faintStyle),e.italic&&n.push(this._italicStyle),e.underline&&n.push(this._underlineStyle),this._use_classes?(s&&("truecolor"!==s.class_name?i.push(`${s.class_name}-fg`):n.push(`color:rgb(${s.rgb.join(",")})`)),l&&("truecolor"!==l.class_name?i.push(`${l.class_name}-bg`):n.push(`background-color:rgb(${l.rgb.join(",")})`))):(s&&n.push(`color:rgb(${s.rgb.join(",")})`),l&&n.push(`background-color:rgb(${l.rgb})`));let r="",a="";return i.length&&(r=` class="${i.join(" ")}"`),n.length&&(a=` style="${n.join(";")}"`),`${t}`}process_hyperlink(e){let t=e.url.split(":");return t.length<1?"":this._url_allowlist[t[0]]?`${this.escape_txt_for_html(e.text)}`:""}}function r(e,...t){let n=e.raw[0].replace(/^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm,"");return new RegExp(n)}export{l as AnsiUp};export default null;