aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Parsing/Handler/Nest.php
blob: f4e58e408e1eed215bec5478a4946cba928082a4 (plain) (blame)
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
<?php

namespace dokuwiki\Parsing\Handler;

/**
 * Generic call writer class to handle nesting of rendering instructions
 * within a render instruction. Also see nest() method of renderer base class
 *
 * @author    Chris Smith <chris@jalakai.co.uk>
 */
class Nest extends AbstractRewriter
{
    protected $closingInstruction;

    /**
     * @inheritdoc
     *
     * @param  CallWriterInterface $CallWriter     the parser's current call writer, i.e. the one above us in the chain
     * @param  string     $close          closing instruction name, this is required to properly terminate the
     *                                    syntax mode if the document ends without a closing pattern
     */
    public function __construct(CallWriterInterface $CallWriter, $close = "nest_close")
    {
        parent::__construct($CallWriter);
        $this->closingInstruction = $close;
    }

    /** @inheritdoc */
    public function writeCall($call)
    {
        $this->calls[] = $call;
    }

    /** @inheritdoc */
    public function writeCalls($calls)
    {
        $this->calls = array_merge($this->calls, $calls);
    }

    /** @inheritdoc */
    public function finalise()
    {
        $last_call = end($this->calls);
        $this->writeCall([$this->closingInstruction, [], $last_call[2]]);

        $this->process();
        $this->callWriter->finalise();
        unset($this->callWriter);
    }

    /** @inheritdoc */
    public function process()
    {
        // merge consecutive cdata
        $unmerged_calls = $this->calls;
        $this->calls = [];

        foreach ($unmerged_calls as $call) $this->addCall($call);

        $first_call = reset($this->calls);
        $this->callWriter->writeCall(["nest", [$this->calls], $first_call[2]]);

        return $this->callWriter;
    }

    /**
     * @param array $call
     */
    protected function addCall($call)
    {
        $key = count($this->calls);
        if ($key && $call[0] == 'cdata' && $this->calls[$key - 1][0] == 'cdata') {
            $this->calls[$key - 1][1][0] .= $call[1][0];
        } elseif ($call[0] == 'eol') {
            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
        } else {
            $this->calls[] = $call;
        }
    }
}