-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMarqueur.php
1250 lines (1004 loc) · 36.9 KB
/
TMarqueur.php
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace TMarqueur;
/**
||TMarqueur|| is an alternative lightweight markup language
for creating formatted text using a plain-text editor.
||TMarqueur.php|| is used to convert TMarqueur documents into HTML.
It is coded for PHP 8.1.
*/
const FOR_PHP = '8.1' ;
const VERSION = "0.1.202407311135" ;
const NAME = "TMarqueur aka Terminajones' Marqueur" ;
const COPYRIGHT = 'IDGAF' ;
const LICENSE = <<<END_OF_LICENSE
This project is released under the IDGAF LICENSE v1.0 that you can read here : https://github.com/SuperUserNameMan/IDGAF_LICENSE
END_OF_LICENSE;
const THIRD_PARTY_MODIFICATIONS_LIST = [
/// XXX if you alter the original source code, summarize your modifications in here, and identify yourself
];
class CONFIG {
static bool $disable_custom_attr = true ; /// disable custom attributes added to blocks
static bool $disable_html_blocks = true ; /// disable blocks that accept raw HTML content
static int $tab_size = 4 ; /// how many spaces for tab ?
static bool $auto_summary = true ;
static bool $auto_footnote = true ;
static string $summary_prefix = 'goto_'; /// prefix to automated titles anchor used to build the table of content
static string $footnote_prefix = 'footnote-'; /// prefix to footnote anchor
static string $footnote_return_prefix = 'ref-footnote-'; /// prefix to linkback to footnote reference anchor
static string $auto_summary_title_attribute = ' ondblclick="location.replace(\'#\' + TMarqueur.summary_prefix + \'summary\');"' ; /// this attribute is added to titles if TMarqueur.auto_summary
static string $footnote_title = "== Footnotes : ==" ;
static string $attribute_separator = ' / ' ; /// used to separate blocks tag's attributes and bracket tag's parameters
static string $attribute_keyval_seperator = ' : '; /// used to name the blocks tag's attributes. Ex : """"" cite : René Descartes / date : 1637 """"""
};
// XXX current source code bellow is a loosy direct translation of TMarqueur.js
$html_special_chars =
[
[ '&' , '&'],
[ '<' , '<' ],
[ '>' , '>' ],
];
$text_to_html =
[
/// Table of convertion of text codes into HTML :
/// [ text , html ] is applied if formating != 'HTML'
/// [ text , [ html ] ] is applied if formating == true
/// NOTE : ︎ force the preceding emoji or entity to be rendered in text mode
[ '->' , '→' ], //!\ ->
[ '<-' , '←' ], //!\ <-
[ '<->' , '↔' ], //!\ <->
[ ':-D' , '😁' ],
[ ':-)' , '😀' ],
[ 'X-D' , '😆' ],
[ ';-)' , '😉' ],
[ '^_^' , '😊' ],
[ ':-P' , '😋' ],
[ 'B-)' , '😎' ],
[ ':-|' , '😐' ],
[ ':-/' , '😕' ],
[ ':-S' , '😖' ],
[ ':-*' , '😗' ],
[ ';-*' , '😘' ],
[ '/!\\' , '⚠️' ],
[ '[i]' , 'ℹ️' ],
[ '(c)' , '©' ],
[ '(C)' , '©' ],
[ '(r)' , '®' ],
[ '(R)' , '®' ],
[ '(TM)' , '™' ],
[ '(tm)' , '™' ],
[ ' :' , ' :' ],
[ ' ?' , ' ?' ],
[ ' !' , ' !' ],
[ ' ;' , ' ;' ],
[ '[X]' , '❌' ],
[ '[V]' , '✅' ],
[ '[x]' , '❌︎' ],
[ '[v]' , '✅︎' ],
[ '[N]' , '❌' ],
[ '[Y]' , '✅' ],
[ '[n]' , '❌︎' ],
[ '[y]' , '✅︎' ],
[ '[NO]' , '❌' ],
[ '[YES]' , '✅' ],
[ '[no]' , '❌︎' ],
[ '[yes]' , '✅︎' ],
[ '[STAR]' , '⭐' ],
[ '[star]' , '⭐︎' ],
[ '[0-STAR]' , ' ' ],
[ '[0-star]' , ' ' ],
[ '[0-STARS]' , ' ' ],
[ '[0-stars]' , ' ' ],
[ '[1-STAR]' , '⭐' ],
[ '[1-star]' , '⭐︎' ],
[ '[1-STARS]' , '⭐' ],
[ '[1-stars]' , '⭐︎' ],
[ '[2-STARS]' , '⭐⭐' ],
[ '[2-stars]' , '⭐︎⭐︎' ],
[ '[3-STARS]' , '⭐⭐⭐' ],
[ '[3-stars]' , '⭐︎⭐︎⭐︎' ],
[ '[4-STARS]' , '⭐⭐⭐⭐' ],
[ '[4-stars]' , '⭐︎⭐︎⭐︎⭐︎' ],
[ '[5-STARS]' , '⭐⭐⭐⭐⭐' ],
[ '[5-stars]' , '⭐︎⭐︎⭐︎⭐︎⭐︎' ],
];
$escape_seq =
[
'\\' => '\' ,
'n' => '<br/>' ,
' ' => ' ',
'`' => '`' ,
];
$title_tags =
[
/// .tags : contains the list of HTML tags that will surround the content (/!\ only ASCII chr allowed)
/// .formating : true | false | 'HTML'
/// - true = replaces HTML entities and applies inline formating ;
/// - false = only replace HTML special entities ;
/// - 'HTML' = keep the content untouched (the content is raw HTML code) ;
/// .summary : true | false
/// - true : an id attribute will be defined by default, and the content will be used to feed the summary table ;
/// .summary_indent :
/// - when the summary table is converted to HTML, the value is used to indent the <li> ;
/// .hr_class :
/// - when the content is empty, a <hr> is used instead
'#' => [ 'tags' => [ 'h1' ] , 'formating' => true , 'summary' => true , 'summary_indent' => 0 , 'hr_class' => 'hr_type_1' ],
'=' => [ 'tags' => [ 'h2' ] , 'formating' => true , 'summary' => true , 'summary_indent' => 2 , 'hr_class' => 'hr_type_2' ],
'+' => [ 'tags' => [ 'h3' ] , 'formating' => true , 'summary' => true , 'summary_indent' => 4 , 'hr_class' => 'hr_type_3' ],
'-' => [ 'tags' => [ 'h4' ] , 'formating' => true , 'summary' => true , 'summary_indent' => 6 , 'hr_class' => 'hr_type_4' ],
'>' => [ 'tags' => [ 'blockquote' , 'pre' ] , 'formating' => true , 'summary' => false ], // not title, but behave the same
];
$block_tags =
[
/// .tags : contains the list of HTML tags that will surround the content (/!\ only ASCII chr allowed)
/// .attr : contains the list of default attributes. They can specified in the given order, or using
/// the name (see TMarqueur.attribute_separator and TMarqueur.attribute_keyval_seperator)
/// .formating : true | false | 'HTML'
/// - true = replaces HTML entities and applies inline formating ;
/// - false = only replace HTML special entities ;
/// - 'HTML' = keep the content untouched (the content is raw HTML code) ;
/// .custom_attr : true | false
/// - true : accept named attributes not specified into the .attr[] list ;
/// - false : if named attribute is not specified into the .attr[] list, the name will be prefixed with "data-" ;
/// .variants :
/// The content of the blocks can be parsed and interpreted by plugins/extensions.
/// These plugins/extensions are called "variants". See : TMarqueur.plug_block_variant()
'"' => [ 'type' => 'quote' , 'tags' => [ 'blockquote' ] , 'attr' => [ 'title' , 'cite' , 'date' , 'source' ] , 'formating' => true ],
'\'' => [ 'type' => 'quote-raw' , 'tags' => [ 'blockquote' ] , 'attr' => [ 'title' , 'cite' , 'date' , 'source' ] , 'formating' => false ],
'<' => [ 'type' => 'textarea' , 'tags' => [ 'textarea' ] , 'attr' => [ 'name' , 'title' ] , 'formating' => false ],
';' => [ 'type' => 'div' , 'tags' => [ 'div' ] , 'attr' => [ 'class' , 'id' ], 'formating' => true , 'custom_attr' => true ],
':' => [ 'type' => 'div-HTML' , 'tags' => [ 'div' ] , 'attr' => [ 'class' , 'id' ], 'formating' => 'HTML' , 'custom_attr' => true ],
'`' => [ 'type' => 'code' , 'tags' => [ 'pre','code' ] , 'attr' => [ 'lang' , 'title' , 'source' ] , 'formating' => false , // TODO choose to which tag goes the attributes
'attr_defaults' => [
'lang' => 'raw-text',
//'title' => function( $attr_val ) { return $attr_val[ 'lang' ] ?? '' ; },
],
],
];
$format_tags =
[
/// .tags : contains the list of HTML tags that will surround the content (/!\ only ASCII chr allowed)
/// .formating : true | false | 'HTML'
/// - true = replaces HTML entities and applies inline formating ;
/// - false = only replace HTML special entities ;
/// - 'HTML' = keep the content untouched (the content is raw HTML code) ;
'*' => [ 'tags' => [ 'b' ] , 'formating' => true ], // **bold**
'_' => [ 'tags' => [ 'u' ] , 'formating' => true ], // __underline__
'~' => [ 'tags' => [ 's' ] , 'formating' => true ], // ~~strike~~
'\'' => [ 'tags' => [ 'i' ] , 'formating' => true ], // ''italic''
'"' => [ 'tags' => [ 'q' ] , 'formating' => true ], // ""quote""
'^' => [ 'tags' => [ 'sup' ] , 'formating' => true ], // ^^superscript^^
',' => [ 'tags' => [ 'sub' ] , 'formating' => true ], // ,,subscript,,
'%' => [ 'tags' => [ 'mark' ] , 'formating' => true ], // %%marked%%
'`' => [ 'tags' => [ 'samp' ] , 'formating' => false ], // ``computer``
'|' => [ 'tags' => [ 'em' ] , 'formating' => true ], // ||emphasised||
'$' => [ 'tags' => [ 'strong' ] , 'formating' => true ], // $$important$$
'/' => [ 'tags' => [ 'span' ] , 'formating' => false ], // //unformated//
];
$brackets_tags = // /!\ only ASCII chr allowed as bracket
[
/// .terminator : contains the ending bracket (/!\ only ASCII chr allowed)
/// .html :
/// - Contains the html code used to replace the tag and its content.
/// - This html code can contain placeholders for parameters. Ex : ``§param1§`` or ``!§param2§``.
/// - The values of the parameters are extracted from the content of the tag, where they are separated using
/// the TMarqueur.attribute_separator. Ex : ``[[ param1 / param2 / param3 ]]``
/// - placeholders beginning with '!' means the value of the parameter will be escaped using
/// TMarqueur.escape_html_string().
/// .params_order : list the order of the parameters as given into the content of the tag ;
/// .params_defaults : optional list of the default value of each parameters if not provided intro the tag ;
/// .params_validator : optional list of regular expression used to validate the value of each parameter ;
/// .params_filter : optional list of regular expression used to extract the value from a parameter ;
// [[ linkname / url / target ]]
'[' => [
'type' => 'link',
'terminator' => ']' , 'html' => '<a href="!§url§" target="!§target§" title="!§url§">§content§</a>' ,
'params_order' => [ '§content§' , '!§url§' , '!§target§' ] ,
'params_defaults' => [
'!§url§' => function( array $_params ) : string { return $_params['§content§']; } ,
'!§target§' => function( array $_params ) : string
{
$val_ = $_params['!§url§'];
while( is_callable( $val_ ) ) $val_ = $val_( $_params );
return str_starts_with( $val_ , 'http' ) ? '_blank' : '_self' ;
} ,
],
'params_validator' => [
'!§url§' => '/^(http|https|ftp|mailto):.*$|^#[^ ]*$|^[^: \t]+$/i',
'!§target§' => '/^(_self|_blank)$/',
],
'variants' => [
// [[^footnote-id / title ]]
[ 'type' => 'footnote-link' ,
'test_param' => '§content§' ,
'detector' => '/^[\^][0-9a-zA-Z\-_]+$/i' ,
'html' => function() {
return '<sup>[<a href="#'.CONFIG::$footnote_prefix.'!§id§" title="!§title§" id="'.CONFIG::$footnote_return_prefix.'!§id§">!§id§</a>]</sup>' ;
},
'params_order' => [ '!§id§' , '!§title§' ] ,
'params_defaults' => [
'!§title§' => '',
],
'params_filters' => [ '!§id§' => '/^[\^](.*)$/' ] ,
'on_parse_callback' => function( array $_params ) {
if ( $_params['!§title§'] != '' )
{
DOC::$footnote_index[ $_params['!§id§'] ] = $_params['!§title§'] ;
}
},
],
// [[:footnote-id / content ]]
[ 'type' => 'footnote-ref' ,
'test_param' => '§content§' ,
'detector' => '/^[:][0-9a-zA-Z\-_]+$/i' ,
'html' => '' ,
'params_order' => [ '!§id§' , '!§title§' ] ,
'params_defaults' => [
'!§title§' => '',
],
'params_filters' => [ '!§id§' => '/^[:](.*)$/' ] ,
'on_parse_callback' => function( array $_params ) {
if ( $_params['!§title§'] != '' )
{
DOC::$footnote_index[ $_params['!§id§'] ] = $_params['!§title§'] ;
}
},
],
],
],
// {{ abbreviation / definition }}
'{' => [ 'type'=>'abbreviation' , 'terminator' => '}' , 'html' => '<abbr title="!§title§">§content§</abbr>' , 'params_order' => [ '§content§' , '!§title§' ] ],
// (( anchor ))
'(' => [ 'type' => 'anchor' , 'terminator' => ')' , 'html' => '<a name="!§anchor§" id="!§anchor§" title="#!§anchor§">⚓</a>',
'params_order' => [ '!§anchor§' ],
],
// :: entity ::
':' => [ 'type' => 'entity', 'terminator' => ':' , 'html' => '&§code§;' , 'params_order' => [ '§code§' ] ],
];
$listing_tags =
[
/// type : is CSS code for 'list-style-type:'
/// detector : is regex used to detect the begining of a list item, and to extracts its value if available.
/// WARNING : /!\ only ASCII chr allowed as raw-text bullet
/// NOTE : \\FE0E forces the text-mode display of the preceding emoji
[ 'type' => 'disc' , 'detector' => '/^[ \t]*[*] /' ], // *
[ 'type' => 'circle' , 'detector' => '/^[ \t]*[@] /' ], // @
[ 'type' => 'square' , 'detector' => '/^[ \t]*[#] /' ], // #
[ 'type' => '\'- \'', 'detector' => '/^[ \t]*[-] /' ], // -
[ 'type' => 'disclosure-closed' , 'detector' => '/^[ \t]*[>] /' ], // >
[ 'type' => 'disclosure-open' , 'detector' => '/^[ \t]*[v] /i' ], // v V
[ 'type' => '\'▲\\FE0E \'' , 'detector' => '/^[ \t]*[\^] /' ], // ^
[ 'type' => '\'\\2192\\FE0E \'' , 'detector' => '/^[ \t]*[\-][>] /' ], // ->
[ 'type' => '\'\\21E8\\FE0E \'' , 'detector' => '/^[ \t]*[=][>] /' ], // =>
[ 'type' => '\'\\21B3\\FE0E \'' , 'detector' => '/^[ \t]*[L][>] /' ], // L>
[ 'type' => '\'§value§. \'' , 'detector' => '/^[ \t]*(([0-9]+[.]?)+)[.] /' ], // 1. 1.2. 1.2.3.
[ 'type' => '\'§value§) \'' , 'detector' => '/^[ \t]*(([0-9]+[.]?)+)[)] /' ], // 1) 1.2) 1.2.3)
[ 'type' => '\'§value§. \'' , 'detector' => '/^[ \t]*([a-z])[.] /i' ], // a. A.
[ 'type' => '\'§value§) \'' , 'detector' => '/^[ \t]*([a-z])[)] /i' ], // a) A)
[ 'type' => '\'(§value§) \'' , 'detector' => '/^[ \t]*[(]([a-z]+)[)] /i' ], // (a) (A)
[ 'type' => '\'☐\\FE0E \'', 'detector' => '/^[ \t]*\[_\] /' ], // [_]
[ 'type' => '\'☐\\FE0E \'', 'detector' => '/^[ \t]*\[\] /' ], // []
[ 'type' => '\'☐\\FE0E \'', 'detector' => '/^[ \t]*\[ \] /' ], // [ ]
[ 'type' => '\'☒\\FE0E \'', 'detector' => '/^[ \t]*\[x\] /i' ], // [x]
[ 'type' => '\'☑\\FE0E \'', 'detector' => '/^[ \t]*\[v\] /i' ], // [v]
[ 'type' => '\'⚐\\FE0E \'', 'detector' => '/^[ \t]*\[f\] /' ], // [f]
[ 'type' => '\'⚑\\FE0E \'', 'detector' => '/^[ \t]*\[F\] /' ], // [F]
//[ 'type' => '\'⚠\\FE0E \'', 'detector' => '/^[ \t]*\[\!\] /' ], // [!]
//[ 'type' => '\'ℹ\\FE0E \'', 'detector' => '/^[ \t]*\[i\] /' ], // [i]
[ 'type' => '\'⚠️ \'', 'detector' => '/^[ \t]*\[\!\] /' ], // [!]
[ 'type' => '\'ℹ️ \'', 'detector' => '/^[ \t]*\[i\] /' ], // [i]
];
/// Plugs a block variant.
/// _block_tag : tag of the default block ;
/// _variant : object following this format :
///
/// {
/// type : 'variant_name' ,
/// test_attribute : 'lang' ,
/// detector : /^variant_name[ ]*(.*)$/i ,
/// callback : 'your_callback' ,
///
/// tags : [ 'div' ] ,
/// attr : [ 'title' ] ,
/// formating : true ,
/// }
///
/// where :
/// - .type : is the unque name of the variant ;
/// - .test_attribute : is the name of the attribute of the default block that will be used to match the .detector ;
/// - .detector : is the regex used to detect the variant ;
/// - .callback : contains the name of the callback function that will update and return the content :
/// function your_callback( content , attributes ) { return updated_content; }
/// - .tags , .attr and .formating : see TMarqueur.block_tags ;
///
///
function plug_block_variant( string $_block_tag , array $_variant ) : bool
{
return _plug_variant( 'block_tags' , $_block_tag , $_variant );
}
function plug_brackets_variant( string $_brackets_tag , array $_variant ) : bool
{
return _plug_variant( 'brackets_tags' , $_brackets_tag , $_variant );
}
function _plug_variant( string $_marker_type, string $_marker_tag , array $_variant ) : bool
{
global $$_marker_type;
if ( ! isset( $$_marker_type[ $_marker_tag ] ) ) return false ;
$$_marker_type[ $_marker_tag ]['variants'] ??= [];
for( $i = 0 ; $i < count( $$_marker_type[ $_marker_tag ]['variants'] ) ; $i++ )
{
if ( $$_marker_type[ $_marker_tag ]['variants'][ $i ]['type'] == $_marker_tag )
{
$$_marker_type[ $_marker_tag ]['variants'][ $i ] = $_variant ;
return true ;
}
}
$$_marker_type[ $_marker_tag ]['variants'][] = $_variant ;
return true ;
}
function is_eol( string $_c ) : bool { return $_c == "\n" || $_c == "\r" ; }
function is_space( string $_c ) : bool { return $_c == " " || $_c == "\t" ; }
function count_at( string $_str , int $_pos ) : int
{
$len_ = strlen( $_str );
if ( $len_ <= 1 ) return $len_;
$c_ = $_str[ $_pos ];
$beg_ = $_pos ;
while( $_pos < $len_ && $_str[ $_pos ] == $c_ ) $_pos++;
return $_pos - $beg_ ;
}
function line_byte_length_at( string $_src , int $_pos ) : int
{
$beg_ = $_pos ;
$len_ = strlen( $_src );
while( $_pos < $len_ && ! is_eol( $_src[$_pos] ) ) $_pos++ ;
return $_pos - $beg_ ;
}
function list_depth( string $_line ) : int
{
$depth_ = 0 ;
$pos_ = 0 ;
$len_ = strlen( $_line );
while( $pos_ < $len_ && is_space( $_line[ $pos_ ] ) )
{
$depth_ += ( $_line[ $pos_ ] == "\t" ) ? CONFIG::$tab_size : 1 ;
$pos_++;
}
return $depth_ ;
}
function ltrim_tag( string $_str , string $_tag ) : string
{
$beg_ = 0 ;
$len_ = strlen( $_str );
while( $beg_ < $len_ && $_str[ $beg_ ] == $_tag ) $beg_++ ;
return substr( $_str , $beg_ );
}
function rtrim_tag( string $_str , string $_tag ) : string
{
$end_ = strlen( $_str ) - 1;
while( 0 <= $end_ && $_str[ $end_ ] == $_tag ) $end_-- ;
return substr( $_str ,0 , $end_ + 1 );
}
function trim_tag( string $_str , string $_tag ) : string
{
return ltrim_tag( rtrim_tag( $_str , $_tag ) , $_tag );
}
function escape_html_string( string $_val ) : string
{
return addslashes( $_val );
}
function open_tags( array $_tags , string $_attributes = null ) : string
{
$html_ = '';
for( $i = 0 ; $i < count( $_tags ) ; $i++ )
{
$html_ .= '<'.$_tags[ $i ] ;
if ( $_attributes !== null ) $html_ .= ' ' . $_attributes;
$html_ .= '>';
}
return $html_ ;
}
function close_tags( array $_tags ) : string
{
$html_ = '';
for( $i = count( $_tags ) - 1 ; $i >= 0 ; $i-- )
{
$html_ .= '</'.$_tags[ $i ].'>';
}
return $html_;
}
function tag_attributes_to_object( string $_tag , array $_variant , string $_line ) : array|null
{
if ( ! isset( $_variant['attr'] ) ) return null ;
$_line = trim( trim_tag( trim( $_line ) , $_tag ) );
$attributes_ = explode( CONFIG::$attribute_separator , $_line );
$obj_ = [];
$key_ = '';
$val_ = '';
$custom_attr_ = ! CONFIG::$disable_custom_attr && ( $_variant['custom_attr'] ?? false );
for( $i = 0 ; $i < count( $attributes_ ) ; $i++ )
{
if ( str_contains( $attributes_[ $i ] , CONFIG::$attribute_keyval_seperator ) )
{
$key_ = trim( explode( CONFIG::$attribute_keyval_seperator , $attributes_[ $i ] ) );
$val_ = trim( substr( $attributes_[ $i ] , strlen( key_ ) + strlen( CONFIG::$attribute_separator ) ) );
if ( ! $custom_attr_ )
{
if ( ! in_array( $key_ , $_variant['attr'] ) )
{
$key_ = "data-" . $key_ ;
}
}
}
else
if ( isset( $_variant['attr'][ $i ] ) )
{
$key_ = $_variant['attr'][ $i ];
$val_ = trim( $attributes_[ $i ] );
}
else
{
$key_ = "data-".$i;
$val_ = trim( $attributes_[ $i ] );
}
$obj_[ $key_ ] = $val_ ;
}
if ( isset( $_variant['attr_defaults'] ) )
{
for( $i = 0 ; $i < count( $_variant['attr'] ) ; $i++ )
{
$key_ = $_variant['attr'][ $i ];
if ( ( ! isset( $obj_[ $key_ ] ) || $obj_[ $key_ ] == '' ) && isset( $_variant['attr_defaults'][ $key_ ] ) )
{
$obj_[ $key_ ] = is_callable( $_variant['attr_defaults'][ $key_ ] ) ? $_variant['attr_defaults'][ $key_ ]( $obj_ ) : $_variant['attr_defaults'][ $key_ ] ;
}
}
}
return $obj_;
}
function attributes_object_to_html( array|null $_obj ) : string|null
{
if ( empty( $_obj ) ) return null ;
$res_ = '';
foreach( $_obj as $key_ => $val_ )
{
$res_ .= $key_ . '="' . escape_html_string( $val_ ) . '" ';
}
return $res_ ;
}
function content_to_html( string $_content , bool|string $_content_formating = true ) : string
{
if ( strcmp( $_content_formating , 'HTML' ) == 0 && ! CONFIG::$disable_html_blocks ) return $_content;
global $html_special_chars;
for( $i = 0 ; $i < count( $html_special_chars ) ; $i++ )
{
$key_ = $html_special_chars[ $i ][ 0 ];
$val_ = $html_special_chars[ $i ][ 1 ];
$_content = str_replace( $key_ , $val_ , $_content );
}
if ( $_content_formating == false ) return $_content ;
$stack_ = [];
$pos_ = 0 ;
$html_ = '';
$inner_formating_ = true ;
global $format_tags;
global $brackets_tags;
$content_len_ = strlen( $_content );
while ( $pos_ < $content_len_ )
{
$tag_ = $_content[ $pos_ ];
$len_ = 1 ;
$count_ = count_at( $_content , $pos_ );
if ( isset( $format_tags[ $tag_ ] ) && $count_ >= 2 )
{
if ( $count_ >= 2 && ( $inner_formating_ || $stack_[ count( $stack_ ) - 1 ]['tag'] == $tag_ ) )
{
if ( count( $stack_ ) == 0 || $stack_[ count( $stack_ ) - 1 ]['tag'] != $tag_ )
{
$stack_[] = [ 'tag' => $tag_ , 'pos' => $pos_ ];
$html_ .= open_tags( $format_tags[ $tag_ ]['tags'] );
$inner_formating_ = $format_tags[ $tag_ ]['formating'] ;
$tag_ = str_repeat( $tag_ , $count_ - 2 );
}
else
{
$html_ .= str_repeat( $tag_ , $count_ - 2 );
array_pop( $stack_ );
$html_ .= close_tags( $format_tags[ $tag_ ]['tags'] );
$tag_ = '';
$inner_formating_ = true ;
}
$len_ = $count_ ;
}
}
else
if ( $inner_formating_ === true && isset( $brackets_tags[ $tag_ ] ) && $count_ >= 2 )
{
//$count_ = count_at( $_content , $pos_ );
$terminator_ = $brackets_tags[ $tag_ ]['terminator'] ;
if ( $count_ >= 2 )
{
$end_ = $pos_ + $count_ ;
while
(
$end_ < $content_len_
&&
(
$_content[ $end_ ] != $terminator_
||
(
$_content[ $end_ ] == $terminator_
&&
count_at( $_content , $end_ ) != 2
)
)
) $end_++ ;
while( $end_ < $content_len_ && $_content[ $end_ ] == $terminator_ ) $end_++;
$params_ = substr( $_content , $pos_ , $end_ - $pos_ );
$len_ = strlen( $params_ );
$params_ = ltrim_tag( $params_ , $tag_ );
$params_ = rtrim_tag( $params_ , $brackets_tags[ $tag_ ]['terminator'] );
$params_ = trim( $params_ );
$params_ = explode( CONFIG::$attribute_separator , $params_ );
$variant = $brackets_tags[ $tag_ ] ;
$vals_ = [];
$out_ = '';
if ( strcmp( $_content_formating , 'summary' ) != 0 )
{
for( $i = 0 ; $i < count( $variant['params_order'] ) ; $i++ )
{
$key_ = $variant['params_order'][ $i ];
$val_ = '';
if ( isset( $params_[ $i ] ) )
{
$val_ = $params_[ $i ];
$val_ = preg_replace( '/[\r\n\s]+/', ' ' , $val_ );
$val_ = str_replace( "\\n", "\n" , $val_ );
$val_ = trim( $val_);
}
else
if ( isset( $variant['params_defaults'] ) && isset( $variant['params_defaults'][ $key_ ] ) )
{
$val_ = $variant['params_defaults'][ $key_ ] ;
}
$vals_[ $key_ ] = $val_ ;
}
if ( isset( $variant['variants'] ) )
{
for( $i = 0 ; $i < count( $variant['variants'] ) ; $i++ )
{
if ( regex_test( $variant['variants'][ $i ]['detector'] , $vals_[ $variant['variants'][ $i ]['test_param'] ] ) )
{
$variant = $variant['variants'][ $i ];
break;
}
}
for( $i = 0 ; $i < count( $variant['params_order'] ) ; $i++ )
{
$key_ = $variant['params_order'][ $i ];
$val_ = '';
if ( isset( $params_[ $i ] ) )
{
$val_ = $params_[ $i ];
$val_ = preg_replace( '/[\r\n\s]+/', ' ' , $val_ );
$val_ = str_replace( "\\n", "\n" , $val_ );
$val_ = trim( $val_);
}
else
if ( isset( $variant['params_defaults'] ) && isset( $variant['params_defaults'][ $key_ ] ) )
{
$val_ = $variant['params_defaults'][ $key_ ] ;
}
$vals_[ $key_ ] = $val_ ;
}
}
$out_ = $variant['html'];
if ( is_callable( $out_ ) ) $out_ = $out_();
for( $i = 0 ; $i < count( $variant['params_order'] ); $i++ )
{
$key_ = $variant['params_order'][ $i ];
$val_ = $vals_[ $key_ ];
while( is_callable( $val_ ) ) $val_ = $val_( $vals_ );
if ( isset( $variant['params_validator'] ) && isset( $variant['params_validator'][ $key_ ] ) )
{
if ( ! regex_test( $variant['params_validator'][ $key_ ] , $val_ ) )
{
$val_ = '';
}
}
if ( isset( $variant['params_filters'] ) && isset( $variant['params_filters'][ $key_ ] ) )
{
$val_ = regex_exec( $variant['params_filters'][ $key_ ] , $val_ )[1] ;
$vals_[ $key_ ] = $val_ ;
}
if ( $variant['params_order'][ $i ][ 0 ] == '!' )
{
$val_ = escape_html_string( $val_ );
}
$out_ = str_replace( $variant['params_order'][ $i ] , $val_ , $out_ );
}
if ( isset( $variant['on_parse_callback'] ) && is_callable( $variant['on_parse_callback'] ) )
{
$variant['on_parse_callback']( $vals_ );
}
}
$tag_ = $out_ ;
}
}
else
if ( $tag_ == "\\" && $inner_formating_ == true )
{
$next = $_content[ $pos_ + 1 ];
if ( $next != '' )
{
global $escape_seq ;
if ( isset( $escape_seq[ $next ] ) )
{
$tag_ = $escape_seq[ $next ];
$len_ = 2 ;
}
else
if ( isset( $format_tags[ $next ] ) )
{
$tag_ = '&#'.ord( $next ).';' ;
$len_ = 2 ;
}
}
}
else
if ( $inner_formating_ == true && $tag_ != ' ' )
{
global $text_to_html ;
for( $i = 0 ; $i < count( $text_to_html ) ; $i++ )
{
$key_ = $text_to_html[ $i ][0];
if ( $tag_ == $key_[ 0 ] && substr( $_content , $pos_ , strlen( $key_ ) ) == $key_ )
{
$tag_ = $text_to_html[ $i ][1];
$len_ = strlen( $key_ );
break;
}
}
}
$html_ .= $tag_ ;
$pos_ += $len_ ;
}
while( count( $stack_ ) )
{
$html_ .= close_tags( $format_tags[ array_pop( $stack_ )['tag'] ]['tags'] );
}
return $html_ ;
}
function regex_exec( string $pattern , string $subject ) : array
{
$out = [];
if ( false === preg_match( $pattern , $subject , $out ) )
{
echo "$pattern : $subject".PHP_EOL;
debug_print_backtrace();
exit();
}
return $out ;
}
function regex_test( string $pattern , string $subject ) : bool
{
$res = preg_match( $pattern , $subject );
if ( $res === false )
{
echo "$pattern : $subject".PHP_EOL;
debug_print_backtrace();
exit();
}
return $res === 1 ;
}
function is_listing_bullet( string $_line ) : bool|array
{
global $listing_tags;
for( $i = 0 ; $i < count( $listing_tags ) ; $i++ )
{
if ( regex_test( $listing_tags[ $i ]['detector'] , $_line ) )
{
return [
'type' => $listing_tags[ $i ]['type'] ,
'value' => regex_exec( $listing_tags[ $i ]['detector'] , $_line ) ,
'depth' => list_depth( $_line )
];
}
}
return false;
}
class DOC
{
static $summary_index = [];
static $footnote_index = []; //XXX associative array
static string $src = '';
static int $src_len = 0 ;
static string $html = '';
static string $content = '';
static string $line = '';
static int $pos = 0 ;
static $inside_title = false ;
static $inside_listing = false ;
static $inside_block = false ;
static $inside_block_variant = null ;
static $inside_block_attributes = null ;
static $inside_paragraph = false ;
static function flush_paragraph()
{
if ( false === DOC::$inside_paragraph || DOC::$content == '' ) return ;
DOC::$html .= '<p style="text-align:'.DOC::$inside_paragraph.'">' ;
DOC::$html .= content_to_html( DOC::$content ) ;
DOC::$html .= '</p>'.PHP_EOL.PHP_EOL ;
DOC::$content = '';
DOC::$inside_paragraph = false ;
}
static function flush_listing()
{
if ( false === DOC::$inside_listing ) return ;
DOC::parse_listing_line('');
}
static function flush_title()
{
if ( false === DOC::$inside_title ) return ;
DOC::parse_title_line('');
}
static function read_line() : bool
{
DOC::$line = substr( DOC::$src, DOC::$pos , line_byte_length_at( DOC::$src , DOC::$pos ) );
DOC::$pos += strlen( DOC::$line ) ;
if ( DOC::$pos < DOC::$src_len && DOC::$src[ DOC::$pos ] == "\r" ) DOC::$pos++;
if ( DOC::$pos < DOC::$src_len && DOC::$src[ DOC::$pos ] == "\n" ) DOC::$pos++;
return true ;
}
static function parse_listing_line( string $line ) : bool
{
$is_bullet = is_listing_bullet( $line ); // * ....
$is_empty_line = trim( $line ) == '' ;
$is_first_item = DOC::$inside_listing === false && $is_bullet !== false ; // <ul><li> ...
$is_multiline = DOC::$inside_listing !== false && $is_bullet === false && ! $is_empty_line ; // ...
$is_next_item = DOC::$inside_listing !== false && $is_bullet !== false ; // ...</li><li>...
$is_list_ending = DOC::$inside_listing !== false && $is_empty_line ; // ... </li></ul>
if ( $is_first_item )
{
DOC::flush_title() ;
DOC::flush_paragraph() ;
DOC::$html .= '<ul>'.PHP_EOL ;
DOC::$content = substr( $line , strlen( $is_bullet['value'][0] ) );
DOC::$inside_listing = $is_bullet ;
return true ;
}
if ( $is_multiline )
{
DOC::$content .= PHP_EOL . $line ;
return true ;
}
if ( $is_next_item )
{
$value = DOC::$inside_listing['value'][1] ?? '' ;
$type = str_replace( '§value§' , $value , DOC::$inside_listing['type'] ) ;
DOC::$html .= '<li';
DOC::$html .= ' value="'.$value.'"';
DOC::$html .= ' style="list-style-type:'.$type.'; margin-left:'.DOC::$inside_listing['depth'].'ch;"';
DOC::$html .= '><span>';
DOC::$html .= content_to_html( DOC::$content );
DOC::$html .= '</span></li>'.PHP_EOL;
DOC::$content = substr( $line , strlen( $is_bullet['value'][0] ) );
DOC::$inside_listing = $is_bullet ;
return true ;
}
if ( $is_list_ending )
{
$value = DOC::$inside_listing['value'][1] ?? '' ;
$type = str_replace( '§value§' , $value , DOC::$inside_listing['type'] ) ;
DOC::$html .= '<li';
DOC::$html .= ' value="'.$value.'"';
DOC::$html .= ' style="list-style-type:'.$type.'; margin-left:'.DOC::$inside_listing['depth'].'ch;"';
DOC::$html .= '><span>';
DOC::$html .= content_to_html( DOC::$content );
DOC::$html .= '</span></li>'.PHP_EOL;
DOC::$html .= '</ul>'.PHP_EOL.PHP_EOL;
DOC::$inside_listing = false ;
DOC::$content = '';
return true ;
}
return false ;
}
static function parse_title_line( $line ) : bool