1<?php
2/**
3 * Discussion Plugin
4 *
5 * Enables/disables discussion features based on config settings.
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Esther Brunner <wikidesign@gmail.com>
9 * @author  Dave Lawson <dlawson@masterytech.com>
10 */
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_discussion_comments extends DokuWiki_Syntax_Plugin
17{
18
19    /**
20     * Syntax Type
21     *
22     * @return string
23     */
24    public function getType()
25    {
26        return 'substition';
27    }
28
29    /**
30     * Paragraph Type
31     *
32     * @return string
33     */
34    public function getPType()
35    {
36        return 'block';
37    }
38
39    /**
40     * Sort for applying this mode
41     *
42     * @return int
43     */
44    public function getSort()
45    {
46        return 230;
47    }
48
49    /**
50     * Connect pattern to lexer
51     */
52    public function connectTo($mode)
53    {
54        if ($mode == 'base') {
55            $this->Lexer->addSpecialPattern('~~DISCUSSION[^\r\n]*?~~', $mode, 'plugin_discussion_comments');
56        }
57    }
58
59    /**
60     * Handler to prepare matched data for the rendering process
61     *
62     * @param string $match The text matched by the patterns
63     * @param int $state The lexer state for the match
64     * @param int $pos The character position of the matched text
65     * @param Doku_Handler $handler The Doku_Handler object
66     * @return  array Return an array with all data you want to use in render
67     */
68    public function handle($match, $state, $pos, Doku_Handler $handler)
69    {
70        // strip markup
71        $match = substr($match, 12, -2);
72
73        // split title (if there is one)
74        list($match, $title) = array_pad(explode('|', $match, 2), 2, '');
75
76        // assign discussion state
77        if ($match == ':off') {
78            $status = 0;
79        } elseif ($match == ':closed') {
80            $status = 2;
81        } else {
82            // comments enabled
83            $status = 1;
84        }
85
86        return [$status, $title];
87    }
88
89    /**
90     * Handles the actual output creation.
91     *
92     * @param string $format output format being rendered
93     * @param Doku_Renderer $renderer the current renderer object
94     * @param array $data data created by handler()
95     * @return boolean rendered correctly?
96     */
97    public function render($format, Doku_Renderer $renderer, $data)
98    {
99        list($status, $title) = $data;
100        if ($format == 'metadata') {
101            /** @var Doku_Renderer_metadata $renderer */
102            $renderer->meta['plugin_discussion'] = ['status' => $status, 'title' => $title];
103        }
104        return true;
105    }
106}
107