-
Notifications
You must be signed in to change notification settings - Fork 0
/
jun.js
248 lines (222 loc) · 7.55 KB
/
jun.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
====================================
Juniper config to set converter
https://github.com/Mr-Method/jun2set
====================================
date: 2023.08.13
====================================
*/
const btnConvert = document.querySelector('[data-convert]');
const btnClean = document.querySelector('[data-clear]');
const btnDemo = document.querySelector('[data-demo]');
const outputBlock = document.querySelector('.output');
const codeBlock = document.getElementById('textout');
let setInstance = "";
btnConvert.addEventListener('click', doConvert);
btnClean.addEventListener('click', destroyBoxes);
btnDemo.addEventListener('click', setDemo);
const dptree = {};
function setDPTree(commands, mode, leaf) {
let str = commands.join(' ');
if ( dptree[mode] === undefined ) dptree[mode] = {};
if ( dptree[mode][str] === undefined ) dptree[mode][str] = leaf;
}
function getDPTree(commands, mode) {
let str = commands.join(' ');
if ( dptree[mode] === undefined || dptree[mode][str] === undefined ) return;
setInstance += dptree[mode][str];
delete dptree[mode][str];
}
function setDemo (e) {
document.getElementById("textin").value =
"system {\n processes {\n general-authentication-service {\n traceoptions {\n" +
" file gas.log size 10k files 3;\n flag all;\n }\n }\n }\n}";
}
function FiltrInactive(leaf) {
let str = leaf.slice();
console.log(str);
str = str.replace(/\s(\d+)\s*$/, ``);
str = str.replace(/(\sdiscard.*)$/, ``);
str = str.replace(/(policer)\s.+$/, `$1`);
str = str.replace(/(\snext-hop\s.*)$/, ``);
str = str.replace(/(keepalives)\s.+$/, `$1`);
str = str.replace(/(\smembers\s\d+\:\d+)\s*$/, ``);
console.log(str);
return str;
}
function printSetCommand(commands, leaf) {
console.log(`${commands.join(' ')} ${leaf}`);
}
function retSetCommand(commands, leaf) {
let str = `${commands.join(' ')} ${leaf}`;
str.replace(/\s{2,}/g, `\ `);
return str + "\n";
}
function replaceCurly(s) {
return s.replace('{', '{\n').replace('}', '\n}');
}
function doConvert() {
setInstance = "";
let data = document.getElementById("textin").value;
let filein;
let ignoreAnnotations = 0;
if (!data.includes('"')) {
data = replaceCurly(data);
} else {
data = data
.split('"')
.map((item, i) => (i % 2 !== 0 ? replaceCurly(item) : item))
.join('"');
}
const lAnnotations = [];
let annotation = '';
const tree = ['set'];
const lines = data.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line === '' || line.startsWith('#')) {
continue;
}
if (line.startsWith('/*')) {
annotation = line.replace('/* ', '"').replace(' */', '"');
} else {
let cleanElem = line.trim().replace(/[\t\r]/g, '');
if (annotation) {
lAnnotations.push('top');
if (tree.length > 1) {
const level = [...tree];
level[0] = 'edit';
lAnnotations.push(level.join(' '));
}
const cleanElemOrig = cleanElem;
if (cleanElem.includes(';')) {
cleanElem = cleanElem.split(' ')[0];
}
lAnnotations.push(`annotate ${cleanElemOrig} ${annotation}`);
annotation = '';
}
if (cleanElem.includes(";")) {
if ( cleanElem.match(/\[(.*?)\];/g) ) {
var [a, b] = cleanElem.split(' [ ');
if (a.includes('inactive: ')) {
a = a.replace('inactive: ', '');
const linactive = [...tree];
linactive[0] = 'deactivate';
setDPTree(tree, 'inactive_array', retSetCommand(linactive, a));
} else if (a.includes('protect: ')) {
a = a.replace('protect: ', '');
const lprotect = [...tree];
lprotect[0] = 'protect';
setDPTree(tree, 'protect_line', retSetCommand(lprotect, a));
}
b.split(' ').forEach(function (item) {
if (!item.includes(']')) {
setInstance += retSetCommand(tree, a+' '+item);
} else {
getDPTree(tree, 'inactive_array');
getDPTree(tree, 'protect_array');
}
});
// alert(retSetCommand(tree, cleanElem.split(';')[0]));
} else {
if (cleanElem.includes('inactive: ')) {
cleanElem = cleanElem.replace('inactive: ', '');
const linactive = [...tree];
linactive[0] = 'deactivate';
setDPTree(tree, 'inactive_line', retSetCommand(linactive, FiltrInactive(cleanElem.replace(/\;\s*.*/, '')))); //.split(' ')[0]
} else if (cleanElem.includes('protect: ')) {
cleanElem = cleanElem.replace('protect: ', '');
const lprotect = [...tree];
lprotect[0] = 'protect';
setDPTree(tree, 'protect_line', retSetCommand(lprotect, cleanElem.replace(/\;\s*.*/, '')));
}
setInstance += retSetCommand(tree, cleanElem.replace(/\;\s*.*/, ''));
}
getDPTree(tree, 'inactive_line');
getDPTree(tree, 'protect_line');
} else if (cleanElem === '}') {
getDPTree(tree, 'inactive_block');
getDPTree(tree, 'protect_block');
tree.pop();
} else {
cleanElem = cleanElem.replace(' {', '');
if (cleanElem.includes('inactive: ')) {
cleanElem = cleanElem.replace('inactive: ', '');
const linactive = [...tree];
linactive[0] = 'deactivate';
tree.push(cleanElem);
setDPTree(tree, 'inactive_block', retSetCommand(linactive, cleanElem));
} else if (cleanElem.includes('protect: ')) {
cleanElem = cleanElem.replace('protect: ', '');
const lprotect = [...tree];
lprotect[0] = 'protect';
tree.push(cleanElem);
setDPTree(tree, 'protect_block', retSetCommand(lprotect, cleanElem));
} else {
tree.push(cleanElem);
}
}
}
}
if (!ignoreAnnotations) {
for (const a of lAnnotations) {
console.log(a);
}
}
showSet();
}
function destroyBoxes() {
setInstance = "";
codeBlock.innerText = "";
outputBlock.style.display = 'none';
}
function showSet() {
codeBlock.innerText = setInstance;
if ( setInstance ) {
outputBlock.style.display = 'block';
var b = document.createElement("button");
b.className = "at_copy";
b.type = "button";
b.ariaLabel = "Copy code to clipboard";
b.innerText = "Copy";
codeBlock.parentElement.append(b);
b.addEventListener("click", function() {
var e = codeBlock.innerText.trim();
copyTextToClipboard(e);
b.innerText = "Copied";
setTimeout(function() {
b.innerText = "Copy"
}, 1e3)
});
}
}
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}