Mereged updates from DokuWiki 38
[sudaraka-org:dokuwiki-mods.git] / inc / parser / code.php
1 <?php
2 /**
3  * A simple renderer that allows downloading of code and file snippets
4  *
5  * @author Andreas Gohr <andi@splitbrain.org>
6  */
7 if(!defined('DOKU_INC')) die('meh.');
8 require_once DOKU_INC . 'inc/parser/renderer.php';
9
10 class Doku_Renderer_code extends Doku_Renderer {
11     var $_codeblock=0;
12
13     /**
14      * Send the wanted code block to the browser
15      *
16      * When the correct block was found it exits the script.
17      */
18     function code($text, $language = NULL, $filename='' ) {
19         global $INPUT;
20         if(!$language) $language = 'txt';
21         if(!$filename) $filename = 'snippet.'.$language;
22         $filename = utf8_basename($filename);
23
24         if($this->_codeblock == $INPUT->str('codeblock')){
25             header("Content-Type: text/plain; charset=utf-8");
26             header("Content-Disposition: attachment; filename=$filename");
27             header("X-Robots-Tag: noindex");
28             echo trim($text,"\r\n");
29             exit;
30         }
31
32         $this->_codeblock++;
33     }
34
35     /**
36      * Wraps around code()
37      */
38     function file($text, $language = NULL, $filename='') {
39         $this->code($text, $language, $filename);
40     }
41
42     /**
43      * This should never be reached, if it is send a 404
44      */
45     function document_end() {
46         header("HTTP/1.0 404 Not Found");
47         echo '404 - Not found';
48         exit;
49     }
50
51     /**
52      * Return the format of the renderer
53      *
54      * @returns string 'code'
55      */
56     function getFormat(){
57         return 'code';
58     }
59 }