1<?php
2
3if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
4if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5require_once(DOKU_PLUGIN.'syntax.php');
6
7
8class syntax_plugin_codedoc_block extends DokuWiki_Syntax_Plugin {
9    var $index = 0;
10    var $last_id = 0;
11    var $last_header = "";
12    var $geshi = false;
13    var $no_numbers = false;
14
15    function getType(){ return 'container'; }
16    function getPType(){ return 'block'; }
17    function getAllowedTypes() {
18      return array('substition');
19    }
20    function getSort(){ return 25; }
21
22    // override default accepts() method to allow nesting
23    // - ie, to get the plugin to accept its own entry syntax
24    function accepts($mode) {
25      if ($mode == substr(get_class($this), 7)) return true;
26        return parent::accepts($mode);
27      }
28
29    function connectTo($mode) {
30        $this->Lexer->addEntryPattern('<codedoc.*?>(?=.*?</codedoc>)',$mode,'plugin_codedoc_block');
31        $this->Lexer->addEntryPattern('<codetoggle.*?>(?=.*?</codetoggle)',$mode,'plugin_codedoc_block');
32    }
33    function postConnect() {
34        $this->Lexer->addExitPattern('</codedoc>','plugin_codedoc_block');
35        $this->Lexer->addExitPattern('</codetoggle>','plugin_codedoc_block');
36    }
37
38    function handle($match, $state, $pos, Doku_Handler $handler){
39
40        switch ($state) {
41
42          case DOKU_LEXER_ENTER :
43            $type = strtolower(trim(substr($match,8,-1)));
44            $type = str_replace('_no_numbers',"",$type,$count);
45            if($count) {
46               $type = trim($type);
47               $this->no_numbers =  true;
48            }
49
50            return array($state, trim($type));
51
52         /* case DOKU_LEXER_UNMATCHED :
53            $match = str_replace('<','&lt;',$match);
54            $match = str_replace('>','&gt;',$match);
55            return array($state, $match); */
56          case DOKU_LEXER_EXIT :
57             return array($state,$match);
58
59          default:
60
61            return array($state,$match);
62        }
63    }
64
65    function render($mode, Doku_Renderer $renderer, $data) {
66
67        if($mode == 'xhtml'){
68
69          list($state, $match) = $data;
70
71          switch ($state) {
72            case DOKU_LEXER_ENTER :
73
74              $id = "";
75              if(preg_match('/^(toggle)(.*)/',$match, $matches)) {
76                 if($matches[2]) {
77                     $this->last_header = $matches[2];
78                     $match = $matches[1];  // class
79                 }
80                 $this->index++;
81                 $id = ' id="codedoc_' . $this->index . '" ';
82                 $this->last_id= '"codedoc_' . $this->index . '"';
83              }
84              if($this->last_id) {
85                $show_header ="";
86                $show_button = '<span class="codedoc_show" id="s_' . trim($this->last_id,'"') . '">show</span>';
87                if($this->last_header) {
88                    if($this->no_numbers) {
89                        $show_header = "$this->last_header";
90                    }
91                    else $show_header = "[$this->index]$this->last_header";
92                }
93                $renderer->doc .= "\n$show_header <a href='javascript:codedoc_toggle($this->last_id);void 0;'>$show_button</a>";
94                $this->last_id = "";
95                $this->last_header="";
96              }
97             if(strpos($match,':') != false) {
98                  list($match, $this->geshi) = explode(':',$match);
99                  $match = "$match " . $this->geshi;
100             }
101             $renderer->doc .= '<pre ' . $id  . 'class="'.$match.'">';
102              break;
103
104            case DOKU_LEXER_UNMATCHED :
105            if($this->geshi) {
106               $renderer->doc .= p_xhtml_cached_geshi($match, $this->geshi,'');
107            }
108             else $renderer->doc .= $renderer->_xmlEntities($match);
109             break;
110
111            case DOKU_LEXER_EXIT :
112              $renderer->doc .= "</pre>";
113               break;
114          }
115          return true;
116
117        }
118        // unsupported $mode
119        return false;
120    }
121
122 function write_debug($what) {
123
124  $handle = fopen('codeblock.txt', 'a');
125  fwrite($handle,"$what\n");
126  fclose($handle);
127 }
128}
129
130//Setup VIM: ex: et ts=4 enc=utf-8 :
131?>
132