summaryrefslogtreecommitdiff
path: root/platform/www/inc/parser/handler.php
blob: a3609609cf35db8b6c6c29e2955b6e591475ee4e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
<?php

use dokuwiki\Extension\Event;
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\Parsing\Handler\Block;
use dokuwiki\Parsing\Handler\CallWriter;
use dokuwiki\Parsing\Handler\CallWriterInterface;
use dokuwiki\Parsing\Handler\Lists;
use dokuwiki\Parsing\Handler\Nest;
use dokuwiki\Parsing\Handler\Preformatted;
use dokuwiki\Parsing\Handler\Quote;
use dokuwiki\Parsing\Handler\Table;

/**
 * Class Doku_Handler
 */
class Doku_Handler {
    /** @var CallWriterInterface */
    protected $callWriter = null;

    /** @var array The current CallWriter will write directly to this list of calls, Parser reads it */
    public $calls = array();

    /** @var array internal status holders for some modes */
    protected $status = array(
        'section' => false,
        'doublequote' => 0,
    );

    /** @var bool should blocks be rewritten? FIXME seems to always be true */
    protected $rewriteBlocks = true;

    /**
     * Doku_Handler constructor.
     */
    public function __construct() {
        $this->callWriter = new CallWriter($this);
    }

    /**
     * Add a new call by passing it to the current CallWriter
     *
     * @param string $handler handler method name (see mode handlers below)
     * @param mixed $args arguments for this call
     * @param int $pos  byte position in the original source file
     */
    public function addCall($handler, $args, $pos) {
        $call = array($handler,$args, $pos);
        $this->callWriter->writeCall($call);
    }

    /**
     * Accessor for the current CallWriter
     *
     * @return CallWriterInterface
     */
    public function getCallWriter() {
        return $this->callWriter;
    }

    /**
     * Set a new CallWriter
     *
     * @param CallWriterInterface $callWriter
     */
    public function setCallWriter($callWriter) {
        $this->callWriter = $callWriter;
    }

    /**
     * Return the current internal status of the given name
     *
     * @param string $status
     * @return mixed|null
     */
    public function getStatus($status) {
        if (!isset($this->status[$status])) return null;
        return $this->status[$status];
    }

    /**
     * Set a new internal status
     *
     * @param string $status
     * @param mixed $value
     */
    public function setStatus($status, $value) {
        $this->status[$status] = $value;
    }

    /** @deprecated 2019-10-31 use addCall() instead */
    public function _addCall($handler, $args, $pos) {
        dbg_deprecated('addCall');
        $this->addCall($handler, $args, $pos);
    }

    /**
     * Similar to addCall, but adds a plugin call
     *
     * @param string $plugin name of the plugin
     * @param mixed $args arguments for this call
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @param string $match matched syntax
     */
    public function addPluginCall($plugin, $args, $state, $pos, $match) {
        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
        $this->callWriter->writeCall($call);
    }

    /**
     * Finishes handling
     *
     * Called from the parser. Calls finalise() on the call writer, closes open
     * sections, rewrites blocks and adds document_start and document_end calls.
     *
     * @triggers PARSER_HANDLER_DONE
     */
    public function finalize(){
        $this->callWriter->finalise();

        if ( $this->status['section'] ) {
            $last_call = end($this->calls);
            array_push($this->calls,array('section_close',array(), $last_call[2]));
        }

        if ( $this->rewriteBlocks ) {
            $B = new Block();
            $this->calls = $B->process($this->calls);
        }

        Event::createAndTrigger('PARSER_HANDLER_DONE',$this);

        array_unshift($this->calls,array('document_start',array(),0));
        $last_call = end($this->calls);
        array_push($this->calls,array('document_end',array(),$last_call[2]));
    }

    /**
     * fetch the current call and advance the pointer to the next one
     *
     * @fixme seems to be unused?
     * @return bool|mixed
     */
    public function fetch() {
        $call = current($this->calls);
        if($call !== false) {
            next($this->calls); //advance the pointer
            return $call;
        }
        return false;
    }


