xref: /dokuwiki/lib/plugins/syntax.php (revision 7fb56887dbc7ed17ea8cab3e1330622ebd83e5c6)
1<?php
2/**
3 * Syntax Plugin Prototype
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_INC.'inc/parser/parser.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode {
18
19    var $allowedModesSetup = false;
20
21    /**
22     * General Info
23     *
24     * Needs to return a associative array with the following values:
25     *
26     * author - Author of the plugin
27     * email  - Email address to contact the author
28     * date   - Last modified date of the plugin in YYYY-MM-DD format
29     * name   - Name of the plugin
30     * desc   - Short description of the plugin (Text only)
31     * url    - Website with more information on the plugin (eg. syntax description)
32     */
33    function getInfo(){
34        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
35    }
36
37    /**
38     * Syntax Type
39     *
40     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
41     */
42    function getType(){
43        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
44    }
45
46    /**
47     * Allowed Mode Types
48     *
49     * Defines the mode types for other dokuwiki markup that maybe nested within the
50     * plugin's own markup. Needs to return an array of one or more of the mode types
51     * defined in $PARSER_MODES in parser.php
52     */
53    function getAllowedTypes() {
54        return array();
55    }
56
57    /**
58     * Paragraph Type
59     *
60     * Defines how this syntax is handled regarding paragraphs. This is important
61     * for correct XHTML nesting. Should return one of the following:
62     *
63     * 'normal' - The plugin can be used inside paragraphs
64
65
66
67     * 'block'  - Open paragraphs need to be closed before plugin output
68     * 'stack'  - Special case. Plugin wraps other paragraphs.
69     *
70     * @see Doku_Handler_Block
71     */
72    function getPType(){
73        return 'normal';
74    }
75
76    /**
77     * Handler to prepare matched data for the rendering process
78     *
79     * This function can only pass data to render() via its return value - render()
80     * may be not be run during the object's current life.
81     *
82     * Usually you should only need the $match param.
83     *
84     * @param   $match   string    The text matched by the patterns
85     * @param   $state   int       The lexer state for the match
86     * @param   $pos     int       The character position of the matched text
87     * @param   $handler ref       Reference to the Doku_Handler object
88     * @return  array              Return an array with all data you want to use in render
89     */
90    function handle($match, $state, $pos, &$handler){
91        trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
92    }
93
94    /**
95     * Handles the actual output creation.
96     *
97     * The function must not assume any other of the classes methods have been run
98     * during the object's current life. The only reliable data it receives are its
99     * parameters.
100     *
101     * The function should always check for the given mode and return false
102     * when a mode isn't supported.
103     *
104     * $renderer contains a reference to the renderer object which is
105     * currently handling the rendering. You need to use it for writing
106     * the output. How this is done depends on the renderer used (specified
107     * by $mode
108     *
109     * The contents of the $data array depends on what the handler() function above
110     * created
111     *
112     * @param   $mode     string   current Rendermode
113     * @param   $renderer ref      reference to the current renderer object
114     * @param   $data     array    data created by handler()
115     * @return  boolean            rendered correctly?
116     */
117    function render($mode, &$renderer, $data) {
118        trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
119
120    }
121
122    /**
123     *  There should be no need to override this function
124     */
125    function accepts($mode) {
126
127        if (!$this->allowedModesSetup) {
128            global $PARSER_MODES;
129
130            $allowedModeTypes = $this->getAllowedTypes();
131            foreach($allowedModeTypes as $mt) {
132                $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
133            }
134
135            unset($this->allowedModes[array_search(substr(get_class($this), 7), $this->allowedModes)]);
136            $this->allowedModesSetup = true;
137        }
138
139        return parent::accepts($mode);
140    }
141
142}
143//Setup VIM: ex: et ts=4 enc=utf-8 :