French language fix FS#1875
[dokuwiki/radio.git] / lib / plugins / syntax.php
blob633e001d23807594f372f3a02a19b91045e8af18
1 <?php
2 /**
3 * Syntax Plugin Prototype
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
9 if(!defined('DOKU_INC')) die();
11 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12 require_once(DOKU_INC.'inc/parser/parser.php');
14 /**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
18 class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode {
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()
26 /**
27 * General Info
29 * Needs to return a associative array with the following values:
31 * author - Author of the plugin
32 * email - Email address to contact the author
33 * date - Last modified date of the plugin in YYYY-MM-DD format
34 * name - Name of the plugin
35 * desc - Short description of the plugin (Text only)
36 * url - Website with more information on the plugin (eg. syntax description)
38 function getInfo(){
39 $parts = explode('_',get_class($this));
40 $info = DOKU_PLUGIN.'/'.$parts[2].'/plugin.info.txt';
41 if(@file_exists($info)) return confToHash($info);
42 trigger_error('getInfo() not implemented in '.get_class($this).' and '.$info.' not found', E_USER_WARNING);
45 /**
46 * Syntax Type
48 * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
50 function getType(){
51 trigger_error('getType() not implemented in '.get_class($this), E_USER_WARNING);
54 /**
55 * Allowed Mode Types
57 * Defines the mode types for other dokuwiki markup that maybe nested within the
58 * plugin's own markup. Needs to return an array of one or more of the mode types
59 * defined in $PARSER_MODES in parser.php
61 function getAllowedTypes() {
62 return array();
65 /**
66 * Paragraph Type
68 * Defines how this syntax is handled regarding paragraphs. This is important
69 * for correct XHTML nesting. Should return one of the following:
71 * 'normal' - The plugin can be used inside paragraphs
72 * 'block' - Open paragraphs need to be closed before plugin output
73 * 'stack' - Special case. Plugin wraps other paragraphs.
75 * @see Doku_Handler_Block
77 function getPType(){
78 return 'normal';
81 /**
82 * Handler to prepare matched data for the rendering process
84 * This function can only pass data to render() via its return value - render()
85 * may be not be run during the object's current life.
87 * Usually you should only need the $match param.
89 * @param $match string The text matched by the patterns
90 * @param $state int The lexer state for the match
91 * @param $pos int The character position of the matched text
92 * @param $handler ref Reference to the Doku_Handler object
93 * @return array Return an array with all data you want to use in render
95 function handle($match, $state, $pos, &$handler){
96 trigger_error('handle() not implemented in '.get_class($this), E_USER_WARNING);
99 /**
100 * Handles the actual output creation.
102 * The function must not assume any other of the classes methods have been run
103 * during the object's current life. The only reliable data it receives are its
104 * parameters.
106 * The function should always check for the given output format and return false
107 * when a format isn't supported.
109 * $renderer contains a reference to the renderer object which is
110 * currently handling the rendering. You need to use it for writing
111 * the output. How this is done depends on the renderer used (specified
112 * by $format
114 * The contents of the $data array depends on what the handler() function above
115 * created
117 * @param $format string output format being rendered
118 * @param $renderer ref reference to the current renderer object
119 * @param $data array data created by handler()
120 * @return boolean rendered correctly?
122 function render($format, &$renderer, $data) {
123 trigger_error('render() not implemented in '.get_class($this), E_USER_WARNING);
128 * There should be no need to override these functions
130 function accepts($mode) {
132 if (!$this->allowedModesSetup) {
133 global $PARSER_MODES;
135 $allowedModeTypes = $this->getAllowedTypes();
136 foreach($allowedModeTypes as $mt) {
137 $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
140 $idx = array_search(substr(get_class($this), 7), (array) $this->allowedModes);
141 if ($idx !== false) {
142 unset($this->allowedModes[$idx]);
144 $this->allowedModesSetup = true;
147 return parent::accepts($mode);
150 // plugin introspection methods
151 // extract from class name, format = <plugin type>_plugin_<name>[_<component name>]
152 function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t; }
153 function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; }
154 function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); }
156 // localisation methods
158 * getLang($id)
160 * use this function to access plugin language strings
161 * to try to minimise unnecessary loading of the strings when the plugin doesn't require them
162 * e.g. when info plugin is querying plugins for information about themselves.
164 * @param $id id of the string to be retrieved
165 * @return string string in appropriate language or english if not available
167 function getLang($id) {
168 if (!$this->localised) $this->setupLocale();
170 return (isset($this->lang[$id]) ? $this->lang[$id] : '');
174 * locale_xhtml($id)
176 * retrieve a language dependent wiki page and pass to xhtml renderer for display
177 * plugin equivalent of p_locale_xhtml()
179 * @param $id id of language dependent wiki page
180 * @return string parsed contents of the wiki page in xhtml format
182 function locale_xhtml($id) {
183 return p_cached_output($this->localFN($id));
187 * localFN($id)
188 * prepends appropriate path for a language dependent filename
189 * plugin equivalent of localFN()
191 function localFN($id) {
192 global $conf;
193 $plugin = $this->getPluginName();
194 $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt';
195 if(!@file_exists($file)){
196 //fall back to english
197 $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt';
199 return $file;
203 * setupLocale()
204 * reads all the plugins language dependent strings into $this->lang
205 * this function is automatically called by getLang()
207 function setupLocale() {
208 if ($this->localised) return;
210 global $conf; // definitely don't invoke "global $lang"
211 $path = DOKU_PLUGIN.$this->getPluginName().'/lang/';
213 // don't include once, in case several plugin components require the same language file
214 @include($path.'en/lang.php');
215 if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
217 $this->lang = $lang;
218 $this->localised = true;
221 // configuration methods
223 * getConf($setting)
225 * use this function to access plugin configuration variables
227 function getConf($setting){
229 if (!$this->configloaded){ $this->loadConfig(); }
231 return $this->conf[$setting];
235 * loadConfig()
236 * merges the plugin's default settings with any local settings
237 * this function is automatically called through getConf()
239 function loadConfig(){
240 global $conf;
242 $defaults = $this->readDefaultSettings();
243 $plugin = $this->getPluginName();
245 foreach ($defaults as $key => $value) {
246 if (isset($conf['plugin'][$plugin][$key])) continue;
247 $conf['plugin'][$plugin][$key] = $value;
250 $this->configloaded = true;
251 $this->conf =& $conf['plugin'][$plugin];
255 * read the plugin's default configuration settings from conf/default.php
256 * this function is automatically called through getConf()
258 * @return array setting => value
260 function readDefaultSettings() {
262 $path = DOKU_PLUGIN.$this->getPluginName().'/conf/';
263 $conf = array();
265 if (@file_exists($path.'default.php')) {
266 include($path.'default.php');
269 return $conf;
273 * Allow the plugin to prevent DokuWiki creating a second instance of itself
275 * @return bool true if the plugin can not be instantiated more than once
277 function isSingleton() {
278 return false;
281 //Setup VIM: ex: et ts=4 enc=utf-8 :