    /**
     * Internal function for parsing highlight options.
     * $options is parsed for key value pairs separated by commas.
     * A value might also be missing in which case the value will simple
     * be set to true. Commas in strings are ignored, e.g. option="4,56"
     * will work as expected and will only create one entry.
     *
     * @param string $options space separated list of key-value pairs,
     *                        e.g. option1=123, option2="456"
     * @return array|null     Array of key-value pairs $array['key'] = 'value';
     *                        or null if no entries found
     */
    protected function parse_highlight_options($options) {
        $result = array();
        preg_match_all('/(\w+(?:="[^"]*"))|(\w+(?:=[^\s]*))|(\w+[^=\s\]])(?:\s*)/', $options, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            $equal_sign = strpos($match [0], '=');
            if ($equal_sign === false) {
                $key = trim($match[0]);
                $result [$key] = 1;
            } else {
                $key = substr($match[0], 0, $equal_sign);
                $value = substr($match[0], $equal_sign+1);
                $value = trim($value, '"');
                if (strlen($value) > 0) {
                    $result [$key] = $value;
                } else {
                    $result [$key] = 1;
                }
            }
        }

        // Check for supported options
        $result = array_intersect_key(
            $result,
            array_flip(array(
                           'enable_line_numbers',
                           'start_line_numbers_at',
                           'highlight_lines_extra',
                           'enable_keyword_links')
            )
        );

        // Sanitize values
        if(isset($result['enable_line_numbers'])) {
            if($result['enable_line_numbers'] === 'false') {
                $result['enable_line_numbers'] = false;
            }
            $result['enable_line_numbers'] = (bool) $result['enable_line_numbers'];
        }
        if(isset($result['highlight_lines_extra'])) {
            $result['highlight_lines_extra'] = array_map('intval', explode(',', $result['highlight_lines_extra']));
            $result['highlight_lines_extra'] = array_filter($result['highlight_lines_extra']);
            $result['highlight_lines_extra'] = array_unique($result['highlight_lines_extra']);
        }
        if(isset($result['start_line_numbers_at'])) {
            $result['start_line_numbers_at'] = (int) $result['start_line_numbers_at'];
        }
        if(isset($result['enable_keyword_links'])) {
            if($result['enable_keyword_links'] === 'false') {
                $result['enable_keyword_links'] = false;
            }
            $result['enable_keyword_links'] = (bool) $result['enable_keyword_links'];
        }
        if (count($result) == 0) {
            return null;
        }

        return $result;
    }

    /**
     * Simplifies handling for the formatting tags which all behave the same
     *
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @param string $name actual mode name
     */
    protected function nestingTag($match, $state, $pos, $name) {
        switch ( $state ) {
            case DOKU_LEXER_ENTER:
                $this->addCall($name.'_open', array(), $pos);
                break;
            case DOKU_LEXER_EXIT:
                $this->addCall($name.'_close', array(), $pos);
                break;
            case DOKU_LEXER_UNMATCHED:
                $this->addCall('cdata', array($match), $pos);
                break;
        }
    }


    /**
     * The following methods define the handlers for the different Syntax modes
     *
     * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser()
     *
     * @todo it might make sense to move these into their own class or merge them with the
     *       ParserMode classes some time.
     */
    // region mode handlers

