xref: /dokuwiki/lib/plugins/syntax.php (revision bf5d40c2ffe5ebcef76254c83a17e3955796fa0d)
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    var $localised = false;            // set to true by setupLocale() after loading language dependent strings
21    var $lang = array();            // array to hold language dependent strings, best accessed via ->getLang()
22
23    /**
24     * General Info
25     *
26     * Needs to return a associative array with the following values:
27     *
28     * author - Author of the plugin
29     * email  - Email address to contact the author
30     * date   - Last modified date of the plugin in YYYY-MM-DD format
31     * name   - Name of the plugin
32     * desc   - Short description of the plugin (Text only)
33     * url    - Website with more information on the plugin (eg. syntax description)
34     */
35    function getInfo(){
36        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
37    }
38
39    /**
40     * Syntax Type
41     *
42     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
43     */
44    function getType(){
45        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
46    }
47
48    /**
49     * Allowed Mode Types
50     *
51     * Defines the mode types for other dokuwiki markup that maybe nested within the
52     * plugin's own markup. Needs to return an array of one or more of the mode types
53     * defined in $PARSER_MODES in parser.php
54     */
55    function getAllowedTypes() {
56        return array();
57    }
58
59    /**
60     * Paragraph Type
61     *
62     * Defines how this syntax is handled regarding paragraphs. This is important
63     * for correct XHTML nesting. Should return one of the following:
64     *
65     * 'normal' - The plugin can be used inside paragraphs
66     * 'block'  - Open paragraphs need to be closed before plugin output
67     * 'stack'  - Special case. Plugin wraps other paragraphs.
68     *
69     * @see Doku_Handler_Block
70     */
71    function getPType(){
72        return 'normal';
73    }
74
75    /**
76     * Handler to prepare matched data for the rendering process
77     *
78     * This function can only pass data to render() via its return value - render()
79     * may be not be run during the object's current life.
80     *
81     * Usually you should only need the $match param.
82     *
83     * @param   $match   string    The text matched by the patterns
84     * @param   $state   int       The lexer state for the match
85     * @param   $pos     int       The character position of the matched text
86     * @param   $handler ref       Reference to the Doku_Handler object
87     * @return  array              Return an array with all data you want to use in render
88     */
89    function handle($match, $state, $pos, &$handler){
90        trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
91    }
92
93    /**
94     * Handles the actual output creation.
95     *
96     * The function must not assume any other of the classes methods have been run
97     * during the object's current life. The only reliable data it receives are its
98     * parameters.
99     *
100     * The function should always check for the given output format and return false
101     * when a format isn't supported.
102     *
103     * $renderer contains a reference to the renderer object which is
104     * currently handling the rendering. You need to use it for writing
105     * the output. How this is done depends on the renderer used (specified
106     * by $format
107     *
108     * The contents of the $data array depends on what the handler() function above
109     * created
110     *
111     * @param   $format   string   output format to being Rendered
112     * @param   $renderer ref      reference to the current renderer object
113     * @param   $data     array    data created by handler()
114     * @return  boolean            rendered correctly?
115     */
116    function render($format, &$renderer, $data) {
117        trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
118
119    }
120
121    /**
122     *  There should be no need to override these functions
123     */
124    function accepts($mode) {
125
126        if (!$this->allowedModesSetup) {
127            global $PARSER_MODES;
128
129            $allowedModeTypes = $this->getAllowedTypes();
130            foreach($allowedModeTypes as $mt) {
131                $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
132            }
133
134            unset($this->allowedModes[array_search(substr(get_class($this), 7), $this->allowedModes)]);
135            $this->allowedModesSetup = true;
136        }
137
138        return parent::accepts($mode);
139    }
140
141    // plugin introspection methods
142    // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
143    function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t;  }
144    function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
145    function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
146
147    // localisation methods
148    /**
149     * getLang($id)
150     *
151     * use this function to access plugin language strings
152     * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
153     * e.g. when info plugin is querying plugins for information about themselves.
154     *
155     * @param   $id     id of the string to be retrieved
156     * @return  string  string in appropriate language or english if not available
157     */
158    function getLang($id) {
159      if (!$this->localised) $this->setupLocale();
160
161      return (isset($this->lang[$id]) ? $this->lang[$id] : '');
162    }
163
164    /**
165     * locale_xhtml($id)
166     *
167     * retrieve a language dependent wiki page and pass to xhtml renderer for display
168     * plugin equivalent of p_locale_xhtml()
169     *
170     * @param   $id     id of language dependent wiki page
171     * @return  string  parsed contents of the wiki page in xhtml format
172     */
173    function locale_xhtml($id) {
174      return p_cached_xhtml($this->localFN($id));
175    }
176
177    /**
178     * localFN($id)
179     * prepends appropriate path for a language dependent filename
180     * plugin equivalent of localFN()
181     */
182    function localFN($id) {
183      global $conf;
184      $plugin = $this->getPluginName();
185      $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
186      if(!@file_exists($file)){
187        //fall back to english
188        $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
189      }
190      return $file;
191    }
192
193    /**
194     *  setupLocale()
195     *  reads all the plugins language dependent strings into $this->lang
196     *  this function is automatically called by getLang()
197     */
198    function setupLocale() {
199        if ($this->localised) return;
200
201      global $conf;            // definitely don't invoke "global $lang"
202      $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
203
204      // don't include once, in case several plugin components require the same language file
205      @include($path.'en/lang.php');
206      if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
207
208      $this->lang = $lang;
209      $this->localised = true;
210    }
211
212}
213//Setup VIM: ex: et ts=4 enc=utf-8 :
214