-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
xml_formatting.js
145 lines (143 loc) · 4.92 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
/**@license
* __ _____ ________ __
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ /
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
* \/ /____/
* http://terminal.jcubic.pl
*
* This is example of custom formatter for jQuery Terminal
*
* Copyright (c) 2014-2021 Jakub Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global define */
(function(factory) {
var root;
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');
}
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($) {
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 style = [];
if ('size' in attrs) {
style.push('--size:' + attrs.size);
}
if ('spacing' in attrs) {
style.push('letter-spacing: ' + attrs.spacing);
}
return '[[;;;;;{"style": "' + style.join(';') + '"}]';
},
img: function(attrs) {
return '[[@;;;;' + attrs.src + ']' + (attrs.alt || '') + ']';
},
bold: function() {
return '[[b;rgba(255,255,255,0.9);]';
},
overline: function() {
return '[[o;;]';
},
strike: function() {
return '[[s;;]';
},
underline: function() {
return '[[u;;]';
},
glow: function() {
return '[[g;;]';
},
italic: function() {
return '[[i;;]';
},
link: function(attrs) {
return '[[!;;;;' + attrs.href + ']';
}
};
// short aliases
tags.b = tags.bold;
tags.a = tags.link;
tags.i = tags.italic;
var tag_re = /(<\/?\s*[a-zA-Z]+(?: [^>]+)?>)/;
function xml_formatter(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*(.+))?/);
var name = m[1].toLowerCase();
var attrs = {};
if (m[2]) {
var string_attrs = m[2];
var re = /([a-zA-Z]+)\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 {
return '[[;' + name + ';]';
}
}
return string.replace(/</g, '<').replace(/>/g, '>');
}).join('');
}
xml_formatter.__no_warn__ = true;
xml_formatter.tags = tags;
$.terminal.defaults.allowedAttributes.push('style');
$.terminal.xml_formatter = xml_formatter;
$.terminal.new_formatter(xml_formatter);
});