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