neutralts/
constants.rs

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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use std::collections::HashMap;
use lazy_static::lazy_static;

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_DELIM: &str = ":";

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_OPEN: &str = "{:";

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_CLOSE: &str = ":}";

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_NAME: &str = ";";

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_CODE: &str = ">>";

///  Bif delimiters
///
/// ```text
///   .------------------------------> BIF_OPEN = { + BIF_DELIM
///   |.-----------------------------> BIF_DELIM
///   ||.----------------------------> BIF_MOD_... (Modifiers)
///   |||       .--------------------> BIF_NAME
///   |||       |    .---------------> BIF_ARRAY
///   |||       |    ||    .---------> BIF_CODE
///   |||       |    ||    ||      .-> BIF_CLOSE = BIF_DELIM + }
///   |||       |    ||    ||      |
///   vvv       v    vv    vv      v
///   {:!snippet; var->key >> ... :}
/// ```
pub const BIF_ARRAY: &str = "->";

///  Bif comment
///
/// ```text
///      .---> BIF_COMMENT
///     |
///     v
///   {:* commnet *:}
/// ```
pub const BIF_COMMENT: &str = "*";

///  Bif comment open
///
/// ```text
///   .---> BIF_COMMENT_OPEN
///   |||
///   vvv
///   {:* commnet *:}
/// ```
pub const BIF_COMMENT_OPEN: &str = "{:*";

///  Bif comment close
///
/// ```text
///               .---> BIF_COMMENT_CLOSE
///               |||
///               vvv
///   {:* commnet *:}
/// ```
pub const BIF_COMMENT_CLOSE: &str = "*:}";

/// modifier filter
pub const BIF_MOD_FILTER: &str = "&";

/// modifier not
pub const BIF_MOD_NEGATE: &str = "!";

/// modifier upline
pub const BIF_MOD_UPLINE: &str = "^";

/// modifier scope
pub const BIF_MOD_SCOPE: &str = "+";

/// Identify bif var as it has no name
pub const BIF_VAR: &str = "{:;";

/// delim as bytes
pub const BIF_DELIM_B: &[u8] = BIF_DELIM.as_bytes();

/// BIF_OPEN as bytes
pub const BIF_OPEN_B: &[u8] = BIF_OPEN.as_bytes();

/// BIF_OPEN 0 as bytes
pub const BIF_OPEN0: u8 = BIF_OPEN.as_bytes()[0];

/// BIF_OPEN 1 as bytes
pub const BIF_OPEN1: u8 = BIF_OPEN.as_bytes()[1];

/// BIF_CLOSE as bytes
pub const BIF_CLOSE_B: &[u8] = BIF_CLOSE.as_bytes();

/// BIF_CLOSE 0 as bytes
pub const BIF_CLOSE0: u8 = BIF_CLOSE.as_bytes()[0];

/// BIF_CLOSE 1 as bytes
pub const BIF_CLOSE1: u8 = BIF_CLOSE.as_bytes()[1];

/// BIF_CODE as bytes
pub const BIF_CODE_B: &[u8] = BIF_CODE.as_bytes();

/// BIF_COMMENT as bytes
pub const BIF_COMMENT_B: u8 = BIF_COMMENT.as_bytes()[0];

/// BIF_COMMENT_OPEN as bytes
pub const BIF_COMMENT_OPEN_B: &[u8] = BIF_COMMENT_OPEN.as_bytes();

/// BIF_COMMENT_CLOSE as bytes
pub const BIF_COMMENT_CLOSE_B: &[u8] = BIF_COMMENT_CLOSE.as_bytes();

/// replacement for BIF_OPEN sanitation
pub const BIF_SANITIZE_OPEN: &str = "{:";

/// replacement for BIF_CLOSE sanitation
pub const BIF_SANITIZE_CLOSE: &str = ":}";

/// html entity for {:;:}
pub const UNPRINTABLE: &str = "�";

/// html entity for null
pub const NULL: &str = "�";

/// empty
pub const EMPTY: &str = "";

