1<?php
2/**
3 * File Plugin: replaces Dokuwiki's own file syntax
4 *
5 * Syntax:     <file |title>
6 *   title     (optional) all text after '|' will be rendered above the main code text with a
7 *             different style.
8 *
9 * if no title is provided will render as native dokuwiki code syntax mode, e.g.
10 *   <pre class='file'> ... </pre>
11 *
12 * if title is provide will render as follows
13 *   <div class='file'>
14 *     <p>{title}</p>
15 *     <pre class='code'> ... </pre>
16 *   </div>
17 *
18 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
19 * @author     Christopher Smith <chris@jalakai.co.uk>
20 * @author     Ilya Lebedev <ilya@lebedev.net>
21 */
22
23if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
24if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
25require_once(DOKU_PLUGIN.'syntax.php');
26
27/**
28 * All DokuWiki plugins to extend the parser/rendering mechanism
29 * need to inherit from this class
30 */
31class syntax_plugin_code_file extends DokuWiki_Syntax_Plugin {
32
33    var $syntax = "";
34
35    /**
36     * return some info
37     */
38    function getInfo(){
39      return array(
40        'author' => 'Christopher Smith',
41        'email'  => 'chris@jalakai.co.uk',
42        'date'   => '2005-08-18',
43        'name'   => '<file> replacement plugin',
44        'desc'   => 'Replacement for Dokuwiki\'s own <file> handler, adds a title to the box and JavaScript-based syntax highlighting.
45                     Syntax: <file|title>, title is optional and does not support any dokuwiki markup.',
46        'url'    => 'https://www.dokuwiki.org/plugin:code3',
47      );
48    }
49
50    function getType(){ return 'protected';}
51    function getPType(){ return 'block'; }
52
53    // must return a number lower than returned by native 'file' mode (210)
54    function getSort(){ return 194; }
55
56
57    /**
58     * Connect pattern to lexer
59     */
60    function connectTo($mode) {
61      $this->Lexer->addEntryPattern('<file(?=[^\r\n]*?>.*?</file>)',$mode,'plugin_code_file');
62    }
63
64    function postConnect() {
65      $this->Lexer->addExitPattern('</file>', 'plugin_code_file');
66    }
67
68    /**
69     * Handle the match
70     */
71    function handle($match, $state, $pos, &$handler){
72
73        switch ($state) {
74            case DOKU_LEXER_ENTER:
75                $this->syntax = substr($match, 1);
76                return false;
77
78            case DOKU_LEXER_UNMATCHED:
79                // will include everything from <file ... to ... </file >
80                // e.g. ... [lang] [|title] > [content]
81                list($attr, $content) = preg_split('/>/u',$match,2);
82                list($lang, $title) = preg_split('/\|/u',$attr,2);
83
84                if ($this->syntax == 'code') {
85                    $lang = trim($lang);
86                    if ($lang == 'html') $lang = 'html4strict';
87                    if (!$lang) $lang = NULL;
88                } else {
89                    $lang = NULL;
90                }
91
92                return array($this->syntax, $lang, trim($title), $content);
93        }
94        return false;
95    }
96
97    /**
98     * Create output
99     */
100    function render($mode, &$renderer, $data) {
101
102      if($mode == 'xhtml'){
103        if (count($data) == 4) {
104
105            list($syntax, $lang, $title, $content) = $data;
106            if ($title) $renderer->doc .= "<div class='$syntax'><p>".$renderer->_xmlEntities($title)."</p>";
107            if ($syntax == 'code')
108                $renderer->doc .= "<pre class=\"code\">".$renderer->_xmlEntities($content)."</pre>";
109            else
110                $renderer->file($content);
111            if ($title) $renderer->doc .= "</div>";
112        }
113
114        return true;
115      }
116      return false;
117    }
118}
119
120//Setup VIM: ex: et ts=4 enc=utf-8 :
121