xref: /dokuwiki/lib/plugins/syntax.php (revision 166da0c59f106b2bc81be3dd2d9f074434261357)
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// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_INC.'inc/parser/parser.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode {
19
20    var $allowedModesSetup = false;
21    var $localised = false;         // set to true by setupLocale() after loading language dependent strings
22    var $lang = array();            // array to hold language dependent strings, best accessed via ->getLang()
23    var $configloaded = false;      // set to true by loadConfig() after loading plugin configuration variables
24    var $conf = array();            // array to hold plugin settings, best accessed via ->getConf()
25    var $match = null;              // string containing the match of a syntax plugin, accessed via
26                                    // it's setters and getters setMatch() and getMatch()
27    /**
28     * General Info
29     *
30     * Needs to return a associative array with the following values:
31     *
32     * author - Author of the plugin
33     * email  - Email address to contact the author
34     * date   - Last modified date of the plugin in YYYY-MM-DD format
35     * name   - Name of the plugin
36     * desc   - Short description of the plugin (Text only)
37     * url    - Website with more information on the plugin (eg. syntax description)
38     */
39    function getInfo(){
40        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
41    }
42
43    /**
44     * Syntax Type
45     *
46     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
47     */
48    function getType(){
49        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
50    }
51
52    /**
53     * Allowed Mode Types
54     *
55     * Defines the mode types for other dokuwiki markup that maybe nested within the
56     * plugin's own markup. Needs to return an array of one or more of the mode types
57     * defined in $PARSER_MODES in parser.php
58     */
59    function getAllowedTypes() {
60        return array();
61    }
62
63    /**
64     * Paragraph Type
65     *
66     * Defines how this syntax is handled regarding paragraphs. This is important
67     * for correct XHTML nesting. Should return one of the following:
68     *
69     * 'normal' - The plugin can be used inside paragraphs
70     * 'block'  - Open paragraphs need to be closed before plugin output
71     * 'stack'  - Special case. Plugin wraps other paragraphs.
72     *
73     * @see Doku_Handler_Block
74     */
75    function getPType(){
76        return 'normal';
77    }
78
79    /**
80     * Handler to prepare matched data for the rendering process
81     *
82     * This function can only pass data to render() via its return value - render()
83     * may be not be run during the object's current life.
84     *
85     * Usually you should only need the $match param.
86     *
87     * @param   $match   string    The text matched by the patterns
88     * @param   $state   int       The lexer state for the match
89     * @param   $pos     int       The character position of the matched text
90     * @param   $handler ref       Reference to the Doku_Handler object
91     * @return  array              Return an array with all data you want to use in render
92     */
93    function handle($match, $state, $pos, &$handler){
94        trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
95    }
96
97    /**
98     * Handles the actual output creation.
99     *
100     * The function must not assume any other of the classes methods have been run
101     * during the object's current life. The only reliable data it receives are its
102     * parameters.
103     *
104     * The function should always check for the given output format and return false
105     * when a format isn't supported.
106     *
107     * $renderer contains a reference to the renderer object which is
108     * currently handling the rendering. You need to use it for writing
109     * the output. How this is done depends on the renderer used (specified
110     * by $format
111     *
112     * The contents of the $data array depends on what the handler() function above
113     * created
114     *
115     * @param   $format   string   output format being rendered
116     * @param   $renderer ref      reference to the current renderer object
117     * @param   $data     array    data created by handler()
118     * @return  boolean            rendered correctly?
119     */
120    function render($format, &$renderer, $data) {
121        trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
122
123    }
124
125    /**
126     *  There should be no need to override these functions
127     */
128    function accepts($mode) {
129
130        if (!$this->allowedModesSetup) {
131            global $PARSER_MODES;
132
133            $allowedModeTypes = $this->getAllowedTypes();
134            foreach($allowedModeTypes as $mt) {
135                $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
136            }
137
138            $idx = array_search(substr(get_class($this), 7), (array) $this->allowedModes);
139            if ($idx !== false) {
140              unset($this->allowedModes[$idx]);
141            }
142            $this->allowedModesSetup = true;
143        }
144
145        return parent::accepts($mode);
146    }
147
148    // plugin introspection methods
149    // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
150    function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t;  }
151    function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
152    function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
153
154    // localisation methods
155    /**
156     * getLang($id)
157     *
158     * use this function to access plugin language strings
159     * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
160     * e.g. when info plugin is querying plugins for information about themselves.
161     *
162     * @param   $id     id of the string to be retrieved
163     * @return  string  string in appropriate language or english if not available
164     */
165    function getLang($id) {
166      if (!$this->localised) $this->setupLocale();
167
168      return (isset($this->lang[$id]) ? $this->lang[$id] : '');
169    }
170
171    /**
172     * locale_xhtml($id)
173     *
174     * retrieve a language dependent wiki page and pass to xhtml renderer for display
175     * plugin equivalent of p_locale_xhtml()
176     *
177     * @param   $id     id of language dependent wiki page
178     * @return  string  parsed contents of the wiki page in xhtml format
179     */
180    function locale_xhtml($id) {
181      return p_cached_output($this->localFN($id));
182    }
183
184    /**
185     * localFN($id)
186     * prepends appropriate path for a language dependent filename
187     * plugin equivalent of localFN()
188     */
189    function localFN($id) {
190      global $conf;
191      $plugin = $this->getPluginName();
192      $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
193      if(!@file_exists($file)){
194        //fall back to english
195        $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
196      }
197      return $file;
198    }
199
200    /**
201     *  setupLocale()
202     *  reads all the plugins language dependent strings into $this->lang
203     *  this function is automatically called by getLang()
204     */
205    function setupLocale() {
206        if ($this->localised) return;
207
208      global $conf;            // definitely don't invoke "global $lang"
209      $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
210
211      // don't include once, in case several plugin components require the same language file
212      @include($path.'en/lang.php');
213      if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
214
215      $this->lang = $lang;
216      $this->localised = true;
217    }
218
219  // configuration methods
220  /**
221   * getConf($setting)
222   *
223   * use this function to access plugin configuration variables
224   */
225  function getConf($setting){
226
227    if (!$this->configloaded){ $this->loadConfig(); }
228
229    return $this->conf[$setting];
230  }
231
232  /**
233   * loadConfig()
234   * merges the plugin's default settings with any local settings
235   * this function is automatically called through getConf()
236   */
237  function loadConfig(){
238    global $conf;
239
240    $defaults = $this->readDefaultSettings();
241    $plugin = $this->getPluginName();
242
243    foreach ($defaults as $key => $value) {
244      if (isset($conf['plugin'][$plugin][$key])) continue;
245      $conf['plugin'][$plugin][$key] = $value;
246    }
247
248    $this->configloaded = true;
249    $this->conf =& $conf['plugin'][$plugin];
250  }
251
252  /**
253   * read the plugin's default configuration settings from conf/default.php
254   * this function is automatically called through getConf()
255   *
256   * @return    array    setting => value
257   */
258  function readDefaultSettings() {
259
260    $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
261    $conf = array();
262
263    if (@file_exists($path.'default.php')) {
264      include($path.'default.php');
265    }
266
267    return $conf;
268  }
269
270  /**
271   * setMatch is the setter of the match string in a syntax plugin
272   *
273   * @author Pierre Spring <pierre.spring@liip.ch>
274   * @param string $match
275   * @access public
276   * @return void
277   */
278  function setMatch($match)
279  {
280    $this->match = (string) $match;
281  }
282  /**
283   * getMatch is the getter of the match string in a syntax plugin
284   *
285   * @author Pierre Spring <pierre.spring@liip.ch>
286   * @access public
287   * @return string
288   */
289  function getMatch()
290  {
291    return $this->match;
292  }
293
294}
295//Setup VIM: ex: et ts=4 enc=utf-8 :
296