1<?php
2/**
3 * DokuWiki Plugin sequencediagram (Syntax Component)
4 *
5 * @author  K.-M. Hansche <mf-developing@hansche.de>
6 */
7
8// must be run within Dokuwiki
9if (!defined('DOKU_INC')) die();
10
11class syntax_plugin_sequencediagram extends DokuWiki_Syntax_Plugin {
12    /**
13     * @return string Syntax mode type
14     */
15    public function getType() {
16        return 'substition';
17    }
18    /**
19     * @return string Paragraph type
20     */
21    public function getPType() {
22        return 'block'; // or normal?
23    }
24    /**
25     * @return int Sort order - Low numbers go before high numbers
26     */
27    public function getSort() {
28        return 999;
29    }
30
31    /**
32     * Connect lookup pattern to lexer.
33     *
34     * @param string $mode Parser mode
35     */
36    public function connectTo($mode) {
37        $this->Lexer->addSpecialPattern('<sequencediagram>.*?</sequencediagram>',$mode,'plugin_sequencediagram');
38    }
39
40//    public function postConnect() {
41//        $this->Lexer->addExitPattern('</FIXME>','plugin_sequencediagram_sd');
42//    }
43
44    /**
45     * Handle matches of the sequencediagram syntax
46     *
47     * @param string $match The match of the syntax
48     * @param int    $state The state of the handler
49     * @param int    $pos The position in the document
50     * @param Doku_Handler    $handler The handler
51     * @return array Data for the renderer
52     */
53    public function handle($match, $state, $pos, Doku_Handler $handler){
54        $data = array();
55        if ($state==DOKU_LEXER_SPECIAL){
56            array_push($data, $match);
57            //echo '<pre>';print_r($match);
58            //print_r($data); echo '</pre>';
59        }
60        return $data;
61    }
62
63    /**
64     * Render xhtml output or metadata
65     *
66     * @param string         $mode      Renderer mode (supported modes: xhtml)
67     * @param Doku_Renderer  $renderer  The renderer
68     * @param array          $data      The data from the handler() function
69     * @return bool If rendering was successful.
70     */
71    public function render($mode, Doku_Renderer $renderer, $data) {
72        if($mode == 'xhtml'){
73            try {
74                preg_match('/<sequencediagram>(.*?)<\/sequencediagram>/s', $data[0], $erg);
75                $src = $renderer->_xmlEntities($erg[1]);
76                $text="<div class=\"diagram\" style=\"overflow:auto;\">$src</div><script>var sdTheme='".$this->getConf('sequencediagram_layout')."'</script>";
77                $renderer->doc .= $text;
78            } catch (Exception $e) {
79              $renderer->doc .= "<pre>".htmlentities($text)."\n".$e."</pre>";
80            }
81            return true;
82        }
83
84        return false;
85    }
86}
87
88// vim:ts=4:sw=4:et:
89