1<?php
2/**
3 * Jirainfo syntax plugin for DokuWiki
4 *
5 * @author     Vadim Balabin <vadikflint@gmail.com>
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_jirainfo extends DokuWiki_Syntax_Plugin
17{
18    public function getType() {	return 'substition'; }
19    public function getSort() { return 361; }
20	public function connectTo($mode) { $this->Lexer->addEntryPattern('<(?:ji|jirainfo).*?>(?=.*?</(?:ji|jirainfo)>)', $mode, 'plugin_jirainfo'); }
21    public function postConnect() { $this->Lexer->addExitPattern('</(?:ji|jirainfo)>','plugin_jirainfo'); }
22
23
24    public function handle($match, $state, $pos, Doku_Handler $handler){
25        switch ($state) {
26
27          case DOKU_LEXER_ENTER :
28            $xml = simplexml_load_string(str_replace('>', '/>', $match));
29            //$tag = $xml->getName();
30
31			foreach ($xml->attributes() as $key => $value) {
32				$attributes[$key] = (string) $value;
33            }
34            if (array_key_exists('key', $attributes)) return array($state, $attributes['key']);
35          break;
36
37          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
38          case DOKU_LEXER_EXIT :       return array($state, '');
39        }
40        return array();
41    }
42
43    /**
44     * check - correct attributes
45     *
46     * @param  Array $attributes
47     *
48     * @return boolean
49     */
50    public function check(array $attributes)
51    {
52        return array_key_exists('key', $attributes);
53    }
54
55    public function render($mode, Doku_Renderer $renderer, $data)
56    {
57        // $data is what the function handle() return'ed.
58        if($mode == 'xhtml') {
59
60            list($state, $match) = $data;
61            switch ($state) {
62                case DOKU_LEXER_ENTER:
63                    list($state, $key) = $data;
64                    $renderer->doc .= sprintf('<a class="jirainfo" href="javascript:void(0);" data-key="%s">', $key);
65                    break;
66
67                case DOKU_LEXER_UNMATCHED:
68                    $renderer->doc .= htmlentities($match);
69                    break;
70
71                case DOKU_LEXER_EXIT:
72                    $renderer->doc .= '</a>';
73                    break;
74            }
75        }
76    }
77}