1<?php
2
3/**
4 * DokuWiki Plugin imagelink (Syntax Component)
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author  peterfromearth
8 */
9
10/*
11 * All DokuWiki plugins to extend the parser/rendering mechanism
12 * need to inherit from this class
13 */
14class syntax_plugin_imagelink_imagelink extends DokuWiki_Syntax_Plugin {
15
16    public $pattern_start  = '<imagelink.*?>(?=.*?</imagelink>)';
17    public $pattern_end    = '</imagelink>';
18
19    /*
20     * What kind of syntax are we?
21     */
22    function getType() {return 'container';}
23
24    /*
25     * Where to sort in?
26     */
27    function getSort() {return 104;}
28
29    /*
30     * Paragraph Type
31     */
32    function getPType(){return 'block';}
33
34    public function getAllowedTypes() {
35        return array('substition');
36    }
37
38     public function connectTo($mode) {
39    	$this->Lexer->addEntryPattern($this->pattern_start,$mode,'plugin_imagelink_imagelink');
40    }
41
42   public function postConnect() {
43   	   $this->Lexer->addPattern("\{\{(?:[^\}]|(?:\}[^\}]))+\}\}",'plugin_imagelink_imagelink');
44
45       $this->Lexer->addExitPattern($this->pattern_end,'plugin_imagelink_imagelink');
46   }
47
48    /*
49     * Handle the matches
50     */
51   public function handle($match, $state, $pos, Doku_Handler $handler){
52       $data = array();
53       $data['state'] = $state;
54       switch($state) {
55           case DOKU_LEXER_ENTER:
56               $match = substr($match, 11,-1);  // strip markup
57               $flags = $this->parseFlags($match);
58               $data['flags'] = $flags;
59               break;
60           case DOKU_LEXER_UNMATCHED:
61               if (trim($match) !== '') {
62                   $handler->_addCall('cdata', array($match), $pos);
63               }
64               break;
65           case DOKU_LEXER_EXIT:
66               $this->first_item = false;
67               break;
68
69       }
70       return $data;
71   }
72
73    /*
74     * Create output
75     */
76   function render($mode, Doku_Renderer $renderer, $data)
77	{
78	    if($mode != 'xhtml') return false;
79
80	    if($data['state'] === DOKU_LEXER_ENTER) {
81	        $renderer->doc .= '<div class="plugin_linkimage ">';
82	        $renderer->doc .= '<div class="title">';
83	        $renderer->cdata($data['flags']['title']);
84	        $renderer->doc .= '</div>';
85
86	    } else if($data['state'] === DOKU_LEXER_EXIT) {
87
88	        $renderer->doc .= '</div>';
89	    }
90
91	    return true;
92	}
93
94	protected function parseFlags($confString) {
95	    $confString = explode('&',$confString);
96	    $flags = array(
97	        'title' => '',
98            'position' => 'center',
99            'width' => '100%',
100	        'hover' => false,
101	    );
102	    foreach($confString as $flag) {
103
104	        switch($flag) {
105	            case 'hover':
106	                $flags['hover'] = true;
107	                break;
108
109	        }
110
111	        $tmp = explode('=',$flag,2);
112	        if(count($tmp) === 2) {
113
114	            switch($tmp[0]) {
115	                case 'title':
116	                    $flags['title'] = $tmp[1];
117	                    break;
118	            }
119
120
121	        }
122	    }
123
124	    return $flags;
125	}
126
127}
128
129//Setup VIM: ex: et ts=4 enc=utf-8 :
130