1<?php
2/**
3 * DokuWiki Plugin admnote (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Klaus Vormweg <klaus.vormweg@gmx.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_admnote_renderer extends DokuWiki_Syntax_Plugin {
15    /* possible note types */
16    var $types = array('abstract','bug','danger','example','failure','information',
17                       'note','question','quote','achievement','tip','warning');
18    /* default type */
19    var $deftype = 'note';
20    /* title switch */
21    var $handletitle = FALSE;
22
23    /**
24     * @return string Syntax mode type
25     */
26    public function getType() {
27        return 'container';
28    }
29
30    /**
31     * @return string Paragraph type
32     */
33    public function getPType() {
34        return 'stack';
35    }
36
37    /**
38     * @return int Sort order - Low numbers go before high numbers
39     */
40    public function getSort() {
41        return 0;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * @param string $mode Parser mode
48     */
49    public function connectTo($mode) {
50        $this->Lexer->addEntryPattern('<adm ?[^>]*>', $mode, 'plugin_admnote_renderer');
51    }
52
53    public function postConnect() {
54        $this->Lexer->addExitPattern('</adm>', 'plugin_admnote_renderer');
55    }
56
57    /**
58     * Allowed modes inside admnotes
59     *
60     * @return array allowed modes
61     */
62    public function getAllowedTypes() {
63        return array('container','formatting','substition','protected','disabled','paragraphs');
64    }
65
66    /**
67     * Handle matches of the admnote syntax
68     *
69     * @param string       $match   The match of the syntax
70     * @param int          $state   The state of the handler
71     * @param int          $pos     The position in the document
72     * @param Doku_Handler $handler The handler
73     *
74     * @return array Data for the renderer
75     */
76    public function handle($match, $state, $pos, Doku_Handler $handler) {
77        global $conf;
78        if(is_file(DOKU_PLUGIN.'admnote/lang/'.$conf['lang'].'/lang.php')) {
79            require DOKU_PLUGIN.'admnote/lang/'.$conf['lang'].'/lang.php';
80        } else {
81            require DOKU_PLUGIN.'admnote/lang/en/lang.php';
82        }
83
84        $data = array();
85        $cssClass = '';
86        $heading = '';
87        switch($state) {
88            case DOKU_LEXER_ENTER:
89                $match = trim(str_replace(array('<','>'),'',$match));
90                $match = preg_replace('/ +/',' ',$match);
91                $words = explode(' ',$match);
92                array_shift($words);
93                if($words[0] and in_array(strtolower($words[0]), $this->types)) {
94                    $cssClass = strtolower(array_shift($words));
95                } else {
96                    $cssClass = $this->deftype;
97                }
98                if($words) {
99                    $heading = implode(' ',$words);
100                } else {
101                    $heading = $lang['adm_'.$cssClass];
102                }
103                $data = array($state, $cssClass.'#'.$heading);
104                break;
105
106            case DOKU_LEXER_UNMATCHED:
107                $data = array($state, $match);
108                break;
109
110            case DOKU_LEXER_EXIT:
111                $data = array($state, $match);
112                break;
113
114            default:
115                $data = array($state, '');
116        }
117        return $data;
118    }
119
120    /**
121     * Render xhtml output or metadata
122     *
123     * @param string        $mode     renderer mode (supported modes: xhtml)
124     * @param Doku_renderer $renderer The renderer
125     * @param array         $data     The data from the handler() function
126     *
127     * @return bool If rendering was successful.
128     */
129    public function render($mode, Doku_renderer $renderer, $data) {
130      if($mode == 'xhtml') {
131          list($state, $text) = $data;
132
133          switch($state) {
134              case DOKU_LEXER_ENTER:
135                  list($cssClass, $heading) = explode('#',$text);
136                  $renderer->doc .= '<div class="admonition '.$cssClass.'">'."\n";
137                  $renderer->doc .= '<p class="admonition-title">'.
138                                    $renderer->_xmlEntities($heading)."</p>\n";
139                  break;
140
141              case DOKU_LEXER_UNMATCHED:
142                  $renderer->doc .= $renderer->_xmlEntities($text);
143                  break;
144
145              case DOKU_LEXER_EXIT:
146                  $renderer->doc .= "\n</div>";
147                  break;
148          }
149          return true;
150      }
151
152      // unsupported $mode
153      return false;
154    }
155}
156