1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5/**
6 * This class sets the markup for bold (=strong),
7 * italic (=emphasis), underline etc.
8 */
9class Formatting extends AbstractMode
10{
11    protected $type;
12
13    protected $formatting = [
14        'strong' => [
15            'entry' => '\*\*(?=.*\*\*)',
16            'exit' => '\*\*',
17            'sort' => 70
18        ],
19        'emphasis' => [
20            'entry' => '//(?=[^\x00]*[^:])',
21            //hack for bugs #384 #763 #1468
22            'exit' => '//',
23            'sort' => 80,
24        ],
25        'underline' => [
26            'entry' => '__(?=.*__)',
27            'exit' => '__',
28            'sort' => 90
29        ],
30        'monospace' => [
31            'entry' => '\x27\x27(?=.*\x27\x27)',
32            'exit' => '\x27\x27',
33            'sort' => 100
34        ],
35        'subscript' => [
36            'entry' => '<sub>(?=.*</sub>)',
37            'exit' => '</sub>',
38            'sort' => 110
39        ],
40        'superscript' => [
41            'entry' => '<sup>(?=.*</sup>)',
42            'exit' => '</sup>',
43            'sort' => 120
44        ],
45        'deleted' => [
46            'entry' => '<del>(?=.*</del>)',
47            'exit' => '</del>',
48            'sort' => 130
49        ]
50    ];
51
52    /**
53     * @param string $type
54     */
55    public function __construct($type)
56    {
57        global $PARSER_MODES;
58
59        if (!array_key_exists($type, $this->formatting)) {
60            trigger_error('Invalid formatting type ' . $type, E_USER_WARNING);
61        }
62
63        $this->type = $type;
64
65        // formatting may contain other formatting but not it self
66        $modes = $PARSER_MODES['formatting'];
67        $key = array_search($type, $modes);
68        if (is_int($key)) {
69            unset($modes[$key]);
70        }
71
72        $this->allowedModes = array_merge(
73            $modes,
74            $PARSER_MODES['substition'],
75            $PARSER_MODES['disabled']
76        );
77    }
78
79    /** @inheritdoc */
80    public function connectTo($mode)
81    {
82
83        // Can't nest formatting in itself
84        if ($mode == $this->type) {
85            return;
86        }
87
88        $this->Lexer->addEntryPattern(
89            $this->formatting[$this->type]['entry'],
90            $mode,
91            $this->type
92        );
93    }
94
95    /** @inheritdoc */
96    public function postConnect()
97    {
98
99        $this->Lexer->addExitPattern(
100            $this->formatting[$this->type]['exit'],
101            $this->type
102        );
103    }
104
105    /** @inheritdoc */
106    public function getSort()
107    {
108        return $this->formatting[$this->type]['sort'];
109    }
110}
111