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            return array($state, $match);
54          case DOKU_LEXER_EXIT :
55             return array($state,$match);
56
57          default:
58
59            return array($state,$match);
60        }
61    }
62
63    function render($mode, Doku_Renderer $renderer, $data) {
64
65        if($mode == 'xhtml'){
66
67          list($state, $match) = $data;
68
69          switch ($state) {
70            case DOKU_LEXER_ENTER :
71
72              $id = "";
73              if(preg_match('/^(toggle)(.*)/',$match, $matches)) {
74                 if($matches[2]) {
75                     $this->last_header = $matches[2];
76                     $match = $matches[1];  // class
77                 }
78                 $this->index++;
79                 $id = ' id="codedoc_' . $this->index . '" ';
80                 $this->last_id= '"codedoc_' . $this->index . '"';
81              }
82              if($this->last_id) {
83                $show_header ="";
84                $show_button = '<span class="codedoc_show" id="s_' . trim($this->last_id,'"') . '">show</span>';
85                if($this->last_header) {
86                    if($this->no_numbers) {
87                        $show_header = "$this->last_header";
88                    }
89                    else $show_header = "[$this->index]$this->last_header";
90                }
91                $renderer->doc .= "\n$show_header <a href='javascript:codedoc_toggle($this->last_id);void 0;'>$show_button</a>";
92                $this->last_id = "";
93                $this->last_header="";
94              }
95             if(strpos($match,':') != false) {
96                  list($match, $this->geshi) = explode(':',$match);
97                  $match = "$match " . $this->geshi;
98             }
99             $renderer->doc .= '<pre ' . $id  . 'class="'.$match.'">';
100              break;
101
102            case DOKU_LEXER_UNMATCHED :
103            if($this->geshi) {
104               $renderer->doc .= p_xhtml_cached_geshi($match, $this->geshi,'');
105            }
106             else $renderer->doc .= $renderer->_xmlEntities($match);
107             break;
108
109            case DOKU_LEXER_EXIT :
110              $renderer->doc .= "</pre>";
111               break;
112          }
113          return true;
114
115        }
116        // unsupported $mode
117        return false;
118    }
119
120 function write_debug($what) {
121
122  $handle = fopen('codeblock.txt', 'a');
123  fwrite($handle,"$what\n");
124  fclose($handle);
125 }
126}
127
128//Setup VIM: ex: et ts=4 enc=utf-8 :
129?>
130