xref: /plugin/discussion/syntax/comments.php (revision e7ac9ada4998117ca2a6894a8446c270f3b90808)
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// must be run within Dokuwiki
13if(!defined('DOKU_INC')) die();
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_discussion_comments extends DokuWiki_Syntax_Plugin {
20
21    /**
22     * Syntax Type
23     *
24     * @return string
25     */
26    function getType() { return 'substition'; }
27
28    /**
29     * Paragraph Type
30     *
31     * @return string
32     */
33    function getPType() { return 'block'; }
34
35    /**
36     * Sort for applying this mode
37     *
38     * @return int
39     */
40    function getSort() { return 230; }
41
42    /**
43     * Connect pattern to lexer
44     */
45    function connectTo($mode) {
46        if ($mode == 'base') {
47            $this->Lexer->addSpecialPattern('~~DISCUSSION[^\r\n]*?~~', $mode, 'plugin_discussion_comments');
48        }
49    }
50
51    /**
52     * Handler to prepare matched data for the rendering process
53     *
54     * @param   string       $match   The text matched by the patterns
55     * @param   int          $state   The lexer state for the match
56     * @param   int          $pos     The character position of the matched text
57     * @param   Doku_Handler $handler The Doku_Handler object
58     * @return  array Return an array with all data you want to use in render
59     */
60    function handle($match, $state, $pos, &$handler) {
61
62        // strip markup
63        $match = substr($match, 12, -2);
64
65        // split title (if there is one)
66        list($match, $title) = explode('|', $match, 2);
67
68        // assign discussion state
69        if ($match == ':off') $status = 0;
70        else if ($match == ':closed') $status = 2;
71        else $status = 1;
72
73        return array($status, $title);
74    }
75
76    /**
77     * Handles the actual output creation.
78     *
79     * @param   $mode   string        output format being rendered
80     * @param   $renderer Doku_Renderer the current renderer object
81     * @param   $data     array         data created by handler()
82     * @return  boolean                 rendered correctly?
83     */
84    function render($mode, Doku_Renderer $renderer, $data) {
85        list($status, $title) = $data;
86        if ($mode == 'metadata') {
87            /** @var $renderer Doku_Renderer_metadata */
88            $renderer->meta['plugin_discussion'] = array('status' => $status, 'title' => $title);
89        }
90        return true;
91    }
92}
93// vim:ts=4:sw=4:et:enc=utf-8:
94