1<?php
2/**
3 * Outliner Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Michael Hamann <michael [at] content-space [dot] de>, Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
7 */
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_outliner extends DokuWiki_Syntax_Plugin
14{
15    /**
16     * What kind of syntax are we?
17     */
18    public function getType()
19    {
20        return 'container';
21    }
22
23    public function getAllowedTypes()
24    {
25        return array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
26    }
27
28    /* Accept own mode so nesting is allowed */
29    public function accepts($mode)
30    {
31      if ($mode == substr(get_class($this), 7)) return true;
32      return parent::accepts($mode);
33    }
34
35    public function getPType()
36    {
37        return 'block';
38    }
39
40    /**
41     * Where to sort in?
42     */
43    public function getSort()
44    {
45        return 10;
46    }
47
48    /**
49     * Connect pattern to lexer
50     */
51    public function connectTo($mode)
52    {
53        $this->Lexer->addEntryPattern(
54            '\n\s*-->[^\n]*(?=.*?\n\s*<--[^\n]*)',
55            $mode,
56            'plugin_outliner');
57    }
58
59    public function postConnect()
60    {
61        $this->Lexer->addExitPattern(
62            '\n\s*<--[^\n]*',
63            'plugin_outliner');
64    }
65
66    /**
67     * Handle the match
68     */
69    public function handle($match, $state, $pos, Doku_Handler $handler)
70    {
71        global $ID;
72
73        switch ($state) {
74        case DOKU_LEXER_ENTER:
75            $matches = array();
76            preg_match('/-->\s*([^#^@]*)([#^@]*)/', $match, $matches);
77            $title = $matches[1];
78            $outline_id = ''.md5($ID).'_'.$pos;
79
80            // Test if '^ - opened node' flag is present
81            $opened = (strpos($matches[2], '^') !== false);
82            // Test if '# - no popup' flag is present
83            $nopopup = (strpos($matches[2], '#') !== false);
84			// Test if '$ - link flag is present
85            $link = (strpos($matches[2], '@') !== false);
86            return array($state, $title, $outline_id, $opened, $nopopup, $link);
87
88        case DOKU_LEXER_EXIT:
89            return array($state);
90
91        case DOKU_LEXER_UNMATCHED :
92            return array($state, $match);
93
94        default:
95            return array();
96        }
97    }
98
99    /**
100     * Create output
101     */
102    public function render($format, Doku_Renderer $renderer, $data)
103    {
104      if ($format == 'xhtml') {
105          $state = $data[0];
106          switch ($state) {
107          case DOKU_LEXER_ENTER:
108              list($state, $title, $outline_id, $opened, $nopopup, $link) = $data;
109              $renderer->doc .= '<dl class="outliner';
110              // only set node id when cookies are used
111              if ($this->getConf('useLocalStorage'))
112                 $renderer->doc .= ' outl_'.$outline_id;
113              if ($opened) $renderer->doc .= ' outliner-open';
114              if ($nopopup) $renderer->doc .= ' outliner-nopopup';
115			  if ($link) {
116				  $charToReplace = array("[","]");
117				  $linkId = str_replace($charToReplace, "", $title);
118				  $linkId = explode('|', $linkId);
119				  $renderer->doc .= '"><dt>'.html_wikilink($linkId[0],$linkId[1])."</dt><dd>\n";
120			  } else{
121				  $renderer->doc .= '"><dt>'.hsc($title)."</dt><dd>\n";
122			  }
123              break;
124          case DOKU_LEXER_EXIT:
125              $renderer->doc .= "</dd></dl>\n";
126              break;
127          case DOKU_LEXER_UNMATCHED:
128              $renderer->cdata($data[1]);
129              break;
130          }
131          return true;
132      }
133      return false;
134    }
135}
136
137//Setup VIM: ex: et ts=4 enc=utf-8 :
138