1<?php
2/**
3 * Code Plugin: replaces Dokuwiki's own code syntax with SyntaxHighigter by Alex Gorbatchev.
4 *              This plugin is based off of SyntaxHighlighter 1.5.1 which can be found at
5 *              http://code.google.com/p/syntaxhighlighter/
6 *
7 * Syntax:     <code lang:nogutter:nocontrols:collapse:firstline[value]:showcolumns>
8 *   lang        (optional) programming language name that will be passed to syntaxhighlighter.
9 *               If not provided, the plugin will use the default syntax highlighting.
10 *               Refer to http://code.google.com/p/syntaxhighlighter/wiki/Languages for list of supported languages.
11 *               For more languages, please add the .js files contributed by other users.
12 *   nogutter    (optional) will display no gutter.
13 *   nocontrols  (optional) will display no controls at the top
14 *   collapse    (optional) will collapse the block by default
15 *   firstline[value] (optional) will begin line count at value.  Default value is 1.
16 *   showcolumns (optional) will show row columns in the first line.
17 *
18 * The options are passed together with the alias and are separated by a colon : character. e.g
19 * <pre name="code" class="html:nocontrols:firstline[10]">
20 * ... some code here ...
21 * </pre>
22 *
23 * @license    LGPL 2 (http://www.gnu.org/licenses/lgpl.html)
24 * @author     David Shin <dshin@pimpsmart.com>
25 */
26// must be run within Dokuwiki
27if (!defined('DOKU_INC')) die();
28if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
29require_once(DOKU_PLUGIN.'syntax.php');
30
31/**
32 * All DokuWiki plugins to extend the parser/rendering mechanism
33 * need to inherit from this class
34 */
35class syntax_plugin_syntaxhighlighter_code extends DokuWiki_Syntax_Plugin {
36
37    var $syntax = "";
38
39    /**
40     * return some info
41     */
42    function getInfo(){
43      return array(
44        'author' => 'David Shin',
45        'email'  => 'dshin@pimpsmart.com',
46        'date'   => '2008-12-04',
47        'name'   => 'SyntaxHighlighter Plugin',
48        'desc'   => 'Replacement for Dokuwiki\'s own <code> handler with SyntaxHighigter by Alex Gorbatchev.
49                     Syntax: <code lang:nogutter:nocontrols:collapse:firstline[value]:showcolumns>, all configurations are optional.',
50        'url'    => 'http://wiki.splitbrain.org/plugin:syntaxhighlighter',
51      );
52    }
53
54    function getType(){ return 'protected';}
55    function getSort(){ return 195; }
56    function getPType(){ return 'block'; }
57
58    /**
59     * Connect pattern to lexer
60     */
61    function connectTo($mode) {
62      $this->Lexer->addEntryPattern('<code(?=[^\r\n]*?>.*?</code>)',$mode,'plugin_syntaxhighlighter_code');
63    }
64
65    function postConnect() {
66      $this->Lexer->addExitPattern('</code>', 'plugin_syntaxhighlighter_code');
67    }
68
69    /**
70     * Handle the match
71     */
72    function handle($match, $state, $pos, &$handler){
73
74        switch ($state) {
75            case DOKU_LEXER_ENTER:
76                $this->syntax = substr($match, 1);
77                return false;
78
79            case DOKU_LEXER_UNMATCHED:
80                // will include everything from <code ... to ... </code >
81                // e.g. ... [lang]:nogutter:nocontrols:collapse:firstline[value]:showcolumns> [content]
82                list($attr, $content) = preg_split('/>/u',$match,2);
83
84                if ($this->syntax == 'code') {
85                    $attr = trim($attr);
86                    if ($attr == NULL) {
87                        $attr = 'html';
88                    }
89                } else {
90                    $attr = NULL;
91                }
92
93                return array($this->syntax, $attr, $content);
94        }
95        return false;
96    }
97
98    /**
99     * Create output
100     */
101    function render($mode, &$renderer, $data) {
102
103      if($mode == 'xhtml'){
104        if (count($data) == 3) {
105
106            list($syntax, $attr, $content) = $data;
107            if ($syntax == 'code')
108                $renderer->doc .= "<pre name=\"code\" class=\"".$attr."\">".$renderer->_xmlEntities($content)."</pre>";
109            else
110                $renderer->file($content);
111        }
112        return true;
113      }
114      return false;
115    }
116}
117
118