/// Empty string could be different in different environments and could be an
/// HTM entity. It is also used to make it clear that it is an empty string.
pub const EMPTY_STRING: String = String::new();

/// html entity for space
pub const SPACE: &str = " ";

/// html entity for crlf
pub const CRLF: &str = "
";

/// html entity for Backspace
pub const BACKSPACE: &str = "&#9224";

/// false
pub const FALSE: bool = false;

/// true
pub const TRUE: bool = true;

/// Identify bif allow
pub const BIF_ALLOWED: [&str; 2] = ["{:allow;", "{:!allow;"];

/// To detect the template files that may contain snippet
pub const SNIPPETS_FILES: &str = "snippet";

/// bif list
pub const BIF_LIST: [&str; 36] = [
    "",
    "allow",
    "array",
    "bool",
    "cache",
    "coalesce",
    "code",
    "contains",
    "count",
    "data",
    "date",
    "declare",
    "defined",
    "each",
    "else",
    "eval",
    "exit",
    "fetch",
    "filled",
    "flg",
    "for",
    "hash",
    "include",
    "join",
    "lang",
    "locale",
    "moveto",
    "neutral",
    "param",
    "rand",
    "redirect",
    "replace",
    "same",
    "snippet",
    "sum",
    "trans",
];

/// bif alias list because some bifs have no name
pub const BIF_ALIAS_LIST: [&str; 37] = [
    "allow",
    "array",
    "bool",
    "cache",
    "coalesce",
    "code",
    "contains",
    "count",
    "data",
    "date",
    "declare",
    "defined",
    "each",
    "else",
    "eval",
    "exit",
    "fetch",
    "filled",
    "flg",
    "for",
    "hash",
    "include",
    "join",
    "lang",
    "locale",
    "moveto",
    "neutral",
    "param",
    "rand",
    "redirect",
    "replace",
    "same",
    "snippet",
    "sum",
    "trans",
    "unprintable",
    "var",
];

lazy_static! {
    /// HTTP status codes
    pub static ref STATUS_CODES: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
        m.insert("100", "Continue");
        m.insert("101", "Switching Protocols");
        m.insert("103", "Early Hints");
        m.insert("200", "OK");
        m.insert("201", "Created");
        m.insert("202", "Accepted");
        m.insert("203", "Non-Authoritative Information");
        m.insert("204", "No Content");
        m.insert("205", "Reset Content");
        m.insert("206", "Partial Content");
        m.insert("208", "Already Reported");
        m.insert("226", "IM Used");
        m.insert("300", "Multiple Choices");
        m.insert("301", "Moved Permanently");
        m.insert("302", "Found");
        m.insert("303", "See Other");
        m.insert("304", "Not Modified");
        m.insert("305", "Use Proxy");
        m.insert("306", "Switch Proxy"); // old http version
        m.insert("307", "Temporary Redirect");
        m.insert("308", "Permanent Redirect");
        m.insert("400", "Bad Request");
        m.insert("401", "Unauthorized");
        m.insert("402", "Payment Required");
        m.insert("403", "Forbidden");
        m.insert("404", "Not Found");
        m.insert("405", "Method Not Allowed");
        m.insert("406", "Not Acceptable");
        m.insert("407", "Proxy Authentication Required");
        m.insert("408", "Request Time-out");
        m.insert("409", "Conflict");
        m.insert("410", "Gone");
        m.insert("411", "Length Required");
        m.insert("412", "Precondition Failed");
        m.insert("413", "Payload Too Large");
        m.insert("414", "URI Too Long");
        m.insert("415", "Unsupported Media Type");
        m.insert("416", "Range Not Satisfiable");
        m.insert("417", "Expectation Failed");
        m.insert("421", "Misdirected Request");
        m.insert("422", "Unprocessable Entity");
        m.insert("423", "Locked");
        m.insert("424", "Failed Dependency");
        m.insert("425", "Too Early");
        m.insert("426", "Upgrade Required");
        m.insert("428", "Precondition Required");
        m.insert("429", "Too Many Requests");
        m.insert("431", "Request Header Fields Too Large");
        m.insert("451", "Unavailable For Legal Reasons");
        m.insert("500", "Internal Server Error");
        m.insert("501", "Not Implemented");
        m.insert("502", "Bad Gateway");
        m.insert("503", "Service Unavailable");
        m.insert("504", "Gateway Time-out");
        m.insert("505", "HTTP Version Not Supported");
        m.insert("506", "Variant Also Negotiates (Experimental)");
        m.insert("510", "Not Extended");
        m.insert("511", "Network Authentication Required");

        m
    };
}

