forked from NebulaSS13/Nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.dm
More file actions
850 lines (750 loc) · 30.1 KB
/
text.dm
File metadata and controls
850 lines (750 loc) · 30.1 KB
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
* Holds procs designed to help with filtering text
* Contains groups:
* SQL sanitization
* Text sanitization
* Text searches
* Text modification
* Misc
*/
/*
* SQL sanitization
*/
// Run all strings to be used in an SQL query through this proc first to properly escape out injection attempts.
/proc/sanitize_sql(t)
var/sqltext = dbcon.Quote("[t]") // http://www.byond.com/forum/post/2218538
return copytext(sqltext, 2, -1)
/*
* Text sanitization
*/
/**
* Strip out the special beyond characters for \proper and \improper
* from text that will be sent to the browser.
*/
#define strip_improper(input_text) replacetext(replacetext(input_text, "\proper", ""), "\improper", "")
var/global/regex/starts_uppercase_regex = regex(@"^[A-Z]")
var/global/regex/starts_lowercase_regex = regex(@"^[a-z]")
#define is_proper(input_text) ((findtext(input_text, "\proper") == 1) || findtext(input_text, starts_uppercase_regex))
#define is_improper(input_text) ((findtext(input_text, "\improper") == 1 || findtext(input_text, starts_lowercase_regex)))
//Used for preprocessing entered text
//Added in an additional check to alert players if input is too long
/proc/sanitize(input, max_length = MAX_MESSAGE_LEN, encode = TRUE, trim = TRUE, extra = TRUE, ascii_only = FALSE)
if(!input)
return
if(max_length)
var/input_length = length_char(input)
if(input_length > max_length)
to_chat(usr, SPAN_WARNING("Your message is too long by [input_length - max_length] character\s."))
return
input = copytext_char(input, 1, max_length + 1)
input = strip_improper(input)
if(extra)
input = replace_characters(input, list("\n"=" ","\t"=" "))
if(ascii_only)
// Some procs work differently depending on unicode/ascii string
// You should always consider this with any text processing work
// More: http://www.byond.com/docs/ref/info.html#/{notes}/Unicode
// http://www.byond.com/forum/post/2520672
input = strip_non_ascii(input)
else
// Strip Unicode control/space-like chars here exept for line endings (\n,\r) and normal space (0x20)
// codes from https://www.compart.com/en/unicode/category/
// https://en.wikipedia.org/wiki/Whitespace_character#Unicode
var/static/regex/unicode_control_chars = regex(@"[\u0001-\u0009\u000B\u000C\u000E-\u001F\u007F\u0080-\u009F\u00A0\u1680\u180E\u2000-\u200D\u2028\u2029\u202F\u205F\u2060\u3000\uFEFF]", "g")
input = unicode_control_chars.Replace(input, "")
// Allows at most one Unicode combining diacritical mark in a row
// Codes acquired from https://en.wikipedia.org/wiki/Combining_Diacritical_Marks
// https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_Extended
// https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_Supplement
// https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols
// https://en.wikipedia.org/wiki/Combining_Half_Marks
var/static/regex/unicode_diacritical_marks = regex(@"([\u0300-\u036F\u1AB0-\u1ACE\u1DC0-\u1DFF\u20D0-\u20F0\uFE20-\uFE2F]){2,}", "g")
input = unicode_diacritical_marks.Replace(input, "$1")
if(encode)
// In addition to processing html, html_encode removes byond formatting codes like "\red", "\i" and other.
// It is important to avoid double-encode text, it can "break" quotes and some other characters.
// Also, keep in mind that escaped characters don't work in the interface (window titles, lower left corner of the main window, etc.)
input = html_encode(input)
else
// If not need encode text, simply remove < and >
// note: we can also remove here byond formatting codes: 0xFF + next byte
input = replace_characters(input, list("<"=" ", ">"=" "))
if(trim)
input = trim(input)
return input
//Run sanitize(), but remove <, >, " first to prevent displaying them as > < &34; in some places after html_encode().
//Best used for sanitize object names, window titles.
//If you have a problem with sanitize() in chat, when quotes and >, < are displayed as html entities -
//this is a problem of double-encode(when & becomes &), use sanitize() with encode=0, but not the sanitize_safe()!
/proc/sanitize_safe(input, max_length = MAX_MESSAGE_LEN, encode = TRUE, trim = TRUE, extra = TRUE, ascii_only = FALSE)
return sanitize(replace_characters(input, list(">"=" ","<"=" ", "\""="'")), max_length, encode, trim, extra, ascii_only)
//Filters out undesirable characters from names
/proc/sanitize_name(input, max_length = MAX_NAME_LEN, allow_numbers = 0, force_first_letter_uppercase = TRUE)
if(!input || length_char(input) > max_length)
return //Rejects the input if it is null or if it is longer then the max length allowed
var/number_of_alphanumeric = 0
var/last_char_group = 0
var/output = ""
var/char = ""
var/bytes_length = length(input)
var/ascii_char
for(var/i = 1, i <= bytes_length, i += length(char))
char = input[i]
ascii_char = text2ascii(char)
switch(ascii_char) //todo: unicode names?
// A .. Z
if(65 to 90) //Uppercase Letters
output += ascii2text(ascii_char)
number_of_alphanumeric++
last_char_group = 4
// a .. z
if(97 to 122) //Lowercase Letters
if(last_char_group<2 && force_first_letter_uppercase)
output += ascii2text(ascii_char-32) //Force uppercase first character
else
output += ascii2text(ascii_char)
number_of_alphanumeric++
last_char_group = 4
// 0 .. 9
if(48 to 57) //Numbers
if(!last_char_group) continue //suppress at start of string
if(!allow_numbers) continue
output += ascii2text(ascii_char)
number_of_alphanumeric++
last_char_group = 3
// ' - .
if(39,45,46) //Common name punctuation
if(!last_char_group) continue
output += ascii2text(ascii_char)
last_char_group = 2
// ~ | @ : # $ % & * +
if(126,124,64,58,35,36,37,38,42,43) //Other symbols that we'll allow (mainly for AI)
if(!last_char_group) continue //suppress at start of string
if(!allow_numbers) continue
output += ascii2text(ascii_char)
last_char_group = 2
//Space
if(32)
if(last_char_group <= 1) continue //suppress double-spaces and spaces at start of string
output += ascii2text(ascii_char)
last_char_group = 1
else
return
if(number_of_alphanumeric < 2) return //protects against tiny names like "A" and also names like "' ' ' ' ' ' ' '"
if(last_char_group == 1)
output = copytext(output, 1, -1) //removes the last character (in this case a space)
return output
// UNICODE: Convert to regex?
//Used to strip text of everything but letters and numbers, make letters lowercase, and turn spaces into .'s.
//Make sure the text hasn't been encoded if using this.
/proc/sanitize_for_account(text)
if(!text) return ""
var/list/dat = list()
var/last_was_fullstop = 1
for(var/i=1, i<=length(text), i++)
var/ascii_char = text2ascii(text,i)
switch(ascii_char)
if(65 to 90) //A-Z, make them lowercase
dat += ascii2text(ascii_char + 32)
last_was_fullstop = 0
if(97 to 122) //a-z
dat += ascii2text(ascii_char)
last_was_fullstop = 0
if(48 to 57) //0-9
dat += ascii2text(ascii_char)
last_was_fullstop = 0
if(32) //space
if(last_was_fullstop)
continue
dat += "." //We turn these into ., but avoid repeats or . at start.
last_was_fullstop = 1
if(46) //.
if(last_was_fullstop)
continue
dat += "."
last_was_fullstop = 1
if(dat[length(dat)] == ".") //kill trailing .
dat.Cut(length(dat))
return jointext(dat, null)
// Strips text of everything alphanumeric characters, '_' and '-'. Converts spaces to '_'
/proc/sanitize_for_group(text)
if(!text) return ""
var/list/dat = list()
for(var/i=1, i<=length(text), i++)
var/ascii_char = text2ascii(text,i)
switch(ascii_char)
if(65 to 90) //A-Z
dat += ascii2text(ascii_char)
if(97 to 122) //a-z
dat += ascii2text(ascii_char)
if(48 to 57) //0-9
dat += ascii2text(ascii_char)
if(45, 95) //-, _
dat += ascii2text(ascii_char)
if(32) //space
dat += "_"
return jointext(dat, null)
//Used to strip text of everything but letters and numbers, and select special symbols.
//Requires that the filename has an alphanumeric character.
/proc/sanitize_for_file(text)
if(!text) return ""
var/list/dat = list()
var/has_alphanumeric = FALSE
var/last_was_fullstop = FALSE
for(var/i=1, i<=length(text), i++)
var/ascii_char = text2ascii(text,i)
switch(ascii_char)
if(65 to 90) //A-Z
dat += ascii2text(ascii_char)
has_alphanumeric = TRUE
last_was_fullstop = FALSE
if(97 to 122) //a-z
dat += ascii2text(ascii_char)
has_alphanumeric = TRUE
last_was_fullstop = FALSE
if(48 to 57) //0-9
dat += ascii2text(ascii_char)
has_alphanumeric = TRUE
last_was_fullstop = FALSE
if(32) //space
dat += ascii2text(ascii_char)
last_was_fullstop = FALSE
if(33, 36, 40, 41, 42, 45, 95) //!, $, (, ), *, -, _
dat += ascii2text(ascii_char)
last_was_fullstop = FALSE
if(46) //.
if(last_was_fullstop) // No repeats of . to avoid confusion with ..
continue
dat += ascii2text(ascii_char)
last_was_fullstop = TRUE
if(!has_alphanumeric)
return ""
if(dat[length(dat)] == ".") //kill trailing .
dat.Cut(length(dat))
return jointext(dat, null)
// UNICODE: Convert to regex?
//Returns null if there is any bad text in the string
/proc/reject_bad_text(var/text, var/max_length=512)
if(length(text) > max_length) return //message too long
var/non_whitespace = 0
for(var/i=1, i<=length(text), i++)
switch(text2ascii(text,i))
if(62,60,92,47) return //rejects the text if it contains these bad characters: <, >, \ or /
if(127 to 255) return //rejects non-ASCII letters
if(0 to 31) return //more weird stuff
if(32) continue //whitespace
else non_whitespace = 1
if(non_whitespace) return text //only accepts the text if it has some non-spaces
//Old variant. Haven't dared to replace in some places.
/proc/sanitize_old(var/t,var/list/repl_chars = list("\n"="#","\t"="#"))
return html_encode(replace_characters(t,repl_chars))
/*
* Text searches
*/
//Checks the beginning of a string for a specified sub-string
//Returns the position of the substring or 0 if it was not found
/proc/dd_hasprefix(text, prefix)
var/start = 1
var/end = length(prefix) + 1
return findtext(text, prefix, start, end)
//Checks the beginning of a string for a specified sub-string. This proc is case sensitive
//Returns the position of the substring or 0 if it was not found
/proc/dd_hasprefix_case(text, prefix)
var/start = 1
var/end = length(prefix) + 1
return findtextEx(text, prefix, start, end)
//Checks the end of a string for a specified substring.
//Returns the position of the substring or 0 if it was not found
/proc/dd_hassuffix(text, suffix)
var/start = length(text) - length(suffix)
if(start)
return findtext(text, suffix, start, null)
return
//Checks the end of a string for a specified substring. This proc is case sensitive
//Returns the position of the substring or 0 if it was not found
/proc/dd_hassuffix_case(text, suffix)
var/start = length(text) - length(suffix)
if(start)
return findtextEx(text, suffix, start, null)
/*
* Text modification
*/
/proc/replace_characters(t, list/repl_chars)
for(var/char in repl_chars)
t = replacetext(t, char, repl_chars[char])
return t
//Adds 'u' number of zeros ahead of the text 't'
/proc/add_zero(t, u)
return pad_left(t, u, "0")
// Adds the required amount of 'character' in front of 'text' to extend the lengh to 'desired_length', if it is shorter
// No consideration are made for a multi-character 'character' input
/proc/pad_left(text, desired_length, character)
var/padding = generate_padding(length_char(text), desired_length, character)
return length(padding) ? "[padding][text]" : text
// Adds the required amount of 'character' after 'text' to extend the lengh to 'desired_length', if it is shorter
// No consideration are made for a multi-character 'character' input
/proc/pad_right(text, desired_length, character)
var/padding = generate_padding(length_char(text), desired_length, character)
return length(padding) ? "[text][padding]" : text
/proc/generate_padding(current_length, desired_length, character)
if(current_length >= desired_length)
return ""
var/characters = list()
for(var/i = 1 to (desired_length - current_length))
characters += character
return JOINTEXT(characters)
// Returns a string with reserved characters and spaces before the first letter removed
// not work for unicode spaces - you should cleanup them first with sanitize()
/proc/trim_left(text)
for (var/i = 1 to length(text))
if (text2ascii(text, i) > 32)
return copytext(text, i)
return ""
// Returns a string with reserved characters and spaces after the last letter removed
// not work for unicode spaces - you should cleanup them first with sanitize()
/proc/trim_right(text)
for (var/i = length(text), i > 0, i--)
if (text2ascii(text, i) > 32)
return copytext(text, 1, i + 1)
return ""
// Returns a string with reserved characters and spaces before the first word and after the last word removed.
// not work for unicode spaces - you should cleanup them first with sanitize()
/proc/trim(text)
return trim_left(trim_right(text))
//Returns a string with the first element of the string capitalized.
// NOTE: This will not work if there are any HTML tags.
/proc/capitalize(text)
if(text)
text = uppertext(text[1]) + copytext(text, 1 + length(text[1]))
return text
// Returns a string with the first alphabetical element of the string capitalized, skipping HTML tags.
/proc/capitalize_proper_html(text)
var/static/regex/split_html_regex = regex(@"(^<[^>]*>)([A-Za-z])(.+)$")
if(!text)
return text
if(!split_html_regex.Find(text))
return capitalize(text)
split_html_regex.group[2] = uppertext(split_html_regex.group[2])
return JOINTEXT(split_html_regex.group)
//Returns a string with the first element of the every word of the string capitalized.
/proc/capitalize_words(text)
var/list/S = splittext(text, " ")
var/list/M = list()
for (var/w in S)
M += capitalize(w)
return jointext(M, " ")
/proc/strip_non_ascii(text)
var/static/regex/non_ascii_regex = regex(@"[^\x00-\x7F]+", "g")
return non_ascii_regex.Replace(text, "")
//This proc strips html properly, remove < > and all text between
//for complete text sanitizing should be used sanitize()
/proc/strip_html_properly(input)
if(!input)
return
var/opentag = 1 //These store the position of < and > respectively.
var/closetag = 1
while(1)
opentag = findtext(input, "<")
closetag = findtext(input, ">")
if(closetag && opentag)
if(closetag < opentag)
input = copytext(input, (closetag + 1))
else
input = copytext(input, 1, opentag) + copytext(input, (closetag + 1))
else if(closetag || opentag)
if(opentag)
input = copytext(input, 1, opentag)
else
input = copytext(input, (closetag + 1))
else
break
return input
//Used in preferences' SetFlavorText and human's set_flavor verb
//Previews a string of len or less length
/proc/TextPreview(var/string,var/len=40)
if(length(string) <= len)
if(!length(string))
return "\[...\]"
else
return string
else
return "[copytext_preserve_html(string, 1, 37)]..."
//alternative copytext() for encoded text, doesn't break html entities (" and other)
/proc/copytext_preserve_html(var/text, var/first, var/last)
return html_encode(copytext(html_decode(text), first, last))
/proc/create_text_tag(var/tagname, var/tagdesc = tagname, var/client/C = null)
if(!(C && C.get_preference_value(/datum/client_preference/chat_tags) == PREF_SHOW))
return tagdesc
return "<IMG src='\ref['./icons/chattags.dmi']' class='text_tag' iconstate='[tagname]'" + (tagdesc ? " alt='[tagdesc]'" : "") + ">"
/proc/contains_az09(var/input)
for(var/i=1, i<=length(input), i++)
var/ascii_char = text2ascii(input,i)
switch(ascii_char)
// A .. Z
if(65 to 90) //Uppercase Letters
return 1
// a .. z
if(97 to 122) //Lowercase Letters
return 1
// 0 .. 9
if(48 to 57) //Numbers
return 1
return 0
/proc/generateRandomString(var/length)
. = list()
for(var/a in 1 to length)
var/letter = rand(33,126)
. += ascii2text(letter)
. = jointext(.,null)
#define starts_with(string, substring) (copytext(string,1,1+length(substring)) == substring)
/proc/pencode2html(t)
t = replacetext(t, "\n", "<BR>")
t = replacetext(t, "\[center\]", "<center>")
t = replacetext(t, "\[/center\]", "</center>")
t = replacetext(t, "\[br\]", "<BR>")
t = replacetext(t, "\[b\]", "<B>")
t = replacetext(t, "\[/b\]", "</B>")
t = replacetext(t, "\[i\]", "<I>")
t = replacetext(t, "\[/i\]", "</I>")
t = replacetext(t, "\[u\]", "<U>")
t = replacetext(t, "\[/u\]", "</U>")
t = replacetext(t, "\[time\]", "[stationtime2text()]")
t = replacetext(t, "\[date\]", "[stationdate2text()]")
t = replacetext(t, "\[large\]", "<font size=\"4\">")
t = replacetext(t, "\[/large\]", "</font>")
t = replacetext(t, "\[field\]", "<span class=\"paper_field\"></span>")
t = replacetext(t, "\[h1\]", "<H1>")
t = replacetext(t, "\[/h1\]", "</H1>")
t = replacetext(t, "\[h2\]", "<H2>")
t = replacetext(t, "\[/h2\]", "</H2>")
t = replacetext(t, "\[h3\]", "<H3>")
t = replacetext(t, "\[/h3\]", "</H3>")
t = replacetext(t, "\[*\]", "<li>")
t = replacetext(t, "\[hr\]", "<HR>")
t = replacetext(t, "\[small\]", "<font size = \"1\">")
t = replacetext(t, "\[/small\]", "</font>")
t = replacetext(t, "\[list\]", "<ul>")
t = replacetext(t, "\[/list\]", "</ul>")
t = replacetext(t, "\[table\]", "<table border=1 cellspacing=0 cellpadding=3 style='border: 1px solid black;'>")
t = replacetext(t, "\[/table\]", "</td></tr></table>")
t = replacetext(t, "\[grid\]", "<table>")
t = replacetext(t, "\[/grid\]", "</td></tr></table>")
t = replacetext(t, "\[row\]", "</td><tr>")
t = replacetext(t, "\[cell\]", "<td>")
t = replacetext(t, "\[editorbr\]", "")
return t
//pencode translation to html for tags exclusive to digital files (currently email, nanoword, report editor fields,
//modular scanner data and txt file printing) and prints from them
/proc/digitalPencode2html(var/text)
text = replacetext(text, "\[pre\]", "<pre>")
text = replacetext(text, "\[/pre\]", "</pre>")
text = replacetext(text, "\[fontred\]", "<font color=\"red\">") //</font> to pass html tag unit test
text = replacetext(text, "\[fontblue\]", "<font color=\"blue\">")//</font> to pass html tag unit test
text = replacetext(text, "\[fontgreen\]", "<font color=\"green\">")
text = replacetext(text, "\[/font\]", "</font>")
text = replacetext(text, "\[redacted\]", "<span class=\"redacted\">R E D A C T E D</span>")
return pencode2html(text)
//Will kill most formatting; not recommended.
/proc/html2pencode(t)
t = replacetext(t, "<pre>", "\[pre\]")
t = replacetext(t, "</pre>", "\[/pre\]")
t = replacetext(t, "<font color=\"red\">", "\[fontred\]")//</font> to pass html tag unit test
t = replacetext(t, "<font color=\"blue\">", "\[fontblue\]")//</font> to pass html tag unit test
t = replacetext(t, "<font color=\"green\">", "\[fontgreen\]")
t = replacetext(t, "</font>", "\[/font\]")
t = replacetext(t, "<BR>", "\[br\]")
t = replacetext(t, "<br>", "\[br\]")
t = replacetext(t, "<B>", "\[b\]")
t = replacetext(t, "</B>", "\[/b\]")
t = replacetext(t, "<I>", "\[i\]")
t = replacetext(t, "</I>", "\[/i\]")
t = replacetext(t, "<U>", "\[u\]")
t = replacetext(t, "</U>", "\[/u\]")
t = replacetext(t, "<center>", "\[center\]")
t = replacetext(t, "</center>", "\[/center\]")
t = replacetext(t, "<H1>", "\[h1\]")
t = replacetext(t, "</H1>", "\[/h1\]")
t = replacetext(t, "<H2>", "\[h2\]")
t = replacetext(t, "</H2>", "\[/h2\]")
t = replacetext(t, "<H3>", "\[h3\]")
t = replacetext(t, "</H3>", "\[/h3\]")
t = replacetext(t, "<li>", "\[*\]")
t = replacetext(t, "<HR>", "\[hr\]")
t = replacetext(t, "<ul>", "\[list\]")
t = replacetext(t, "</ul>", "\[/list\]")
t = replacetext(t, "<table>", "\[grid\]")
t = replacetext(t, "</table>", "\[/grid\]")
t = replacetext(t, "<tr>", "\[row\]")
t = replacetext(t, "<td>", "\[cell\]")
t = replacetext(t, "<span class=\"paper_field\"></span>", "\[field\]")
t = replacetext(t, "<span class=\"redacted\">R E D A C T E D</span>", "\[redacted\]")
t = strip_html_properly(t)
return t
// Random password generator
/proc/GenerateKey()
//Feel free to move to Helpers.
var/newKey
newKey += pick("the", "if", "of", "as", "in", "a", "you", "from", "to", "an", "too", "little", "snow", "dead", "drunk", "rosebud", "duck", "al", "le")
newKey += pick("diamond", "beer", "mushroom", "assistant", "clown", "captain", "twinkie", "security", "nuke", "small", "big", "escape", "yellow", "gloves", "monkey", "engine", "nuclear", "ai")
newKey += pick("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")
return newKey
//Used for applying byonds text macros to strings that are loaded at runtime
/proc/apply_text_macros(string)
var/next_backslash = findtext(string, "\\")
if(!next_backslash)
return string
var/leng = length(string)
var/next_space = findtext(string, " ", next_backslash + 1)
if(!next_space)
next_space = leng - next_backslash
if(!next_space) //trailing bs
return string
var/base = next_backslash == 1 ? "" : copytext(string, 1, next_backslash)
var/macro = lowertext(copytext(string, next_backslash + 1, next_space))
var/rest = next_backslash > leng ? "" : copytext(string, next_space + 1)
//See http://www.byond.com/docs/ref/info.html#/DM/text/macros
switch(macro)
//prefixes/agnostic
if("the")
rest = text("\the []", rest)
if("a")
rest = text("\a []", rest)
if("an")
rest = text("\an []", rest)
if("proper")
rest = text("\proper []", rest)
if("improper")
rest = text("\improper []", rest)
if("roman")
rest = text("\roman []", rest)
//postfixes
if("th")
base = text("[]\th", rest)
if("s")
base = text("[]\s", rest)
if("he")
base = text("[]\he", rest)
if("she")
base = text("[]\she", rest)
if("his")
base = text("[]\his", rest)
if("himself")
base = text("[]\himself", rest)
if("herself")
base = text("[]\herself", rest)
if("hers")
base = text("[]\hers", rest)
. = base
if(rest)
. += .(rest)
/proc/deep_string_equals(var/A, var/B)
if (length(A) != length(B))
return FALSE
for (var/i = 1 to length(A))
if (text2ascii(A, i) != text2ascii(B, i))
return FALSE
return TRUE
// If char isn't part of the text the entire text is returned
/proc/copytext_after_last(var/text, var/char)
var/regex/copytext_regex = regex("(\[^[char]\]*)$")
copytext_regex.Find(text)
return copytext_regex.group[1]
/proc/sql_sanitize_text(var/text)
text = replacetext(text, "'", "''")
text = replacetext(text, ";", "")
text = replacetext(text, "&", "")
return text
var/global/list/fullstop_alternatives = list(".", "!", "?")
#define APPEND_FULLSTOP_IF_NEEDED(TXT) ((copytext_char(TXT, -1, 0) in global.fullstop_alternatives) ? TXT : "[TXT].")
/proc/make_rainbow(var/msg)
var/static/list/rainbow_classes = list(
"font_red",
"font_orange",
"font_yellow",
"font_green",
"font_blue",
"font_violet",
"font_purple"
)
for(var/i = 1 to length(msg))
. += "<span class='[pick(rainbow_classes)]'>[copytext(msg, i, i+1)]</span>"
// Returns direction-string, rounded to multiples of 22.5, from the first parameter to the second
// N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW
/proc/get_compass_direction_string(var/turf/A, var/turf/B)
var/degree = Get_Angle(A, B)
switch(round(degree, 22.5) % 360) // % appears to round down floats, hence below values all being integers
if(0)
return "North"
if(22)
return "North-Northeast"
if(45)
return "Northeast"
if(67)
return "East-Northeast"
if(90)
return "East"
if(112)
return "East-Southeast"
if(135)
return "Southeast"
if(157)
return "South-Southeast"
if(180)
return "South"
if(202)
return "South-Southwest"
if(225)
return "Southwest"
if(247)
return "West-Southwest"
if(270)
return "West"
if(292)
return "West-Northwest"
if(315)
return "Northwest"
if(337)
return "North-Northwest"
///Returns true if the text starts with the given sequence of characters.
/proc/text_starts_with(var/text, var/start)
return copytext(text, 1, length(start) + 1) == start
///Returns true if the text ends with the given sequence of characters.
/proc/text_ends_with(var/text, var/end)
var/tlen = length(text)
return copytext(text, ((tlen - length(end)) + 1), tlen + 1) == end
///Returns true if the text ends with ANY of the given sequences of characters.
/proc/text_ends_with_any_of(var/text, var/list/endings)
for(var/ending in endings)
if(text_ends_with(text, ending))
return ending
return
///Siblants that should end with es
var/global/list/plural_siblants = list("ss", "x", "sh", "ch")
///Vocalized y sounds that needs to end in -ies when made plural
var/global/list/plural_vocalized_y = list("quy", "by", "dy", "fy", "gy", "hy", "jy", "ky", "ly", "my", "ny", "py", "ry", "sy", "ty", "vy", "xy", "zy")
///Plurals endings in -ves
var/global/list/plural_endings_in_ves = list("fe", "af")
///Plurals endings in -sses or -zzes
var/global/list/plural_endings_with_doubled_letter = list("as", "ez")
///Words that have a different plural form, and their plural form
var/global/list/apophonic_plurals = list(
"foot" = "feet",
"goose" = "geese",
"louse" = "lice",
"man" = "men",
"woman" = "women",
"mouse" = "mice",
"tooth" = "teeth",
"ox" = "oxen",
//#TODO: Add more of those
)
///Used to tell how to make it a plural word and etc.
var/global/list/english_loanwords = list(
"fungus",
"cactus",
//#TODO: Add more of those
)
///Words that stay the same in plural
var/global/list/plural_words_unchanged = list(
"series",
"means",
"species"
//#TODO: Add more of those
)
///Properly changes the given word (or the last word of the string) into a plural word. Applies a bunch of exceptions from the english language.
/proc/text_make_plural(var/word)
if(!length(word))
return
var/initial_word = word
//If someone passed us several words, just keep the last one.
var/list/splited = splittext_char(word, " ", 1, length(word)+1, FALSE)
//log_debug("splitted '[initial_word]' to [log_info_line(splited)].")
if(length(splited) > 1)
word = splited[splited.len]
else
splited = null
//Words that don't change when pluralized
if(global.plural_words_unchanged[word])
return initial_word
//Apophonic plurals
if(global.apophonic_plurals[word])
word = global.apophonic_plurals[word]
//Siblants + plurals of nouns in -o preceded by a consonant. Loanwords ending in o just ends with an s
else if(text_ends_with_any_of(word, global.plural_siblants) || (text_ends_with(word, "o") && !(word in global.english_loanwords)))
word = "[word]es"
//Plurals of nouns in -y
else if(text_ends_with_any_of(word, global.plural_vocalized_y))
word = "[copytext(word, 1, length(word))]ies"
// -f and -fe endings
else if(text_ends_with_any_of(word, global.plural_endings_in_ves))
word = "[copytext(word, 1, length(word) - 1)]ves" //EX: calf -> calves, leaf -> leaves, knife -> knives
//some ‘-s’ and ‘-z’ endings
else if(text_ends_with_any_of(word, global.plural_endings_with_doubled_letter))
word = "[word][copytext(word, length(word), length(word) + 1)]es" //Ex: gas -> gas'ses', fez -> fez'zes'
//Plurals of nouns in -us
else if(text_ends_with(word, "us"))
if(!(word in global.english_loanwords))
word = "[word]es"
else
word = "[copytext(word, 1, length(word) - 1)]i" //EX: Cactus -> Cacti, Fungus -> Fungi
//Finally just go with the basic rules
else
if(text_ends_with(word, "s"))
word = "[word]es"
else
word = "[word]s"
//Put the sentence back together, if applicable
if(splited)
word = "[jointext(splited, " ", 1, length(splited))] [word]"
return word
var/global/list/copula_verbs = list("is" = TRUE, "am" = TRUE, "are" = TRUE)
var/global/list/has_verbs = list("has" = TRUE, "have" = TRUE)
var/global/list/does_verbs = list("does" = TRUE, "do" = TRUE)
var/global/list/agreement_exception_verbs = list("can" = TRUE, "cannot" = TRUE, "can't" = TRUE, "will" = TRUE, "won't" = TRUE, "shall" = TRUE, "shan't" = TRUE, "may" = TRUE, "must" = TRUE, "ought" = TRUE, "could" = TRUE, "would" = TRUE, "should" = TRUE)
/proc/verb_agree_with_pronouns(var/use_verb, var/decl/pronouns/use_pronouns, is_after_pronoun = TRUE)
// deal with compound verb phrases here!
// basically, skip all our adverbs, inflect only the first non-adverb word we find (which is hopefully a verb) and then keep everything after unchanged
// this will properly handle "successfully finish doing" and "properly manage to do"
// but not "somehow finish doing" which will get turned into "somehows finish doing" :(
// todo: fix that
var/list/words = splittext(use_verb, " ")
if(length(words) > 1)
var/last_adverb = 1
for(var/word_index in 1 to length(words))
if(text_ends_with(words[word_index], "ly"))
last_adverb = max(word_index, last_adverb)
use_verb = words[last_adverb]
// This overengineered nonsense ensures that we shouldn't end up with any doubled or missing spaces.
return jointext_no_nulls(list(jointext(words.Copy(1, last_adverb), " "), verb_agree_with_pronouns(use_verb, use_pronouns, is_after_pronoun = is_after_pronoun), jointext(words.Copy(last_adverb + 1, length(words) + 1), " ")), " ")
if(!is_after_pronoun && use_pronouns.pluralize_verb == /decl/pronouns::PLURALIZE_PSEUDO)
return use_verb // do not inflect pseudoplural unless after a pronoun
if(global.copula_verbs[use_verb])
return use_pronouns.is
else if(global.has_verbs[use_verb])
return use_pronouns.has
else if(global.does_verbs[use_verb])
return use_pronouns.does
else if(global.agreement_exception_verbs[use_verb])
return use_verb
// text_make_plural is intended for nouns, but should hopefully work for "fly" -> "flies" and similar.
switch(use_pronouns.pluralize_verb)
if(/decl/pronouns::PLURALIZE_NONE)
return use_verb
if(/decl/pronouns::PLURALIZE_PSEUDO)
// technically only the plural side can get hit here
// but better safe than sorry
return is_after_pronoun ? text_make_plural(use_verb) : use_verb
if(/decl/pronouns::PLURALIZE_ALL)
return text_make_plural(use_verb)
else
CRASH("Invalid value [use_pronouns.pluralize_verb] for [use_pronouns]!")
// Surely we have this defined somewhere already??
/proc/repeatstring(str, num)
. = list()
for(var/i = 1 to num)
. += str
. = JOINTEXT(.)
/proc/jointext_no_nulls(list/L, Glue, Start = 1, End = 0)
var/list/temp = L.Copy()
LIST_CLEAR_NULLS(temp)
return jointext(temp, Glue, Start, End)