xref: /dokuwiki/lib/plugins/syntax.php (revision 4b8edb9d27253137965d9c07ac1b0abe1af3bc31)
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     * 'block'  - Open paragraphs need to be closed before plugin output
66     * 'stack'  - Special case. Plugin wraps other paragraphs.
67     *
68     * @see Doku_Handler_Block
69     */
70    function getPType(){
71        return 'normal';
72    }
73
74    /**
75     * Handler to prepare matched data for the rendering process
76     *
77     * This function can only pass data to render() via its return value - render()
78     * may be not be run during the object's current life.
79     *
80     * Usually you should only need the $match param.
81     *
82     * @param   $match   string    The text matched by the patterns
83     * @param   $state   int       The lexer state for the match
84     * @param   $pos     int       The character position of the matched text
85     * @param   $handler ref       Reference to the Doku_Handler object
86     * @return  array              Return an array with all data you want to use in render
87     */
88    function handle($match, $state, $pos, &$handler){
89        trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
90    }
91
92    /**
93     * Handles the actual output creation.
94     *
95     * The function must not assume any other of the classes methods have been run
96     * during the object's current life. The only reliable data it receives are its
97     * parameters.
98     *
99     * The function should always check for the given mode and return false
100     * when a mode isn't supported.
101     *
102     * $renderer contains a reference to the renderer object which is
103     * currently handling the rendering. You need to use it for writing
104     * the output. How this is done depends on the renderer used (specified
105     * by $mode
106     *
107     * The contents of the $data array depends on what the handler() function above
108     * created
109     *
110     * @param   $mode     string   current Rendermode
111     * @param   $renderer ref      reference to the current renderer object
112     * @param   $data     array    data created by handler()
113     * @return  boolean            rendered correctly?
114     */
115    function render($mode, &$renderer, $data) {
116        trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
117
118    }
119
120    /**
121     *  There should be no need to override this function
122     */
123    function accepts($mode) {
124
125        if (!$this->allowedModesSetup) {
126            global $PARSER_MODES;
127
128            $allowedModeTypes = $this->getAllowedTypes();
129            foreach($allowedModeTypes as $mt) {
130                $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
131            }
132
133            unset($this->allowedModes[array_search(substr(get_class($this), 7), $this->allowedModes)]);
134            $this->allowedModesSetup = true;
135        }
136
137        return parent::accepts($mode);
138    }
139
140}
141//Setup VIM: ex: et ts=4 enc=utf-8 :