1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/renderkid/lib/AnsiPainter.js

161 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-01-16 21:26:16 +08:00
"use strict";
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
// Generated by CoffeeScript 2.5.1
var AnsiPainter,
styles,
tags,
tools,
hasProp = {}.hasOwnProperty;
tools = require('./tools');
tags = require('./ansiPainter/tags');
styles = require('./ansiPainter/styles');
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
module.exports = AnsiPainter = function () {
2023-12-18 13:12:25 +08:00
var self;
2024-01-16 21:26:16 +08:00
var AnsiPainter = /*#__PURE__*/function () {
function AnsiPainter() {
_classCallCheck(this, AnsiPainter);
}
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
_createClass(AnsiPainter, [{
key: "paint",
value: function paint(s) {
return this._replaceSpecialStrings(this._renderDom(this._parse(s)));
}
}, {
key: "_replaceSpecialStrings",
value: function _replaceSpecialStrings(str) {
return str.replace(/&sp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
}
}, {
key: "_parse",
value: function _parse(string) {
var injectFakeRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
if (injectFakeRoot) {
string = '<none>' + string + '</none>';
}
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
return tools.toDom(string);
}
}, {
key: "_renderDom",
value: function _renderDom(dom) {
var parentStyles;
parentStyles = {
bg: 'none',
color: 'none'
};
return this._renderChildren(dom, parentStyles);
}
}, {
key: "_renderChildren",
value: function _renderChildren(children, parentStyles) {
var child, n, ret;
ret = '';
for (n in children) {
if (!hasProp.call(children, n)) continue;
child = children[n];
ret += this._renderNode(child, parentStyles);
}
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
return ret;
}
}, {
key: "_renderNode",
value: function _renderNode(node, parentStyles) {
if (node.type === 'text') {
return this._renderTextNode(node, parentStyles);
} else {
return this._renderTag(node, parentStyles);
2023-12-18 13:12:25 +08:00
}
}
2024-01-16 21:26:16 +08:00
}, {
key: "_renderTextNode",
value: function _renderTextNode(node, parentStyles) {
return this._wrapInStyle(node.data, parentStyles);
}
}, {
key: "_wrapInStyle",
value: function _wrapInStyle(str, style) {
return styles.color(style.color) + styles.bg(style.bg) + str + styles.none();
}
}, {
key: "_renderTag",
value: function _renderTag(node, parentStyles) {
var currentStyles, tagStyles;
tagStyles = this._getStylesForTagName(node.name);
currentStyles = this._mixStyles(parentStyles, tagStyles);
return this._renderChildren(node.children, currentStyles);
}
}, {
key: "_mixStyles",
value: function _mixStyles() {
var final, i, key, len, style, val;
final = {};
for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) {
styles[_key] = arguments[_key];
}
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
for (i = 0, len = styles.length; i < len; i++) {
style = styles[i];
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
for (key in style) {
if (!hasProp.call(style, key)) continue;
val = style[key];
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
if (final[key] == null || val !== 'inherit') {
final[key] = val;
}
}
}
return final;
}
}, {
key: "_getStylesForTagName",
value: function _getStylesForTagName(name) {
if (tags[name] == null) {
throw Error("Unknown tag name `".concat(name, "`"));
}
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
return tags[name];
}
}], [{
key: "getInstance",
value: function getInstance() {
if (self._instance == null) {
self._instance = new self();
}
return self._instance;
}
}, {
key: "paint",
value: function paint(str) {
return self.getInstance().paint(str);
}
}, {
key: "strip",
value: function strip(s) {
return s.replace(/\x1b\[[0-9]+m/g, '');
}
}]);
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
return AnsiPainter;
}();
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
;
AnsiPainter.tags = tags;
self = AnsiPainter;
2023-12-18 13:12:25 +08:00
return AnsiPainter;
2024-01-16 21:26:16 +08:00
}.call(void 0);