    /**
     * Special plugin handler
     *
     * This handler is called for all modes starting with 'plugin_'.
     * An additional parameter with the plugin name is passed. The plugin's handle()
     * method is called here
     *
     * @author Andreas Gohr <andi@splitbrain.org>
     *
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @param string $pluginname name of the plugin
     * @return bool mode handled?
     */
    public function plugin($match, $state, $pos, $pluginname){
        $data = array($match);
        /** @var SyntaxPlugin $plugin */
        $plugin = plugin_load('syntax',$pluginname);
        if($plugin != null){
            $data = $plugin->handle($match, $state, $pos, $this);
        }
        if ($data !== false) {
            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function base($match, $state, $pos) {
        switch ( $state ) {
            case DOKU_LEXER_UNMATCHED:
                $this->addCall('cdata', array($match), $pos);
                return true;
            break;
        }
        return false;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function header($match, $state, $pos) {
        // get level and title
        $title = trim($match);
        $level = 7 - strspn($title,'=');
        if($level < 1) $level = 1;
        $title = trim($title,'=');
        $title = trim($title);

        if ($this->status['section']) $this->addCall('section_close', array(), $pos);

        $this->addCall('header', array($title, $level, $pos), $pos);

        $this->addCall('section_open', array($level), $pos);
        $this->status['section'] = true;
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function notoc($match, $state, $pos) {
        $this->addCall('notoc', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function nocache($match, $state, $pos) {
        $this->addCall('nocache', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function linebreak($match, $state, $pos) {
        $this->addCall('linebreak', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function eol($match, $state, $pos) {
        $this->addCall('eol', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function hr($match, $state, $pos) {
        $this->addCall('hr', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function strong($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'strong');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function emphasis($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'emphasis');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function underline($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'underline');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function monospace($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'monospace');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function subscript($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'subscript');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function superscript($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'superscript');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function deleted($match, $state, $pos) {
        $this->nestingTag($match, $state, $pos, 'deleted');
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function footnote($match, $state, $pos) {
        if (!isset($this->_footnote)) $this->_footnote = false;

        switch ( $state ) {
            case DOKU_LEXER_ENTER:
                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
                // we will still enter a new footnote mode, we just do nothing
                if ($this->_footnote) {
                    $this->addCall('cdata', array($match), $pos);
                    break;
                }
                $this->_footnote = true;

                $this->callWriter = new Nest($this->callWriter, 'footnote_close');
                $this->addCall('footnote_open', array(), $pos);
            break;
            case DOKU_LEXER_EXIT:
                // check whether we have already exitted the footnote mode, can happen if the modes were nested
                if (!$this->_footnote) {
                    $this->addCall('cdata', array($match), $pos);
                    break;
                }

                $this->_footnote = false;
                $this->addCall('footnote_close', array(), $pos);

                /** @var Nest $reWriter */
                $reWriter = $this->callWriter;
                $this->callWriter = $reWriter->process();
            break;
            case DOKU_LEXER_UNMATCHED:
                $this->addCall('cdata', array($match), $pos);
            break;
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function listblock($match, $state, $pos) {
        switch ( $state ) {
            case DOKU_LEXER_ENTER:
                $this->callWriter = new Lists($this->callWriter);
                $this->addCall('list_open', array($match), $pos);
            break;
            case DOKU_LEXER_EXIT:
                $this->addCall('list_close', array(), $pos);
                /** @var Lists $reWriter */
                $reWriter = $this->callWriter;
                $this->callWriter = $reWriter->process();
            break;
            case DOKU_LEXER_MATCHED:
                $this->addCall('list_item', array($match), $pos);
            break;
            case DOKU_LEXER_UNMATCHED:
                $this->addCall('cdata', array($match), $pos);
            break;
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function unformatted($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $this->addCall('unformatted', array($match), $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function php($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $this->addCall('php', array($match), $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function phpblock($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $this->addCall('phpblock', array($match), $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function html($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $this->addCall('html', array($match), $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function htmlblock($match, $state, $pos) {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $this->addCall('htmlblock', array($match), $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function preformatted($match, $state, $pos) {
        switch ( $state ) {
            case DOKU_LEXER_ENTER:
                $this->callWriter = new Preformatted($this->callWriter);
                $this->addCall('preformatted_start', array(), $pos);
            break;
            case DOKU_LEXER_EXIT:
                $this->addCall('preformatted_end', array(), $pos);
                /** @var Preformatted $reWriter */
                $reWriter = $this->callWriter;
                $this->callWriter = $reWriter->process();
            break;
            case DOKU_LEXER_MATCHED:
                $this->addCall('preformatted_newline', array(), $pos);
            break;
            case DOKU_LEXER_UNMATCHED:
                $this->addCall('preformatted_content', array($match), $pos);
            break;
        }

        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function quote($match, $state, $pos) {

        switch ( $state ) {

            case DOKU_LEXER_ENTER:
                $this->callWriter = new Quote($this->callWriter);
                $this->addCall('quote_start', array($match), $pos);
            break;

            case DOKU_LEXER_EXIT:
                $this->addCall('quote_end', array(), $pos);
                /** @var Lists $reWriter */
                $reWriter = $this->callWriter;
                $this->callWriter = $reWriter->process();
            break;

            case DOKU_LEXER_MATCHED:
                $this->addCall('quote_newline', array($match), $pos);
            break;

            case DOKU_LEXER_UNMATCHED:
                $this->addCall('cdata', array($match), $pos);
            break;

        }

        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function file($match, $state, $pos) {
        return $this->code($match, $state, $pos, 'file');
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @param string $type either 'code' or 'file'
     * @return bool mode handled?
     */
    public function code($match, $state, $pos, $type='code') {
        if ( $state == DOKU_LEXER_UNMATCHED ) {
            $matches = explode('>',$match,2);
            // Cut out variable options enclosed in []
            preg_match('/\[.*\]/', $matches[0], $options);
            if (!empty($options[0])) {
                $matches[0] = str_replace($options[0], '', $matches[0]);
            }
            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
            while(count($param) < 2) array_push($param, null);
            // We shortcut html here.
            if ($param[0] == 'html') $param[0] = 'html4strict';
            if ($param[0] == '-') $param[0] = null;
            array_unshift($param, $matches[1]);
            if (!empty($options[0])) {
                $param [] = $this->parse_highlight_options ($options[0]);
            }
            $this->addCall($type, $param, $pos);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function acronym($match, $state, $pos) {
        $this->addCall('acronym', array($match), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function smiley($match, $state, $pos) {
        $this->addCall('smiley', array($match), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function wordblock($match, $state, $pos) {
        $this->addCall('wordblock', array($match), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function entity($match, $state, $pos) {
        $this->addCall('entity', array($match), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function multiplyentity($match, $state, $pos) {
        preg_match_all('/\d+/',$match,$matches);
        $this->addCall('multiplyentity', array($matches[0][0], $matches[0][1]), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function singlequoteopening($match, $state, $pos) {
        $this->addCall('singlequoteopening', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function singlequoteclosing($match, $state, $pos) {
        $this->addCall('singlequoteclosing', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function apostrophe($match, $state, $pos) {
        $this->addCall('apostrophe', array(), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function doublequoteopening($match, $state, $pos) {
        $this->addCall('doublequoteopening', array(), $pos);
        $this->status['doublequote']++;
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function doublequoteclosing($match, $state, $pos) {
        if ($this->status['doublequote'] <= 0) {
            $this->doublequoteopening($match, $state, $pos);
        } else {
            $this->addCall('doublequoteclosing', array(), $pos);
            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
        }
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function camelcaselink($match, $state, $pos) {
        $this->addCall('camelcaselink', array($match), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function internallink($match, $state, $pos) {
        // Strip the opening and closing markup
        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);

        // Split title from URL
        $link = explode('|',$link,2);
        if ( !isset($link[1]) ) {
            $link[1] = null;
        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
            // If the title is an image, convert it to an array containing the image details
            $link[1] = Doku_Handler_Parse_Media($link[1]);
        }
        $link[0] = trim($link[0]);

        //decide which kind of link it is

        if ( link_isinterwiki($link[0]) ) {
            // Interwiki
            $interwiki = explode('>',$link[0],2);
            $this->addCall(
                'interwikilink',
                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
                $pos
                );
        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
            // Windows Share
            $this->addCall(
                'windowssharelink',
                array($link[0],$link[1]),
                $pos
                );
        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
            // external link (accepts all protocols)
            $this->addCall(
                    'externallink',
                    array($link[0],$link[1]),
                    $pos
                    );
        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
            // E-Mail (pattern above is defined in inc/mail.php)
            $this->addCall(
                'emaillink',
                array($link[0],$link[1]),
                $pos
                );
        }elseif ( preg_match('!^#.+!',$link[0]) ){
            // local link
            $this->addCall(
                'locallink',
                array(substr($link[0],1),$link[1]),
                $pos
                );
        }else{
            // internal link
            $this->addCall(
                'internallink',
                array($link[0],$link[1]),
                $pos
                );
        }

        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function filelink($match, $state, $pos) {
        $this->addCall('filelink', array($match, null), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function windowssharelink($match, $state, $pos) {
        $this->addCall('windowssharelink', array($match, null), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function media($match, $state, $pos) {
        $p = Doku_Handler_Parse_Media($match);

        $this->addCall(
              $p['type'],
              array($p['src'], $p['title'], $p['align'], $p['width'],
                     $p['height'], $p['cache'], $p['linking']),
              $pos
             );
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function rss($match, $state, $pos) {
        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);

        // get params
        list($link,$params) = explode(' ',$link,2);

        $p = array();
        if(preg_match('/\b(\d+)\b/',$params,$match)){
            $p['max'] = $match[1];
        }else{
            $p['max'] = 8;
        }
        $p['reverse'] = (preg_match('/rev/',$params));
        $p['author']  = (preg_match('/\b(by|author)/',$params));
        $p['date']    = (preg_match('/\b(date)/',$params));
        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
        $p['nosort']  = (preg_match('/\b(nosort)\b/',$params));

        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
        } else {
            $p['refresh'] = 14400;   // default to 4 hours
        }

        $this->addCall('rss', array($link, $p), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function externallink($match, $state, $pos) {
        $url   = $match;
        $title = null;

        // add protocol on simple short URLs
        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
            $title = $url;
            $url   = 'ftp://'.$url;
        }
        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
            $title = $url;
            $url = 'http://'.$url;
        }

        $this->addCall('externallink', array($url, $title), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function emaillink($match, $state, $pos) {
        $email = preg_replace(array('/^</','/>$/'),'',$match);
        $this->addCall('emaillink', array($email, null), $pos);
        return true;
    }

    /**
     * @param string $match matched syntax
     * @param int $state a LEXER_STATE_* constant
     * @param int $pos byte position in the original source file
     * @return bool mode handled?
     */
    public function table($match, $state, $pos) {
        switch ( $state ) {

            case DOKU_LEXER_ENTER:

                $this->callWriter = new Table($this->callWriter);

                $this->addCall('table_start', array($pos + 1), $pos);
                if ( trim($match) == '^' ) {
                    $this->addCall('tableheader', array(), $pos);
                } else {
                    $this->addCall('tablecell', array(), $pos);
                }
            break;

            case DOKU_LEXER_EXIT:
                $this->addCall('table_end', array($pos), $pos);
                /** @var Table $reWriter */
                $reWriter = $this->callWriter;
                $this->callWriter = $reWriter->process();
            break;

            case DOKU_LEXER_UNMATCHED:
                if ( trim($match) != '' ) {
                    $this->addCall('cdata', array($match), $pos);
                }
            break;

            case DOKU_LEXER_MATCHED:
                if ( $match == ' ' ){
                    $this->addCall('cdata', array($match), $pos);
                } else if ( preg_match('/:::/',$match) ) {
                    $this->addCall('rowspan', array($match), $pos);
                } else if ( preg_match('/\t+/',$match) ) {
                    $this->addCall('table_align', array($match), $pos);
                } else if ( preg_match('/ {2,}/',$match) ) {
                    $this->addCall('table_align', array($match), $pos);
                } else if ( $match == "\n|" ) {
                    $this->addCall('table_row', array(), $pos);
                    $this->addCall('tablecell', array(), $pos);
                } else if ( $match == "\n^" ) {
                    $this->addCall('table_row', array(), $pos);
                    $this->addCall('tableheader', array(), $pos);
                } else if ( $match == '|' ) {
                    $this->addCall('tablecell', array(), $pos);
                } else if ( $match == '^' ) {
                    $this->addCall('tableheader', array(), $pos);
                }
            break;
        }
        return true;
    }

    // endregion modes
}

//------------------------------------------------------------------------
function Doku_Handler_Parse_Media($match) {

    // Strip the opening and closing markup
    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);

    // Split title from URL
    $link = explode('|',$link,2);

    // Check alignment
    $ralign = (bool)preg_match('/^ /',$link[0]);
    $lalign = (bool)preg_match('/ $/',$link[0]);

    // Logic = what's that ;)...
    if ( $lalign & $ralign ) {
        $align = 'center';
    } else if ( $ralign ) {
        $align = 'right';
    } else if ( $lalign ) {
        $align = 'left';
    } else {
        $align = null;
    }

    // The title...
    if ( !isset($link[1]) ) {
        $link[1] = null;
    }

    //remove aligning spaces
    $link[0] = trim($link[0]);

    //split into src and parameters (using the very last questionmark)
    $pos = strrpos($link[0], '?');
    if($pos !== false){
        $src   = substr($link[0],0,$pos);
        $param = substr($link[0],$pos+1);
    }else{
        $src   = $link[0];
        $param = '';
    }

    //parse width and height
    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
        !empty($size[1]) ? $w = $size[1] : $w = null;
        !empty($size[3]) ? $h = $size[3] : $h = null;
    } else {
        $w = null;
        $h = null;
    }

    //get linking command
    if(preg_match('/nolink/i',$param)){
        $linking = 'nolink';
    }else if(preg_match('/direct/i',$param)){
        $linking = 'direct';
    }else if(preg_match('/linkonly/i',$param)){
        $linking = 'linkonly';
    }else{
        $linking = 'details';
    }

    //get caching command
    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
        $cache = $cachemode[1];
    }else{
        $cache = 'cache';
    }

    // Check whether this is a local or remote image or interwiki
    if (media_isexternal($src) || link_isinterwiki($src)){
        $call = 'externalmedia';
    } else {
        $call = 'internalmedia';
    }

    $params = array(
        'type'=>$call,
        'src'=>$src,
        'title'=>$link[1],
        'align'=>$align,
        'width'=>$w,
        'height'=>$h,
        'cache'=>$cache,
        'linking'=>$linking,
    );

    return $params;
}