/// JavaScript script reloads the current top page, used in {:redirect;
pub const REDIR_JS_RELOAD_TOP: &str = "<!DOCTYPE html><script>top.location.href=self.location.href.split('#')[0];</script>";

/// JavaScript script reloads the current, used in {:redirect;
pub const REDIR_JS_RELOAD_SELF: &str = "<!DOCTYPE html><script>self.location.href=self.location.href.split('#')[0]</script>";

/// JavaScript script that redirects to a new URL in the top page, used in {:redirect;
/// The placeholder '{}' should be replaced with the destination url.
pub const REDIR_JS_REDIRECT_TOP: &str = "<!DOCTYPE html><script>top.location.href='{}';</script>";

/// JavaScript script that redirects to a new URL in the self page, used in {:redirect;
/// The placeholder '{}' should be replaced with the destination url.
pub const REDIR_JS_REDIRECT_SELF: &str = "<!DOCTYPE html><script>self.location.href='{}';</script>";

lazy_static! {
    /// JavaScript for {:fetch; ... :}
    pub static ref NEUTRAL_JS: String = r#"<script>{
        const d=document;
        let sl='...';
        let sd=250;
        let st=30000;
        let se=' Form ERROR! ';
        let sed=3500;
        "undefined"!=typeof neutral_submit_loading&&(sl=neutral_submit_loading);
        "undefined"!=typeof neutral_submit_delay&&(sd=neutral_submit_delay);
        "undefined"!=typeof neutral_submit_timeout&&(st=neutral_submit_timeout);
        "undefined"!=typeof neutral_submit_error&&(se=neutral_submit_error);
        "undefined"!=typeof neutral_submit_error_delay&&(sed=neutral_submit_error_delay);

        const o=new IntersectionObserver((e)=>{
            e.forEach(e=>{
                if(e.isIntersecting){
                    o.unobserve(e.target);
                    neutral_fetch(e.target,e.target.dataset.url,e.target.dataset.wrap);
                }
            });
        });

        function neutral_obs(){
            d.querySelectorAll('.neutral-fetch-visible').forEach((e)=>{
                e.classList.remove('neutral-fetch-visible');
                o.observe(e);
            });
        }

        function neutral_fetch(e,u,w){
            let h=e;if(w){h=d.getElementById(w)}
            fetch(u,{headers:{'requested-with-ajax':'fetch'}})
            .then(response=>response.text())
            .then(html=>{
                h.innerHTML=html;
                h.outerHTML=html;
                window.dispatchEvent(new CustomEvent('neutralFetchCompleted',{detail:{element:h,url:u}}));
            })
            .catch(error=>{
                h.innerHTML=se;
                window.dispatchEvent(new CustomEvent('neutralFetchError',{detail:{element:h,url:u}}));
            });
        }

        function neutral_fetch_form(f,u,w){
            const subs = f.querySelectorAll('[type=submit]');
            subs.forEach((e)=>{
                e.disabled=true;
                e.dataset['restsubs']=e.innerHTML;
                e.innerHTML=e.innerHTML + ' ' + sl;
            });
            let h=f;if(w){h = d.getElementById(w)}
            let dt=new FormData(f);
            let has_file=false;
            for(let i=0;i<f.elements.length;i++){
                value=f.elements[i].value;
                if(f.elements[i].name===""){
                    continue;
                }
                if(f.elements[i].type=="file"){
                    has_file=true;
                    continue;
                }
                if(f.elements[i].name.indexOf('[]')>=0){
                    continue;
                }
                if(f.elements[i].type=="checkbox" || f.elements[i].type=="radio"){
                    if(!f.elements[i].checked){
                        continue;
                    }
                }
                dt.append(f.elements[i].name,value);
            }
            if(!has_file){dt=new URLSearchParams(new FormData(f))}
            setTimeout(()=>{
                fetch(u,{
                    signal:AbortSignal.timeout(st),
                    method:'POST',
                    headers:{'requested-with-ajax':'fetch'},
                    body:dt,
                })
                .then(response=>response.text())
                .then(html=>{
                    h.innerHTML=html;
                    h.outerHTML=html;
                    window.dispatchEvent(new CustomEvent('neutralFetchCompleted',{detail:{element:h,url:u}}));
                })
                .catch(error=>{
                    window.dispatchEvent(new CustomEvent('neutralFetchError',{detail:{element:h,url:u}}));
                    subs.forEach((e)=>{e.disabled=true;e.innerHTML=se;});
                    setTimeout(()=>{
                        subs.forEach((e)=>{e.disabled=false;e.innerHTML=e.dataset['restsubs']});
                    },sed);
                });
            },sd);
        }

        function neutral_fev(){
            d.querySelectorAll('.neutral-fetch-form').forEach((e)=>{
                e.classList.remove('neutral-fetch-form');
                function handleSubmit(ev){
                    ev.preventDefault();
                    neutral_fetch_form(e,e.getAttribute('action'),e.dataset.wrap);
                }
                e.addEventListener('submit',handleSubmit);
            });

            d.querySelectorAll('.neutral-fetch-auto').forEach((e)=>{
                e.classList.remove('neutral-fetch-auto');
                neutral_fetch(e,e.dataset.url,e.dataset.wrap);
            });

            d.querySelectorAll('.neutral-fetch-click').forEach((e)=>{
                e.classList.remove('neutral-fetch-click');
                function handleClick(){
                    neutral_fetch(e,e.dataset.url,e.dataset.wrap);
                }
                e.addEventListener('click',handleClick);
            });
            neutral_obs();
        }

        d.addEventListener('DOMContentLoaded',()=>{
            neutral_fev();
        });
        window.addEventListener('neutralFetchCompleted',()=>{
            neutral_fev();
        });
        window.addEventListener('neutralFetchError',()=>{
            neutral_fev();
        });
    }</script>"#.replace("\n", "").replace("  ", "");
}

