summaryrefslogtreecommitdiff
path: root/platform/www/inc/Parsing/ParserMode/Formatting.php
blob: a3c465cc008d7aa649f7a2e99d47e26b2ecda83c (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
<?php

namespace dokuwiki\Parsing\ParserMode;

/**
 * This class sets the markup for bold (=strong),
 * italic (=emphasis), underline etc.
 */
class Formatting extends AbstractMode
{
    protected $type;

    protected $formatting = array(
        'strong' => array(
            'entry' => '\*\*(?=.*\*\*)',
            'exit' => '\*\*',
            'sort' => 70
        ),

        'emphasis' => array(
            'entry' => '//(?=[^\x00]*[^:])', //hack for bugs #384 #763 #1468
            'exit' => '//',
            'sort' => 80
        ),

        'underline' => array(
            'entry' => '__(?=.*__)',
            'exit' => '__',
            'sort' => 90
        ),

        'monospace' => array(
            'entry' => '\x27\x27(?=.*\x27\x27)',
            'exit' => '\x27\x27',
            'sort' => 100
        ),

        'subscript' => array(
            'entry' => '<sub>(?=.*</sub>)',
            'exit' => '</sub>',
            'sort' => 110
        ),

        'superscript' => array(
            'entry' => '<sup>(?=.*</sup>)',
            'exit' => '</sup>',
            'sort' => 120
        ),

        'deleted' => array(
            'entry' => '<del>(?=.*</del>)',
            'exit' => '</del>',
            'sort' => 130
        ),
    );

    /**
     * @param string $type
     */
    public function __construct($type)
    {
        global $PARSER_MODES;

        if (!array_key_exists($type, $this->formatting)) {
            trigger_error('Invalid formatting type ' . $type, E_USER_WARNING);
        }

        $this->type = $type;

        // formatting may contain other formatting but not it self
        $modes = $PARSER_MODES['formatting'];
        $key = array_search($type, $modes);
        if (is_int($key)) {
            unset($modes[$key]);
        }

        $this->allowedModes = array_merge(
            $modes,
            $PARSER_MODES['substition'],
            $PARSER_MODES['disabled']
        );
    }

    /** @inheritdoc */
    public function connectTo($mode)
    {

        // Can't nest formatting in itself
        if ($mode == $this->type) {
            return;
        }

        $this->Lexer->addEntryPattern(
            $this->formatting[$this->type]['entry'],
            $mode,
            $this->type
        );
    }

    /** @inheritdoc */
    public function postConnect()
    {

        $this->Lexer->addExitPattern(
            $this->formatting[$this->type]['exit'],
            $this->type
        );
    }

    /** @inheritdoc */
    public function getSort()
    {
        return $this->formatting[$this->type]['sort'];
    }
}