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