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