/// HTML container form for {:fetch; ... :}
pub const DIV_FETCH_FORM: &str = r#"
<form id="{id}" name="{name}" class="neutral-fetch-form {class}" method="POST" action="{endpoint}" data-wrap="{wrap}">
    {body}
</form>
"#;

/// HTML container none for {:fetch; ... :}
pub const DIV_FETCH_NONE: &str = r#"
<div id="{id}" class="neutral-fetch-none {class}" data-url="{endpoint}" data-wrap="{wrap}">
    {body}
</div>
"#;

/// HTML container auto for {:fetch; ... :}
pub const DIV_FETCH_AUTO: &str = r#"
<div id="{id}" class="neutral-fetch-auto {class}" data-url="{endpoint}" data-wrap="{wrap}">
    {body}
</div>
"#;

/// HTML container visible for {:fetch; ... :}
pub const DIV_FETCH_VISIBLE: &str = r#"
<div id="{id}" class="neutral-fetch-visible {class}" data-url="{endpoint}" data-wrap="{wrap}">
    {body}
</div>
"#;

/// HTML container click for {:fetch; ... :}
pub const DIV_FETCH_CLICK: &str = r#"
<div id="{id}" class="neutral-fetch-click {class}" data-url="{endpoint}" data-wrap="{wrap}">
    {body}
</div>
"#;

pub const DEFAULT_OBJ_ENGINE: &str = "python";
pub const DEFAULT_OBJ_CALLBACK: &str = "main";