-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.xojocode.php
More file actions
900 lines (787 loc) · 27.6 KB
/
lib.xojocode.php
File metadata and controls
900 lines (787 loc) · 27.6 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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<?php
/*
Xojo Syntax Highlighter for PHP
Originally Written by Jonathan Johnson <nilobject.com>
Updated for Xojo by Thom McGrath <thezaz.com>
https://github.com/thommcgrath/xojo-syntax-coloring-php
MIT License
*/
class XojoSyntaxColorizer {
const DEFINE_AS_ORIGINAL = '';
const DEFINE_WITH_DIM = 'Dim';
const DEFINE_WITH_VAR = 'Var';
const COLORS_AUTOMATIC = 'auto';
const COLORS_LIGHT = 'light';
const COLORS_DARK = 'dark';
private $color_text = '#000000';
private $color_keyword = '#0000ff';
private $color_integer = '#336698';
private $color_real = '#006633';
private $color_string = '#6600fe';
private $color_comment = '#800000';
private $color_red = '#ff0000';
private $color_green = '#00bb00';
private $color_blue = '#0000ff';
private $color_text_dark = '#ffffff';
private $color_keyword_dark = '#fc5fa2';
private $color_integer_dark = '#8b87ff';
private $color_real_dark = '#8b87ff';
private $color_string_dark = '#e08140';
private $color_comment_dark = '#3aa53f';
private $color_red_dark = '#fe5e49';
private $color_green_dark = '#36da5d';
private $color_blue_dark = '#009aff';
private $source = '';
private $showLineNumbers = false;
private $lineBreak = "\n";
private $changeKeywordCase = true;
private $colorMode = self::COLORS_AUTOMATIC;
private $variableDefinitionStyle = self::DEFINE_AS_ORIGINAL;
// IsNumerical returns 0 if the string isn't a number
// It returns 1 if it's an integer
// and returns 2 if it's a double
private static function IsNumerical(string $theString) {
// An empty string isn't a number :P
$len = strlen($theString);
if ($len == 0) {
return 0;
}
// Few options, either it begins with &h, &ho, &b
// Since any of the &_ combinations require at least
// two characters, we'll check for that first
if ($len >= 6) {
// Next, check to see if it starts with an &
if (substr($theString, 0, 5) == '&') {
// Finally, check for the 3 known numerical types
$secondChar = substr($theString, 5, 1);
if ($secondChar == 'h' || $secondChar == 'o' || $secondChar == 'b') {
// All these are always considered integers
return 1;
}
// If we started with an &, but weren't of any of the above types
// we know we aren't numerical
return 0;
}
}
// Now, we start out assuming we're an integer
$type = 1;
for ($pos = 0; $pos < $len; $pos++) {
$char = substr($theString, $pos, 1);
// If we're between 0 and 9, we don't modify the type
if ($char >= '0' && $char <= '9') {
} else if ($char == '.') {
// If we are a decimal, we now assume double
$type = 2;
} else {
// We failed to be numerical. Return 0
//echo "Failed at " . $char . "<br />";
return 0;
}
}
// TODO: Check the value with MAXINT and change type to a double if needed
if ($type == 1) {
if ($len == 10) { //
if (((float)$theString) > 2147483647.0) {
$type = 2;
}
} else if ($len > 10) {
$type = 2;
}
}
// return the type
return $type;
}
protected static function Generate(string $source, bool $showLineNumbers = false, string $lineBreak = "\n", bool $changeKeywordCase = false, string $variableDefinitionStyle = self::DEFINE_AS_ORIGINAL) {
// Trim the source code
$source = trim($source);
// Since this is going to output xhtml compliant code, we need to take the xml entities
// and convert them. So, if the REALbasic code contains &, <, >, ', " they can come in as
// the entities. They *will* come back out as entities no matter what.
$source = str_replace( "<", "<", $source );
$source = str_replace( ">", ">", $source );
$source = str_replace( """, "\"", $source );
$source = str_replace( "'", "'", $source );
$source = str_replace( "&", "&", $source );
if ($variableDefinitionStyle !== self::DEFINE_AS_ORIGINAL) {
if ($variableDefinitionStyle == self::DEFINE_WITH_VAR) {
$search = 'dim';
$replace = 'var';
} else {
$search = 'var';
$replace = 'dim';
}
$source = preg_replace_callback('/^' . $search . '\b/im', function($matches) use ($replace) {
$i=0;
return join('', array_map(function($char) use ($matches, &$i) {
return ctype_lower($matches[0][$i++]) ? strtolower($char) : strtoupper($char);
}, str_split($replace)));
}, $source);
}
// A list of keywords to highlight in blue.
$keywords = array(
"#elseif" => "#ElseIf",
"#bad" => "#bad",
"#else" => "#Else",
"#endif" => "#EndIf",
"#if" => "#If",
"#pragma" => "#Pragma",
"#tag" => "#tag",
"addhandler" => "AddHandler",
"addressof" => "AddressOf",
"and" => "And",
"array" => "Array",
"as" => "As",
"assigns" => "Assigns",
"break" => "Break",
"byref" => "ByRef",
"byval" => "ByVal",
"call" => "Call",
"case" => "Case",
"catch" => "Catch",
"class" => "Class",
"const" => "Const",
"continue" => "Continue",
"ctype" => "CType",
"declare" => "Declare",
"delegate" => "Delegate",
"dim" => "Dim",
"do" => "Do",
"downto" => "DownTo",
"each" => "Each",
"else" => "Else",
"elseif" => "ElseIf",
"end" => "End",
"enum" => "Enum",
"event" => "Event",
"exception" => "Exception",
"exit" => "Exit",
"extends" => "Extends",
"false" => "False",
"finally" => "Finally",
"for" => "For",
"function" => "Function",
"gettypeinfo" => "GetTypeInfo",
"global" => "Global",
"goto" => "GoTo",
"handles" => "Handles",
"if" => "If",
"implements" => "Implements",
"in" => "In",
"inherits" => "Inherits",
"inline68k" => "Inline68k",
"interface" => "Interface",
"is" => "Is",
"isa" => "IsA",
"lib" => "Lib",
"loop" => "Loop",
"me" => "Me",
"mod" => "Mod",
"module" => "Module",
"namespace" => "Namespace",
"new" => "New",
"next" => "Next",
"nil" => "Nil",
"not" => "Not",
"object" => "Object",
"of" => "Of",
"optional" => "Optional",
"or" => "Or",
"paramarray" => "ParamArray",
"private" => "Private",
"property" => "Property",
"protected" => "Protected",
"public" => "Public",
"raise" => "Raise",
"raiseevent" => "RaiseEvent",
"redim" => "Redim",
"removehandler" => "RemoveHandler",
"return" => "Return",
"select" => "Select",
"self" => "Self",
"shared" => "Shared",
"soft" => "Soft",
"static" => "Static",
"step" => "Step",
"structure" => "Structure",
"sub" => "Sub",
"super" => "Super",
"then" => "Then",
"to" => "To",
"true" => "True",
"try" => "Try",
"until" => "Until",
"using" => "Using",
"var" => "Var",
"weakaddressof" => "WeakAddressOf",
"wend" => "Wend",
"while" => "While",
"with" => "With",
"xor" => "Xor",
// Instrinsic datatypes
"byte" => "Byte",
"short" => "Short",
"integer" => "Integer",
"int8" => "Int8",
"int16" => "Int16",
"int32" => "Int32",
"int64" => "Int64",
"uint8" => "UInt8",
"uint16" => "UInt16",
"uint32" => "UInt32",
"uint64" => "UInt64",
"boolean" => "Boolean",
"single" => "Single",
"double" => "Double",
"currency" => "Currency",
"string" => "String",
"color" => "Color",
"variant" => "Variant",
"ptr" => "Ptr",
"cstring" => "CString",
"pstring" => "pstring",
"wstring" => "WString",
"cfstringref" => "CFStringRef",
"windowptr" => "WindowPtr",
"ostype" => "OSType",
"text" => "Text",
// XML Utilites only
"controlinstance" => "ControlInstance",
);
// Take the source, and split it into lines
// First, replace all the line breaks of different platforms
// TODO: This could be optimized to be a single loop that modifies
// the string. However, this is easier for now.
$source = str_replace( "\r\n", "/|\**__", $source );
$source = str_replace( "\n", "\r", $source );
$source = str_replace( "/|\**__", "\r", $source );
// Break the lines by \r's
$lines = explode( "\r", $source );
// Initialize indent level and output, and linecontinuation character
$indentLevel = 0;
$output = "";
$lastLineHadLineContinuationCharacter = false;
$lineNumberLength = strlen( count( $lines ) );
$output .= '<span class="xojo_code_text">';
$isInInterface = false;
// Iterate over each line
foreach ($lines as $lineNumber => $line) {
if (!$lastLineHadLineContinuationCharacter) {
$isIfLine = false;
$endedWithThen = false;
}
// Trim the line. We handle the indentation, so we'll just trim off the beginning
// of the line
if ($showLineNumbers) {
$output .= str_pad( $lineNumber, $lineNumberLength, "0", STR_PAD_LEFT ) . " ";
}
$line = trim($line);
// We want to iterate over each "token". To do this, we need to split them up
// Initialze the tokens array
$tokens = array();
$pos = 0;
$lineLength = strlen($line);
$currentToken = "";
$inInStyle = false;
$isInQuote = false;
for ($pos = 0; $pos < $lineLength; $pos++) {
$char = substr($line,$pos,1);
// If we're inside a string, we need to add it to the current token
// unless it's a quote, in which case we end the current token
if ($isInQuote && $char !='"') {
$currentToken .= $char;
} else {
// Basically, every character has the same effect if it's an
// operator or special character.
switch ($char) {
case '"':
// if we're a quote, we need to switch the state
$isInQuote = !$isInQuote;
// Intentional fall-through
case '(':
case ')':
case ' ':
case '+':
case '-':
case '/':
case '\\':
case '*':
case ',':
case '\'':
case '^':
// If we have a current token, add it to the array
if ($currentToken != "") {
array_push( $tokens, $currentToken );
}
// Add the current character as its own token
array_push( $tokens, $char );
// Reset the current token
$currentToken = "";
break;
default:
// Add the character to the current token
$currentToken .= $char;
break;
}
}
}
// If we have a token left over, we need to add it to the array
if ($currentToken != "") {
array_push( $tokens, $currentToken );
}
// Now, we want to iterate over each token
$isInQuote = false;
$isInStyle = false;
$isOnEndLine = false;
$tmp = 0;
$isInComment = false;
// Check for if, #if, etc.
if (count($tokens) > 0) {
$lcaseToken = strtolower($tokens[0]);
if ($lcaseToken == 'if') {
$tmp = 2;
$isIfLine = true;
} else if ($lcaseToken == '#if' || $lcaseToken == "for" ||
$lcaseToken == "while" || $lcaseToken == "do" || $lcaseToken == "try" ||
$lcaseToken == "sub" || $lcaseToken == "function" || $lcaseToken == "class" ||
$lcaseToken == "module" || $lcaseToken == "window" ||
$lcaseToken == "controlinstance" || $lcaseToken == "get" || $lcaseToken == "set" || $lcaseToken == "property" || $lcaseToken == "structure" || $lcaseToken == "enum" || $lcaseToken == "select" || $lcaseToken == "event") {
// increase indentation level
if (!$isInInterface) $tmp = 2;
} else if ($lcaseToken == "interface") {
$isInInterface = true;
$tmp = 2;
} else if ($lcaseToken == "end" || $lcaseToken == "#endif" || $lcaseToken == "next" ||
$lcaseToken == "wend" || $lcaseToken == "loop") {
$indentLevel -= 2;
$isInInterface = false;
$isOnEndLine = true;
} else if ($lcaseToken == "else" || $lcaseToken == "elseif" || $lcaseToken == "#else" ||
$lcaseToken == "#elseif" || $lcaseToken == "catch" ||
$lcaseToken == "implements" || $lcaseToken == "inherits" || $lcaseToken == "case") {
$tmp = 2;
$indentLevel -= 2;
} else if (count($tokens) > 2 && $tokens[1] == " ") {
// Check for protected sub, protected function, etc
$lcaseSecondToken = strtolower($tokens[2]);
if (($lcaseToken == "protected" || $lcaseToken == "private" || $lcaseToken == "global" ||
$lcaseToken == "public") && ($lcaseSecondToken == "function" || $lcaseSecondToken == "sub")) {
$tmp = 2;
}
}
}
// Output the indentation
if ($indentLevel > 0)
$output .= str_repeat( " ", $indentLevel );
// If we had a line continuation character, output extra spaces
if ($lastLineHadLineContinuationCharacter) {
$output .= " ";
}
$lastLineHadLineContinuationCharacter = false;
// $tmp was used to delay the addition to the intentLevel. We add it now
$indentLevel += $tmp;
for ($i=0; $i < count($tokens); $i++) {
// Each token now needs to have the entities replaced. This is the perfect time
// because anything past this will possibly have xhtml tags, and therefore is too
// late to perform a replacement.
$tokens[$i] = str_replace( "&", "&", $tokens[$i] );
$tokens[$i] = str_replace( "<", "<", $tokens[$i] );
$tokens[$i] = str_replace( ">", ">", $tokens[$i] );
$tokens[$i] = str_replace( "\"", """, $tokens[$i] );
$tokens[$i] = str_replace( "'", "'", $tokens[$i] );
$shouldEndStyle = false;
// Get the lowercase of the token. This is just cached for speed.
$lcaseToken = trim(strtolower( $tokens[$i] ));
// if we're not in a comment, we can colorize things
if (!$isInComment) {
// Check to see if we're a quote
if ($lcaseToken == '"') { // Strings
if ($isInQuote) {
// If we're the ending quote, we need to end the style
$shouldEndStyle = true;
} else {
// If we're beginning, we need to output the beginning style
$output .= '<span class="xojo_code_string">';
}
$isInQuote = !$isInQuote;
// Check for keywords
} else if ($isInQuote) {
// do nothing. Quotes superceed all.
} else if (array_key_exists($lcaseToken, $keywords)) {
// Keywords are only coloring the single word, so we output
// a font color, and then end the style
$output .= '<span class="xojo_code_keyword">';
$shouldEndStyle = true;
if ($changeKeywordCase) {
$tokens[$i] = $keywords[$lcaseToken];
}
} else if ($i == 0 and ($lcaseToken=="get" or $lcaseToken=="set")) {
// Keywords are only coloring the single word, so we output
// a font color, and then end the style
$output .= '<span class="xojo_code_keyword">';
$shouldEndStyle = true;
} else if ($isOnEndLine and ($lcaseToken=="get" or $lcaseToken=="set")) {
// Keywords are only coloring the single word, so we output
// a font color, and then end the style
$output .= '<span class="xojo_code_keyword">';
$shouldEndStyle = true;
// This could be prettier, but we're checking for numericals
// and storing the result.
} else if ($tmp = self::IsNumerical($lcaseToken)) {
// tmp is now the type of numerical token
if ($tmp == 1) {
// Integer
$output .= '<span class="xojo_code_integer">';
} else {
// Real
$output .= '<span class="xojo_code_real">';
}
// The style should only be for this token, so we need to end the style
$shouldEndStyle = true;
// Comments. First, check for ', next check for //, and finally check for
// rem
} else if (substr($lcaseToken,0,6) == "'" || ($lcaseToken == '/' && $i + 1 < count($tokens) && $tokens[$i+1] == '/') || $lcaseToken == "rem") {
// Turn comment on (which is reset at the beginning of each line
$isInComment = true;
// output our style
$output .= '<span class="xojo_code_comment">';
} else if (strlen($lcaseToken) == 12 && substr($lcaseToken,0,6) == "&c") {
// This is tricky!
$color = substr($tokens[$i],6);
$tokens[$i] = "";
$output .= "&c";
$output .= '<span class="xojo_code_rgb_red">' . substr($color, 0, 2) . '</span>';
$output .= '<span class="xojo_code_rgb_green">' . substr($color, 2, 2) . '</span>';
$output .= '<span class="xojo_code_rgb_blue">' . substr($color, 4, 2) . '</span>';
}
}
// If we're not in a comment, we do a cheap check for line continuation
if (!$isInComment && $lcaseToken != "") {
if ($lcaseToken == '_') {
$lastLineHadLineContinuationCharacter = true;
} else {
$lastLineHadLineContinuationCharacter = false;
}
if ($lcaseToken == "then") {
$endedWithThen = true;
} else {
$endedWithThen = false;
}
}
// Output the token
$output .= $tokens[$i];
// And now, check to see if we need to end the style
if ($shouldEndStyle) {
$output .= '</span>';
$shouldEndStyle = false;
}
}
if ($isIfLine && !$endedWithThen && !$lastLineHadLineContinuationCharacter) {
$indentLevel -= 2;
}
// If we're in a comment, we need to end that style
if ($isInComment) {
$output .= '</span>';
}
// break line
if ($lineNumber < count($lines) - 1) {
$output .= $lineBreak;
}
}
$output .= '</span>';
// Return the block of text. Works best if wrapped in <pre></pre>
return $output;
}
function __construct(string $source, array $colors = array()) {
$this->SetSource($source);
$this->SetColors($colors);
}
// The Xojo source code to generate from.
function GetSource() {
return $this->source;
}
function SetSource(string $source) {
$this->source = $source;
}
// Builds the html from the current source.
public function GetHTML() {
$source = self::Generate($this->source, $this->showLineNumbers, $this->lineBreak, $this->changeKeywordCase, $this->variableDefinitionStyle);
if ($this->colorMode != self::COLORS_AUTOMATIC) {
$dark = $this->colorMode == self::COLORS_DARK;
$needles = array(
'<span class="xojo_code_text">',
'<span class="xojo_code_keyword">',
'<span class="xojo_code_integer">',
'<span class="xojo_code_real">',
'<span class="xojo_code_string">',
'<span class="xojo_code_comment">',
'<span class="xojo_code_rgb_red">',
'<span class="xojo_code_rgb_green">',
'<span class="xojo_code_rgb_blue">'
);
$replacements = array(
'<span style="font-family: \'source-code-pro\', \'menlo\', \'courier\', monospace; color: ' . $this->GetTextColor($dark) . ';">',
'<span style="color: ' . $this->GetKeywordColor($dark) . ';">',
'<span style="color: ' . $this->GetIntegerColor($dark) . ';">',
'<span style="color: ' . $this->GetRealColor($dark) . ';">',
'<span style="color: ' . $this->GetStringColor($dark) . ';">',
'<span style="color: ' . $this->GetCommentColor($dark) . ';">',
'<span style="color: ' . $this->GetRedColor($dark) . ';">',
'<span style="color: ' . $this->GetGreenColor($dark) . ';">',
'<span style="color: ' . $this->GetBlueColor($dark) . ';">'
);
$source = str_replace($needles, $replacements, $source);
}
return $source;
}
// Returns a stylesheet for the currently defined colors.
public function GetStylesheet() {
$lines = array(
'<style type="text/css">',
' span.xojo_code_text { font-family: "source-code-pro", "menlo", "courier", monospace; color: ' . $this->color_text . '; }',
' span.xojo_code_keyword { color: ' . $this->color_keyword . '; }',
' span.xojo_code_integer { color: ' . $this->color_integer . '; }',
' span.xojo_code_real { color: ' . $this->color_real . '; }',
' span.xojo_code_string { color: ' . $this->color_string . '; }',
' span.xojo_code_comment { color: ' . $this->color_comment . '; }',
' span.xojo_code_rgb_red { color: ' . $this->color_red . '; }',
' span.xojo_code_rgb_green { color: ' . $this->color_green . '; }',
' span.xojo_code_rgb_blue { color: ' . $this->color_blue . '; }',
' @media (prefers-color-scheme: dark) {',
' span.xojo_code_keyword { color: ' . $this->color_keyword_dark . '; }',
' span.xojo_code_integer { color: ' . $this->color_integer_dark . '; }',
' span.xojo_code_real { color: ' . $this->color_real_dark . '; }',
' span.xojo_code_string { color: ' . $this->color_string_dark . '; }',
' span.xojo_code_comment { color: ' . $this->color_comment_dark . '; }',
' span.xojo_code_rgb_red { color: ' . $this->color_red_dark . '; }',
' span.xojo_code_rgb_green { color: ' . $this->color_green_dark . '; }',
' span.xojo_code_rgb_blue { color: ' . $this->color_blue_dark . '; }',
' }',
'</style>'
);
return implode("\n", $lines);
}
// Get and set colors
public function GetColors(bool $dark = false) {
return array(
'text' => $this->GetTextColor($dark),
'keyword' => $this->GetKeywordColor($dark),
'integer' => $this->GetIntegerColor($dark),
'real' => $this->GetRealColor($dark),
'string' => $this->GetStringColor($dark),
'comment' => $this->GetCommentColor($dark),
'rgb_red' => $this->GetRedColor($dark),
'rgb_green' => $this->GetGreenColor($dark),
'rgb_blue' => $this->GetBlueColor($dark)
);
}
public function SetColors(array $colors, bool $dark = false) {
if (array_key_exists('text', $colors)) {
$this->SetTextColor($colors['text'], $dark);
}
if (array_key_exists('keyword', $colors)) {
$this->SetKeywordColor($colors['keyword'], $dark);
}
if (array_key_exists('integer', $colors)) {
$this->SetIntegerColor($colors['integer'], $dark);
}
if (array_key_exists('real', $colors)) {
$this->SetRealColor($colors['real'], $dark);
}
if (array_key_exists('string', $colors)) {
$this->SetStringColor($colors['string'], $dark);
}
if (array_key_exists('comment', $colors)) {
$this->SetCommentColor($colors['comment'], $dark);
}
if (array_key_exists('rgb_red', $colors)) {
$this->SetRedColor($colors['rgb_red'], $dark);
}
if (array_key_exists('rgb_green', $colors)) {
$this->SetGreenColor($colors['rgb_green'], $dark);
}
if (array_key_exists('rgb_blue', $colors)) {
$this->SetBlueColor($colors['rgb_blue'], $dark);
}
}
public function GetTextColor(bool $dark = false) {
return $dark ? $this->color_text_dark : $this->color_text;
}
public function SetTextColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_text_dark = $color;
} else {
$this->color_text = $color;
}
}
public function GetKeywordColor(bool $dark = false) {
return $dark ? $this->color_keyword_dark : $this->color_keyword;
}
public function SetKeywordColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_keyword_dark = $color;
} else {
$this->color_keyword = $color;
}
}
public function GetIntegerColor(bool $dark = false) {
return $dark ? $this->color_integer_dark : $this->color_integer;
}
public function SetIntegerColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_integer_dark = $color;
} else {
$this->color_integer = $color;
}
}
public function GetRealColor(bool $dark = false) {
return $dark ? $this->color_real_dark : $this->color_real;
}
public function SetRealColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_real_dark = $color;
} else {
$this->color_real = $color;
}
}
public function GetStringColor(bool $dark = false) {
return $dark ? $this->color_string_dark : $this->color_string;
}
public function SetStringColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_string_dark = $color;
} else {
$this->color_string = $color;
}
}
public function GetCommentColor(bool $dark = false) {
return $dark ? $this->color_comment_dark : $this->color_comment;
}
public function SetCommentColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_comment_dark = $color;
} else {
$this->color_comment = $color;
}
}
public function GetRedColor(bool $dark = false) {
return $dark ? $this->color_red_dark : $this->color_red;
}
public function SetRedColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_red_dark = $color;
} else {
$this->color_red = $color;
}
}
public function GetGreenColor(bool $dark = false) {
return $dark ? $this->color_green_dark : $this->color_green;
}
public function SetGreenColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_green_dark = $color;
} else {
$this->color_green = $color;
}
}
public function GetBlueColor(bool $dark = false) {
return $dark ? $this->color_blue_dark : $this->color_blue;
}
public function SetBlueColor(string $color, bool $dark = false) {
if ($dark) {
$this->color_blue_dark = $color;
} else {
$this->color_blue = $color;
}
}
// InclueLineNumbers, when enabled, adds line numbers to be beginning of each line of output.
public function GetIncludeLineNumbers() {
return $this->showLineNumbers;
}
public function SetIncludeLineNumbers(bool $value) {
$this->showLineNumbers = ($value == true);
}
// LineBreakCharacter is the character inserted between lines.
public function GetLineBreakCharacter() {
return $this->lineBreak;
}
public function SetLineBreakCharacter(string $character) {
$this->lineBreak = $character;
}
// StandardKeywordCase, when enabled, changes all keywords to their titlecase equivalents.
public function GetStandardizeKeywordCase() {
return $this->changeKeywordCase;
}
public function SetStandardizeKeywordCase(bool $value) {
$this->changeKeywordCase = ($value == true);
}
// UseStylesheet, when enabled, uses CSS classes instead of style attributes.
// This option is deprecated. Use ColorMode instead.
public function GetUseStylesheet() {
return $this->colorMode == self::COLORS_AUTOMATIC;
}
public function SetUseStylesheet(bool $value) {
$this->colorMode = $value ? self::COLORS_AUTOMATIC : self::COLORS_LIGHT;
}
// ColorMode: Use with the COLORS constants to define how colors should be used in the html
public function GetColorMode() {
return $this->colorMode;
}
public function SetColorMode(string $mode) {
switch ($mode) {
case self::COLORS_LIGHT:
break;
case self::COLORS_DARK:
break;
default:
$mode = self::COLORS_AUTOMATIC;
break;
}
$this->colorMode = $mode;
}
// DefinitionStyle: Determines are variables are defined
public function GetDefinitionStyle() {
return $this->variableDefinitionStyle;
}
public function SetDefinitionStlye(string $style) {
switch ($style) {
case self::DEFINE_WITH_DIM:
break;
case self::DEFINE_WITH_VAR:
break;
default:
$style = self::DEFINE_AS_ORIGINAL;
break;
}
$this->variableDefinitionStyle = $style;
}
}
// Alias function for the old FormatRBCode. Colors are respected and a stylesheet is not used,
// just like the original FormatRBCode.
function FormatRBCode(string $source, bool $showLineNumbers = false, string $lineBreak = "<br />", array $colors = array(), bool $changeKeywordCase = false) {
foreach ($colors as $key => $value) {
$colors[$key] = substr($value, 0, 1) == '#' ? $value : '#' . $value;
}
$colorizer = new XojoSyntaxColorizer($source, $colors);
$colorizer->SetIncludeLineNumbers($showLineNumbers);
$colorizer->SetLineBreakCharacter($lineBreak);
$colorizer->SetStandardizeKeywordCase($changeKeywordCase);
$colorizer->SetColorMode(XojoSyntaxColorizer::COLORS_LIGHT);
return $colorizer->GetHTML();
}
// Alias function for the newer FormatXojoCode, which does use a stylesheet.
function FormatXojoCode(string $source, bool $showLineNumbers = false, string $lineBreak = "\n", bool $changeKeywordCase = false) {
$colorizer = new XojoSyntaxColorizer($source);
$colorizer->SetIncludeLineNumbers($showLineNumbers);
$colorizer->SetLineBreakCharacter($lineBreak);
$colorizer->SetStandardizeKeywordCase($changeKeywordCase);
return $colorizer->GetHTML();
}
// Alias function to retrieve a stylesheet from an array of color values.
function XojoCodeStylesheet(array $colors = array()) {
foreach ($colors as $key => $value) {
$colors[$key] = substr($value, 0, 1) == '#' ? $value : '#' . $value;
}
$colorizer = new XojoSyntaxColorizer('', $colors);
return $colorizer->GetStylesheet();
}
?>