-
-
Notifications
You must be signed in to change notification settings - Fork 572
/
xml_formatting.js
198 lines (196 loc) · 7 KB
/
xml_formatting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**@license
* __ _____ ________ __
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ /
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
* \/ /____/
* http://terminal.jcubic.pl
*
* This is example of custom formatter for jQuery Terminal
*
* Copyright (c) 2014-2024 Jakub Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global define */
(function(factory) {
var root;
/* istanbul ignore next */
if (typeof window !== 'undefined') {
root = window;
} else if (typeof self !== 'undefined') {
root = self;
} else if (typeof global !== 'undefined') {
root = global;
} else {
throw new Error('Unknow context');
}
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
// istanbul ignore next
define(['jquery', 'jquery.terminal'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function(root, jQuery) {
if (jQuery === undefined) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if (typeof window !== 'undefined') {
jQuery = require('jquery');
} else {
jQuery = require('jquery')(root);
}
}
if (!jQuery.fn.terminal) {
if (typeof window !== 'undefined') {
require('jquery.terminal');
} else {
require('jquery.terminal')(jQuery);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser
// istanbul ignore next
factory(root.jQuery);
}
})(function($) {
/* istanbul ignore next */
if (!$.terminal) {
throw new Error('$.terminal is not defined');
}
// this formatter allow to echo xml where tags are colors like:
// <red>hello <navy>blue</navy> world</red>
// it allso support special tags e.g. link, img or bold
var tags = {
font: function(attrs) {
var styles = [];
if ('size' in attrs) {
styles.push('--size:' + attrs.size);
}
if ('spacing' in attrs) {
styles.push('letter-spacing: ' + attrs.spacing);
}
var background = attrs.background || '';
var color = attrs.color || '';
var style = styles.length ? '{"style": "' + styles.join(';') + '"}' : '';
return '[[;' + color + ';' + background + ';;;' + style + ']';
},
img: function(attrs) {
var cls = attrs.class || '';
var alt = attrs.alt || '';
var src = attrs.src || '';
delete attrs.alt;
delete attrs.class;
delete attrs.src;
var formatting = ['@', '', '', cls, src, JSON.stringify(attrs)];
return '[[' + formatting.join(';') + ']' + alt + ']';
},
bold: style('b'),
overline: style('o'),
strike: style('s'),
underline: style('u'),
glow: style('g'),
italic: style('i'),
reverse: style('r'),
span: function(attrs) {
var cls = attrs.class || '';
delete attrs.class;
var formatting = ['', '', '', cls, '', JSON.stringify(attrs)];
return '[[' + formatting.join(';') + ']';
},
link: function(attrs) {
var cls = attrs.class || '';
var href = attrs.href || '';
delete attrs.class;
delete attrs.href;
var formatting = ['!', '', '', cls, href, JSON.stringify(attrs)];
return '[[' + formatting.join(';') + ']';
}
};
function style(value) {
return function(attrs) {
var cls = attrs.class || '';
delete attrs.class;
var formatting = [value, '', '', cls, '', JSON.stringify(attrs)];
return '[[' + formatting.join(';') + ']';
};
}
// short aliases
tags.b = tags.bold;
tags.a = tags.link;
tags.i = tags.italic;
tags.r = tags.reverse;
var tag_re = /(<\/?\s*[a-zA-Z]+(?:\s?[^>]+)?>)/;
function no_target(options) {
if (!options) {
return true;
}
return Object.keys(options).length === 1 && 'position' in options;
}
function should_render(options) {
if (no_target(options)) {
return true;
}
for (var i = targets.length; i--;) {
var prop = targets[i];
if (options[prop] === true && $.terminal.xml_formatter[prop] === true) {
return true;
}
}
return false;
}
function xml_formatter(string, options) {
if (!should_render(options)) {
return string;
}
return string.split(tag_re).map(function(string) {
if (string.match(tag_re)) {
if (string[1] === '/') {
return ']';
}
string = string.replace(/^<|>$/g, '');
var m = string.match(/^([a-zA-Z]+)(?:\s*(.+))?/);
if (!m) {
// invalid XML
return string;
}
var name = m[1].toLowerCase();
var attrs = {};
if (m[2]) {
var string_attrs = m[2];
var re = /([\w-]+)\s*=\s*"([^"]+)"/g;
var match;
while (match = re.exec(string_attrs)) {
var attr_name = match[1];
var value = match[2];
attrs[attr_name] = value;
}
}
if (tags[name]) {
return tags[name](attrs);
} else {
var cls = attrs.class || '';
var formatting = ['', name, '', cls, '', JSON.stringify(attrs)];
return '[[' + formatting.join(';') + ']';
}
}
return string.replace(/</g, '<').replace(/>/g, '>');
}).join('');
}
xml_formatter.__no_warn__ = true;
xml_formatter.tags = tags;
var targets = ['echo', 'prompt', 'animation', 'command'];
targets.forEach(function(target) {
xml_formatter[target] = true;
});
$.terminal.defaults.allowedAttributes.push('style');
$.terminal.xml_formatter = xml_formatter;
$.terminal.new_formatter(xml_formatter);
});