xref: /dokuwiki/lib/plugins/syntax.php (revision f1f771344f46009891de4c83bfff6043da81231b)
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    var $configloaded = false;      // set to true by loadConfig() after loading plugin configuration variables
23
24    /**
25     * General Info
26     *
27     * Needs to return a associative array with the following values:
28     *
29     * author - Author of the plugin
30     * email  - Email address to contact the author
31     * date   - Last modified date of the plugin in YYYY-MM-DD format
32     * name   - Name of the plugin
33     * desc   - Short description of the plugin (Text only)
34     * url    - Website with more information on the plugin (eg. syntax description)
35     */
36    function getInfo(){
37        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
38    }
39
40    /**
41     * Syntax Type
42     *
43     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
44     */
45    function getType(){
46        trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
47    }
48
49    /**
50     * Allowed Mode Types
51     *
52     * Defines the mode types for other dokuwiki markup that maybe nested within the
53     * plugin's own markup. Needs to return an array of one or more of the mode types
54     * defined in $PARSER_MODES in parser.php
55     */
56    function getAllowedTypes() {
57        return array();
58    }
59
60    /**
61     * Paragraph Type
62     *
63     * Defines how this syntax is handled regarding paragraphs. This is important
64     * for correct XHTML nesting. Should return one of the following:
65     *
66     * 'normal' - The plugin can be used inside paragraphs
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 output format and return false
102     * when a format 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 $format
108     *
109     * The contents of the $data array depends on what the handler() function above
110     * created
111     *
112     * @param   $format   string   output format to being Rendered
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($format, &$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 these functions
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    // plugin introspection methods
143    // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
144    function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t;  }
145    function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
146    function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
147
148    // localisation methods
149    /**
150     * getLang($id)
151     *
152     * use this function to access plugin language strings
153     * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
154     * e.g. when info plugin is querying plugins for information about themselves.
155     *
156     * @param   $id     id of the string to be retrieved
157     * @return  string  string in appropriate language or english if not available
158     */
159    function getLang($id) {
160      if (!$this->localised) $this->setupLocale();
161
162      return (isset($this->lang[$id]) ? $this->lang[$id] : '');
163    }
164
165    /**
166     * locale_xhtml($id)
167     *
168     * retrieve a language dependent wiki page and pass to xhtml renderer for display
169     * plugin equivalent of p_locale_xhtml()
170     *
171     * @param   $id     id of language dependent wiki page
172     * @return  string  parsed contents of the wiki page in xhtml format
173     */
174    function locale_xhtml($id) {
175      return p_cached_xhtml($this->localFN($id));
176    }
177
178    /**
179     * localFN($id)
180     * prepends appropriate path for a language dependent filename
181     * plugin equivalent of localFN()
182     */
183    function localFN($id) {
184      global $conf;
185      $plugin = $this->getPluginName();
186      $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
187      if(!@file_exists($file)){
188        //fall back to english
189        $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
190      }
191      return $file;
192    }
193
194    /**
195     *  setupLocale()
196     *  reads all the plugins language dependent strings into $this->lang
197     *  this function is automatically called by getLang()
198     */
199    function setupLocale() {
200        if ($this->localised) return;
201
202      global $conf;            // definitely don't invoke "global $lang"
203      $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
204
205      // don't include once, in case several plugin components require the same language file
206      @include($path.'en/lang.php');
207      if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
208
209      $this->lang = $lang;
210      $this->localised = true;
211    }
212
213    // configuration methods
214    /**
215     * getConf($id)
216     *
217     * use this function to access plugin configuration variables
218     */
219    function getConf($id){
220      global $conf;
221
222      $plugin = $this->getPluginName();
223
224      if (!$this->configloaded){
225        if ($pconf = $this->loadConfig() !== false){
226          foreach ($pconf as $key => $value){
227            if (isset($conf['plugin'][$plugin][$key])) continue;
228            $conf['plugin'][$plugin][$key] = $value;
229          }
230          $this->configloaded = true;
231        }
232      }
233
234      return $conf['plugin'][$plugin][$id];
235    }
236
237    /**
238     * loadConfig()
239     * reads all plugin configuration variables into $this->conf
240     * this function is automatically called by getConf()
241     */
242    function loadConfig(){
243      $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
244      $conf = array();
245
246      if (!@file_exists($path.'default.php')) return false;
247
248      // load default config file
249      include($path.'default.php');
250
251      return $conf;
252    }
253
254}
255//Setup VIM: ex: et ts=4 enc=utf-8 :
256