1 <?php
2 /**
3  *
4  * @author     Szymon Olewniczak
5  */
6 
7 // must be run within DokuWiki
8 if(!defined('DOKU_INC')) die();
9 
10 /**
11  * All DokuWiki plugins to extend the parser/rendering mechanism
12  * need to inherit from this class
13  */
14 class syntax_plugin_randompage2 extends DokuWiki_Syntax_Plugin {
15 
16 public function getType(){ return 'formatting'; }
17     public function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
18     public function getSort(){ return 158; }
19     public function connectTo($mode) { $this->Lexer->addEntryPattern('<randompage_link>(?=.*?</randompage_link>)',$mode,'plugin_randompage2'); }
20     public function postConnect() { $this->Lexer->addExitPattern('</randompage_link>','plugin_randompage2'); }
21 
22 
23     /**
24      * Handle the match
25      */
26     public function handle($match, $state, $pos, Doku_Handler $handler){
27         switch ($state) {
28           case DOKU_LEXER_ENTER :
29                 return array($state, '');
30 
31           case DOKU_LEXER_UNMATCHED :  return array($state, $match);
32           case DOKU_LEXER_EXIT :       return array($state, '');
33         }
34         return array();
35     }
36 
37     /**
38      * Create output
39      */
40     public function render($mode, Doku_Renderer $renderer, $data) {
41 		global $ID;
42         // $data is what the function handle() return'ed.
43         if($mode == 'xhtml'){
44             /** @var Doku_Renderer_xhtml $renderer */
45             list($state, $match) = $data;
46             switch ($state) {
47                 case DOKU_LEXER_ENTER :
48                     $renderer->doc .= '<a href="'.wl($ID, array('do' => 'randompage'), true).'">';
49                     break;
50 
51                 case DOKU_LEXER_UNMATCHED :
52                     $renderer->doc .= $renderer->_xmlEntities($match);
53                     break;
54                 case DOKU_LEXER_EXIT :
55                     $renderer->doc .= '</a>';
56                     break;
57             }
58             return true;
59         }
60         return false;
61     }
62 }
63